fix(retain): globalise memory_links lock order on the insert path (#3396) - #3406
Merged
Conversation
) The deadlock #2570 targeted still fired a few times a day because the insert-side lock ordering was only partial: 1. _bulk_insert_links sorted on (from, to) — two of the four columns in the unique index (from, to, link_type, COALESCE(entity_id, nil)). A temporal and a semantic edge on the same pair compared equal, so a stable sort left them in input order and concurrent inserts could take the two index entries in opposite orders. 2. That order also disagreed with chunk_storage.delete_chunks_by_ids, which normalises direction via LEAST/GREATEST. Two different total orders can still cycle. Sort both paths on one canonical key — the full, direction-normalised unique key — by extracting _lock_order_key and pointing the insert sort at it. The delete side already uses exactly this order and is unchanged.
Contributor
|
Heads-up: this crossed with new evidence on #3396 — server-log |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3396.
The deadlock #2570 targeted still fired a few times a day (per @kubaodias in #3387):
DeadlockDetectedErrorfromlink_utilsduring temporal-link insertion, absorbed by the worker retry. Reading both write paths, the insert-side lock ordering was only partial in two ways.What was wrong
1. The insert sort key covered only half the unique key.
_bulk_insert_linkssorted on(from_unit_id, to_unit_id)— two of the four columns in the unique index(from, to, link_type, COALESCE(entity_id, nil)). Atemporaland asemanticedge on the same(from, to)pair compared equal; Python's stable sort then left them in input order, which isn't stable across transactions, so two concurrent inserts could take the two index entries in opposite orders.2. Insert and delete used different total orders.
chunk_storage.delete_chunks_by_idsorders by(LEAST(from, to), GREATEST(from, to), link_type, COALESCE(entity_id, nil))— direction-normalised so(A,B)and(B,A)sort adjacent. The insert sorted on the raw(from, to), placing them far apart. Both orders are internally total but different, and a consistent lock order requires every writer to share one.The fix
Both call sites now sort on one canonical key — the full, direction-normalised unique key. Extracted
_lock_order_keyand pointed the insert sort at it; the delete side already uses exactly this order and is unchanged.UUID string ordering matches Postgres
uuidbyte ordering because the ids are canonical lowercase-hex (the same assumption the oldstr()sort already relied on).Tests
TestLockOrderKeyintests/test_link_utils.py— 4 pure unit tests: direction normalisation,link_typedisambiguation (mechanism 1),None→ nil-UUID mapping, and that a mixed batch reproduces the delete'sORDER BY. Full file: 31/31 pass; ruff +tyclean.Note
As the issue says, this is derived from reading the two paths, not a reproduction — it closes both ordering gaps rather than betting on which one fires in production. Severity is low (a few/day, fully absorbed by the worker retry); this makes the #2570 ordering guarantee actually global.