Skip to content

Fixes to locking mechanism#3

Merged
ashishp03 merged 2 commits into
mainfrom
development/firstFeature
Jun 1, 2026
Merged

Fixes to locking mechanism#3
ashishp03 merged 2 commits into
mainfrom
development/firstFeature

Conversation

@ashishp03

Copy link
Copy Markdown
Collaborator

Fix TOCTOU race and double lock acquisition in conversation query path

process_query_locally acquired state.index.read() twice for conversation queries that missed the conversation namespace — once to check the conv namespace, then again for the base fallback. Between the two acquisitions the lock was fully released, allowing the WAL flush task to write to the index between the two reads. This meant the two lookups could observe the index at different points in time: a concurrent insert into the conversation namespace could land in the window and be silently missed, returning scope: "global" when the entry was already in scope: "conversation".

Adds SemanticIndex::query_two_level, which runs both namespace lookups under a single &self borrow. The caller holds the read guard for the duration, so both checks see the same consistent snapshot. As a side effect this halves the lock acquisition count on the conversation miss path — under WAL flush write pressure each extra read().await is a scheduling stall.

The process_query_locally handler is updated to call query_two_level when a conversation_id is present. Six deterministic unit tests cover the new method: conv hit, base fallback, both miss, conv priority, exact-match through the two-level path, and dimension mismatch error propagation.

@ashishp03
ashishp03 merged commit 410b99c into main Jun 1, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant