ISSUE DESCRIPTION Range-lock conflicts can strand waiters in stale queues A waiter that…
- User
ISSUE DESCRIPTION
Range-lock conflicts can strand waiters in stale queues
A waiter that moves to a new range conflict must no longer remain in stale queues. Cleanup must preserve unrelated lock state, wake the next eligible waiter when necessary, and continue to work when the previous key disappeared or was recreated.
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /matrixone, current file: none) bash-$
Assistant
I'll start by exploring the repository structure to understand the codebase and then investigate the range-lock issue.
Assistant
This is MatrixOne, a Go-based database. The issue is about range-lock conflicts stranding waiters in stale queues. Let me find the range-lock related code.
Assistant
The issue is about range-lock conflicts stranding waiters in stale queues. Let me look at the relevant files: lock_table_local.go, waiter_queue.go, waiter.go, deadlock.go.
Assistant
Let me see the full file, particularly the doAcquireLock and related range lock handling:
Assistant
Let me look at the
closeWaitermethod and the Lock struct:Assistant
Let me look at the waiter.go and waiter_events.go files:
Assistant
Now let me look at the
closeWaiterfunction more carefully and understand the bug. Let me also look at how the current test cases for range lock conflict work.Let me search for tests related to rangeLastWaitKey and closeWaiter.
Assistant
Let me look at the git history and recent changes around this area to understand the context:
Assistant
Let me search for all callers of notify/notifyAll and closeWaiter to understand the wake-up paths:
Assistant
Let me look at the existing range lock tests to understand the scenarios:
Assistant
This is a very detailed test. Let me see the whole scenario, particularly the part where the waiter gets stranded. Let me continue reading.
Assistant
Let me run the existing range conflict test to see if it passes currently:
Assistant
OK that test passes. Let me run the full lockservice test suite to see the baseline.
Assistant
Let me look at the lockContext struct and how c.idx / c.offset are used:
Assistant
Let me understand the
doLockflow better. Afterwait()returns:c.w.resetWait(l.logger)c.offset = c.idx- ...
Wait,
c.offset = c.idx. But inhandleLockConflictLocked,c.offset = i(for range). So after the wait, doLock RESETSc.offset = c.idx. What'sc.idx?Let me look at how c.idx is set. In acquireRowLockLocked,
c.offset = idx. There's no c.idx set there. Let me search.Assistant
Let me search the codebase for any hints about stranding or stale queues, and look at the deadlock/orphan tests which heavily exercise these paths:
Assistant
All tests pass currently. Now let me deeply analyze the
closeWaiterlogic and the move path. Let me look at the waiter_queue_test.go and the types:Assistant
Let me check existing tests that use closeWaiter directly and understand the test for the doLock error path:
Assistant
Let me check if there are any uncommitted changes or stashed content, and look at the orphan/deadlock tests which stress these paths:
Assistant
Let me look at the txn.go cancel/closeBlockWaiters paths which notify waiters:
Assistant
Let me check all implementations of the
waiterQueueinterface:Assistant
Now I have a clear understanding. Let me write a reproduction test first to confirm my understanding of the bug. Let me create a test that stresses range-lock waiter moves with multiple waiters and checks for stranded waiters:
Assistant
Let me write a temporary reproduction test to confirm the bug in
closeWaiterdirectly:Assistant
I've confirmed both bugs:
- Recreated empty lock wrongly deleted —
closeWaiterreturnstruefor an empty lock wherewisn't even present. - Non-head waiter stranded —
closeWaiterleaveswin the queue whenwis not the head.
Now let me implement the fix. First, add
removeWaiterto the waiter queue:- Recreated empty lock wrongly deleted —
Assistant
Let me make the edits using the editor. First the interface:
Assistant
Now add the
removeWaiterimplementation. Let me view the removeByTxnID area:Assistant
Now rewrite
Lock.closeWaiterin lock.go:Assistant
Now update
handleLockConflictLockedto move the range cleanup BEFOREaddWaiter, so the recreated+same-key case works correctly:Assistant
Let me restructure the function. I'll move the cleanup block to before
addWaiter:Assistant
Let me verify the new
handleLockConflictLockedorder:Assistant
Now let me build and run my reproduction tests:
Assistant
Both reproduction tests now pass. Let me run the broader range/deadlock related tests:
Assistant
Now let me run the full lockservice test suite:
Assistant
A test failed. Let me find which one:
Assistant
Let me find the exact failing test name:
Assistant
Two real failures. Let me look at them and run individually:
Assistant
TestRowLockWithConflictAndUnlock: txn1 holds row lock. txn2 blocked by txn1 (waits). Thens.Unlock(ctx, txn2, ...)— txn2 unlocks (but it doesn't hold the lock, it's a waiter). This is supposed to abort/cancel txn2's wait. Then check that the lock still has txn2 as waiter with refcount 1 (was 3).Wait, let me look at the check:
checkLock(t, lt, rows[0], [][]byte{txn1}, [][]byte{txn2}, []int32{3}) // holders: txn1, waiters: txn2, refcounts: [3] require.NoError(t, s.Unlock(ctx, txn2, timestamp.Timestamp{})) <-c checkLock(t, lt, rows[0], [][]byte{txn1}, [][]byte{txn2}, []int32{1}) // holders: txn1, waiters: txn2, refcounts: [1]So before unlock txn2: refcount 3. After unlock txn2: refcount 1.
Hmm wait, refcount 3 for the waiter txn2. Let me think about the refcount. The waiter has refs from: acquireWaiter (1), queue (1), blockedWaiters (1)? = 3. After txn2 unlock (which cancels txn2's blocked waiters), the blockedWaiters ref is removed (closeBlockWaiters/closeWaiter). So refcount drops to...
Wait, let me understand.
s.Unlock(ctx, txn2, ...)— txn2 didn't hold the lock. So unlock iterates txn2's held locks (none for this table). But unlock also callstxn.close(...)which doescancelBlocksorcloseBlockWaiters. Let me check.Actually,
s.Unlock→txn.close(txnID, commitTS, ...). Let me look at txn.close.The test expects: after txn2 unlock, the waiter txn2 is STILL in the queue (waiters: txn2) but with refcount 1. So the waiter is NOT removed from the queue, just its refcount reduced (blockedWaiters ref removed).
So this test RELIES on the waiter NOT being removed from the queue when txn2 is unlocked/aborted. With my change, does the closeWaiter get called and remove the waiter?
Wait,
s.Unlock(ctx, txn2, ...)for a txn that's a waiter (not holder). Let me trace.txn.close→ let me look.Assistant
Let me look at
checkLockandwaiter_events.addto understand refcounts:Assistant
I see —
TestRowLockWithConflictAndUnlockexpects that when a txn aborts while waiting on a held lock (holders > 0), the waiter is NOT removed (left for lazy cleanup by the holder's release). My change removed it eagerly. The move path always has holders == 0 (the holder released to wake the waiter), so I need to preserve the "holders > 0 → don't remove" behavior and only fix the holders == 0 case.Let me also reconsider the restructure. With the holders guard, the cleanup-before-addWaiter move is unnecessary. Let me revert
handleLockConflictLockedto the original order to minimize changes:Assistant
Now update
closeWaiterto preserve the "holders > 0 → don't remove" behavior (lazy cleanup) while fixing the holders == 0 stranding:Assistant
Let me verify the
removeWaitersignatures are consistent:Assistant
Both previously-failing tests now pass. Let me run the full lockservice test suite:
Assistant
The full lockservice suite passes. Now let me remove my temporary reproduction test file and run a broader check to ensure nothing else depends on the changed files:
Assistant
The build errors are unrelated cgo C-header issues (usearch/xxhash) — not related to my changes. The lockservice package builds fine. Let me re-run the full lockservice suite without the repro file to confirm a clean pass:
Assistant
Let me re-add a comprehensive temporary test to verify all the issue's requirements, then remove it before submitting:
Assistant
All requirement-based tests pass. Now let me remove the temporary test and run the relevant range/deadlock/orphan tests once more to be safe:
Assistant
Let me review the final diff of my changes:
Assistant
The diff is clean and minimal. Let me run the most concurrency-sensitive tests with the race detector to ensure no data races were introduced:
Assistant
No data races. Let me run the full lockservice suite with the race detector to be thorough:
Assistant
The entire lockservice test suite passes with the race detector enabled, no data races, no failures.
Let me do a final review of the complete changes and confirm there are no leftover temp files:
Assistant
No temp files remain. Let me do a final review of all three changed sections to ensure correctness:
Assistant
Let me verify formatting with gofmt:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →