Repository navigation
fix: use pointer receiver in QueueLock.fetchLock so the per-queue orderly lock actually locks - #1240
Open
wang-jiahua wants to merge 1 commit into
Open
wang-jiahua wants to merge 1 commit into
wang-jiahua wants to merge 1 commit into
Conversation
…erly lock actually locks
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Pull request overview
Fixes broken per-queue mutual exclusion by changing QueueLock.fetchLock to a pointer receiver so all callers share the same sync.Map-backed lock table, restoring FIFO guarantees for orderly consumption.
Changes:
- Change
QueueLock.fetchLockreceiver from value to pointer to prevent copying a struct containingsync.Map. - Add deterministic unit tests to verify
fetchLockreturns the same lock per queue and enforces mutual exclusion under contention.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| consumer/lock.go | Switches fetchLock to a pointer receiver to ensure the lock table is shared and effective. |
| consumer/lock_test.go | Adds regression tests proving the bug and validating mutual exclusion behavior. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+62
to
+84
| var wg sync.WaitGroup | ||
| for i := 0; i < 20; i++ { | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| lock := ql.fetchLock(mq) | ||
| lock.Lock() | ||
| defer lock.Unlock() | ||
|
|
||
| stateMu.Lock() | ||
| concurrent++ | ||
| if concurrent > maxConcurrent { | ||
| maxConcurrent = concurrent | ||
| } | ||
| stateMu.Unlock() | ||
|
|
||
| time.Sleep(2 * time.Millisecond) | ||
|
|
||
| stateMu.Lock() | ||
| concurrent-- | ||
| stateMu.Unlock() | ||
| }() | ||
| } |
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.
Which Issue(s) This PR Fixes
Fixes #1239
Brief Description
QueueLock.fetchLockused a value receiver on a struct containing async.Map. Every call copied the struct,LoadOrStorewrote into the discarded copy, and each caller got a brand-new mutex — so the per-queue lock taken byconsumeMessageOrderlynever actually excluded anyone, breaking the FIFO guarantee of orderly consumption.go vetflags this as copylocks.The fix is one character: value receiver → pointer receiver, so all callers share the same
lockTableand the same per-queue mutex.How Did You Test This Change?
Two new deterministic tests in
consumer/lock_test.go, both red on the unfixed code and green with the fix:TestFetchLockReturnsSameLockForSameQueue: unfixed returns two different mutexes for the same queue; fixed returns the identical one.TestFetchLockProvidesMutualExclusion: 20 goroutines contend on one queue while recording max concurrency — unfixed observesmaxConcurrent=20(no serialization at all); fixed observesmaxConcurrent=1.go vet ./consumer/copylocks diagnostic: present before, gone after.gofmtclean; the consumer test suite passes with no regressions.