Skip to content

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
apache:masterfrom
wang-jiahua:fix/queuelock-value-receiver
Open

wang-jiahua wants to merge 1 commit into
apache:masterfrom
wang-jiahua:fix/queuelock-value-receiver

Conversation

@wang-jiahua

Copy link
Copy Markdown

Which Issue(s) This PR Fixes

Fixes #1239

Brief Description

QueueLock.fetchLock used a value receiver on a struct containing a sync.Map. Every call copied the struct, LoadOrStore wrote into the discarded copy, and each caller got a brand-new mutex — so the per-queue lock taken by consumeMessageOrderly never actually excluded anyone, breaking the FIFO guarantee of orderly consumption. go vet flags this as copylocks.

The fix is one character: value receiver → pointer receiver, so all callers share the same lockTable and 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 observes maxConcurrent=20 (no serialization at all); fixed observes maxConcurrent=1.

go vet ./consumer/ copylocks diagnostic: present before, gone after. gofmt clean; the consumer test suite passes with no regressions.

Copilot AI lite review requested due to automatic review settings September 3, 2026 10:11

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.fetchLock receiver from value to pointer to prevent copying a struct containing sync.Map.
  • Add deterministic unit tests to verify fetchLock returns 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 thread consumer/lock_test.go
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()
}()
}
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.

[Bug] Orderly consumption per-queue lock is ineffective: QueueLock.fetchLock value receiver copies the sync.Map and returns a new mutex every call

2 participants