fix(entity): close concurrency race + Unicode NFC/NFD split#189
Merged
KailasMahavarkar merged 1 commit intomainfrom May 2, 2026
Merged
fix(entity): close concurrency race + Unicode NFC/NFD split#189KailasMahavarkar merged 1 commit intomainfrom
KailasMahavarkar merged 1 commit intomainfrom
Conversation
Stress smoke (8 threads x 50 ingests of same name) leaked 2-4 duplicate
entities per run. Two bugs:
1. Read-decide-write race. resolve_mention() reads candidates, returns
"no match" + a fresh entity_id; caller then issues CREATE NODE. Two
threads racing on the same name both observe zero candidates and
both mint distinct fresh ids - producing N entities for what should
be 1.
Fix: new resolve_and_create_entity() wraps the read+write critical
section in a process-global lock and a name -> entity_id cache.
Cache short-circuits the second concurrent caller without a NODES
query, bypassing any read-after-write visibility lag. After the
fix, 5 consecutive 8-thread x 50-mention runs all produce exactly
1 canonical entity.
2. NFC vs NFD encodings of the same name normalized to different
strings. "Muller" written by source A (NFC: U+00FC composed) and
source B (NFD: u + U+0308 combining) collapsed to "mller" vs
"muller" because [^a-z0-9] strips the combining mark only after
NFD decomposition.
Fix: normalize_name now NFKD-decomposes before lower + strip. Both
encodings collapse to "muller". Confirmed via stress scenario 7.
bonsai_ingestor: synthesizer uses resolve_and_create_entity instead of
resolve_mention so the entity write happens under the lock. When
gs is None (dry-run / unit tests) the synthesizer still emits the
CREATE NODE for the entity since the wrapper hasn't run.
Test plan:
pytest tests/test_entity_resolver.py tests/test_bonsai_ingestor.py
-> 115 passed
pytest --tb=short -q ...
-> 1952 passed, 102 skipped
Stress smoke (10 scenarios, 22+ assertions):
9/10 PASS. Outstanding INVESTIGATE: replay of same msg_id raises
"Node already exists: 'm1'" - by-design CREATE semantics, not
this PR's scope. Caller pipelines must dedupe msg_ids upstream
or use UPSERT NODE for idempotent retries.
Throughput: 216-231 ingests/s on 16-core CPU, model2vec embedder.
reset_resolver_cache_for_tests() exposed for test isolation; pytest
fixture and stress-smoke fresh_store() both clear the cache so prior
runs don't leak entity ids.
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.
Adversarial stress smoke (10 scenarios, 22+ assertions) found two production-relevant bugs in the resolver:
1. Concurrency race (HIGH severity for LLM-rate ingest)
resolve_mention()reads candidates, returns "no match" + fresh entity_id; caller issuesCREATE NODE. Two threads racing on the same name both observe zero candidates → both mint distinct ids → N entities for one human.Repro: 8 threads × 50 ingests of name "Bob" leaked 2-4 canonical Bobs per run.
Fix: new
resolve_and_create_entity()wraps the read+write under a process-global lock + name→entity_id cache. Cache short-circuits the second concurrent caller without aNODESquery, bypassing read-after-write visibility lag.Result: 5 consecutive 8-thread × 50-mention runs all produce exactly 1 canonical entity.
2. NFC vs NFD Unicode split (MEDIUM)
"Müller" with composed ü (NFC, U+00FC) and decomposed ü (NFD, u + U+0308) normalized to
mllervsmullerbecause the regex strips the combining mark only AFTER NFD decomposition.Fix:
normalize_nameNFKD-decomposes first; both encodings →muller.bonsai_ingestor
Synthesizer uses
resolve_and_create_entityso the entity write happens under the lock. Tests withgs=Nonestill get a CREATE NODE for the entity inline (the wrapper isn't called).Test plan
Stress smoke (
/tmp/smoke_entity_stress.py, 10 scenarios):Outstanding (separate work):
msg_idraisesNode already exists: 'm1'. By-design CREATE semantics. Pipelines must dedupe msg_ids upstream or use UPSERT for idempotent retries.