Skip to content

[MOD-16691] Rename spec-lock state to SpecLockState and add debug-only release asserts at BG cycle boundaries (step 1) - #10353

Merged
GuyAv46 merged 3 commits into
masterfrom
guyav-spec-lock-state
Jul 9, 2026
Merged

GuyAv46 merged 3 commits into
masterfrom
guyav-spec-lock-state

Conversation

@GuyAv46

@GuyAv46 GuyAv46 commented Jul 5, 2026 •

Copy link
Copy Markdown
Collaborator

Describe the changes in the pull request

Step 1 of the blocked-client / cross-thread ownership refactor designed in #9426. Independent of step 0 (#10336) — the two can land in either order.

  1. Current: the spec-lock state on RedisSearchCtx is a misleadingly named RSContextFlags flags field, and nothing checks that a background query cycle releases the spec read-lock on the worker thread that acquired it. A leaked lock would be silently released later from whichever thread happens to free the request — for pthread_rwlock_t, unlocking from a non-owning thread is undefined behavior.
  2. Change:
    • Pure rename: RSContextFlags flags → SpecLockState lock_state (SPEC_LOCK_UNSET/READ/WRITE). Lock primitives keep their exact signatures and behavior, including RedisSearchCtx_UnlockSpec's idempotent no-op on UNSET (hybrid relies on it).
    • Add a debug-only (ENABLE_ASSERT) RedisSearchCtx_AssertLockNotHeld macro next to the lock primitives, and place it at the background request-cycle boundaries: the three worker-pool entry points (AREQ_Execute_Callback, cursorRead_ctx, HREQ_Execute_Callback) and the post-execution exits while the request is still provably alive (inside AREQ_Execute before the final DecrRef, the AREQ_Execute_Callback error label, runCursor after the post-sendChunk unlock, and before blockedClientHybridCtx_destroy). Compiles to nothing in release builds.
    • Coordinator paths are untouched (coord BG work never takes the spec lock).
  3. Outcome: rename plus debug-build assertions documenting the "lock is taken and released within one BG cycle, on one worker thread" invariant; zero release-build impact, no behavior change. Verified: full DEBUG build; C/C++ unit tests 895/895; test_blocked_client_timeout.py 54/54, test_cursors.py 28/28, test_hybrid_timeout.py 30/30, test_multithread.py 33/33.

Which additional issues this PR fixes

  1. MOD-16691 (implementation; design: MOD-15430)
  2. Design: Design Refactor for the Multi-threaded Lifetime of a Query - [MOD-15430] #9426

Main objects this PR modified

  1. RedisSearchCtx.lock_state (SpecLockState) and RedisSearchCtx_AssertLockNotHeld, src/search_ctx.h / src/redis_index.c
  2. BG entry/exit paths in src/aggregate/aggregate_exec.c, src/hybrid/hybrid_exec.c

Mark if applicable

  • This PR introduces API changes
  • This PR introduces serialization changes

Release Notes

  • This PR requires release notes
  • This PR does not require release notes

🤖 Generated with Claude Code

GuyAv46 and others added 2 commits July 2, 2026 18:18
Pure mechanical rename, no behavior change: the enum tracked the state of
the per-query spec rwlock, not generic context flags. RS_CTX_UNSET ->
SPEC_LOCK_UNSET, RS_CTX_READONLY -> SPEC_LOCK_READ, RS_CTX_READWRITE ->
SPEC_LOCK_WRITE, and the RedisSearchCtx field 'flags' -> 'lock_state'.

Co-Authored-By: Claude Fable 5 <[email protected]>
Add RequestCycle_AssertLockUnset / RequestCycle_EnsureLockReleased bookends
around every background (worker-pool) query execution cycle. The spec rwlock
must be taken and released on the same worker thread within one cycle;
releasing it later from another thread (request free / client unblock on the
main thread) is undefined behavior for pthread_rwlock.

- Entry asserts: AREQ_Execute_Callback, cursorRead_ctx, HREQ_Execute_Callback.
- Exit checks: AREQ_Execute (after unlock, before the final DecrRef),
  runCursor (after unlock, before Cursor_Pause/Free/reply-store handoff),
  the AREQ_Execute_Callback error label and early-bail paths, the
  cursorRead dropped-index bail, and HREQ_Execute_Callback before
  blockedClientHybridCtx_destroy.

Debug builds assert on a leaked lock; release builds recover by
force-unlocking on the same worker thread and logging a warning.
Coordinator BG paths are untouched (they never take the spec lock).

Co-Authored-By: Claude Fable 5 <[email protected]>
@github-actions github-actions Bot added the size:M label Jul 5, 2026
@GuyAv46
GuyAv46 requested a review from lerman25 July 6, 2026 07:55
Replace the RequestCycle_AssertLockUnset / RequestCycle_EnsureLockReleased
helper functions (which included a release-build warn + force-unlock recovery
path) with a single debug-only RedisSearchCtx_AssertLockNotHeld macro next to
the lock primitives in search_ctx.h. RS_LOG_ASSERT already compiles to nothing
without ENABLE_ASSERT, so release builds are untouched.

Also drop the tautological early-bail checks: on those paths the entry assert
already proved the lock state was clean and no code in between takes the lock.
Kept sites: the three BG entry points, and the genuine post-execution exits
(AREQ_Execute, the AREQ_Execute_Callback error label, runCursor,
HREQ_Execute_Callback).

Co-Authored-By: Claude Fable 5 <[email protected]>
@github-actions github-actions Bot added size:S and removed size:M labels Jul 6, 2026
@GuyAv46
GuyAv46 marked this pull request as ready for review July 6, 2026 09:07
@GuyAv46
GuyAv46 enabled auto-merge July 6, 2026 09:08
@GuyAv46 GuyAv46 changed the title [MOD-15430] Rename spec-lock state to SpecLockState and assert it is released at BG cycle boundaries (step 1) [MOD-16691] Rename spec-lock state to SpecLockState and add debug-only release asserts at BG cycle boundaries (step 1) Jul 6, 2026
@sonarqubecloud

sonarqubecloud Bot commented Jul 6, 2026

Copy link
Copy Markdown

@codecov

codecov Bot commented Jul 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.39%. Comparing base (98be4a3) to head (a67a57a).
⚠️ Report is 18 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #10353      +/-   ##
==========================================
- Coverage   81.48%   81.39%   -0.10%     
==========================================
  Files         541      543       +2     
  Lines       73331    73723     +392     
  Branches    28779    29026     +247     
==========================================
+ Hits        59757    60009     +252     
- Misses      13307    13445     +138     
- Partials      267      269       +2     
Flag Coverage Δ
flow 84.03% <100.00%> (-0.06%) ⬇️
unit 51.20% <64.28%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lerman25 lerman25 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

@GuyAv46
GuyAv46 added this pull request to the merge queue Jul 9, 2026
Merged via the queue into master with commit 87276ca Jul 9, 2026
59 of 66 checks passed
@GuyAv46
GuyAv46 deleted the guyav-spec-lock-state branch July 9, 2026 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants