Skip to content

fix(amqp091): close race on BrokerDetails.clientDisconnect#102

Open
miotte wants to merge 1 commit into
mainfrom
miotte-pr7
Open

fix(amqp091): close race on BrokerDetails.clientDisconnect#102
miotte wants to merge 1 commit into
mainfrom
miotte-pr7

Conversation

@miotte
Copy link
Copy Markdown
Contributor

@miotte miotte commented May 22, 2026

Fixes #101

Summary

  • Switch BrokerDetails.clientDisconnect from bool to atomic.Bool so the connectionWatcher goroutine and the Disconnect caller can no longer race. Mirrors the pattern already used on the analogous flag in streamshim.go:40.
  • All reads in connectionWatcher (amqp091.go:1797, :1817) and connect() (amqp091.go:1863) move to Load(); the single write in disconnectClientByIdentifier (amqp091.go:1337) moves to Store(true).
  • The clientDisconnect: false line in the Connect struct literal and the three bd.clientDisconnect = false re-initializations in Test_connect_connecting_* were dropped — atomic.Bool's zero value is already false.

Test plan

  • go test -race -run "Test_StreamRetry|Test_Disconnect$|Test_SubscribeStream" -count=5 ./internal/provider/connectors/amqp091/...Test_Disconnect (amqp091_test.go:1721) and the Test_SubscribeStream* family (stream_test.go:584, …) hit the same disconnectClientByIdentifierconnectionWatcher race as Test_StreamRetry and are also cleared by this fix.
  • Full go test -race ./internal/provider/connectors/amqp091/... (note: pre-existing bd.state races in Test_Retry, Test_WaitForConnect, Test_connect_connecting_* are out of scope for this PR)
  • go vet ./...

Verification (2026-05-30):

The connectionWatcher goroutine reads bd.clientDisconnect in its main
loop and in the reconnect retry loop, while Disconnect writes it from
the caller goroutine. Both sides were plain bool accesses with no
synchronization, which the race detector reliably flags during
Test_StreamRetry (and several other tests that exercise the
Connect/Disconnect path).

Switch the field to atomic.Bool, matching the pattern already used for
the analogous flag on streamConnectionShim in streamshim.go. The
connect() early-out and both watcher loops use Load(); Disconnect uses
Store(true).

The struct-literal initializer was dropped because atomic.Bool's zero
value is already false, and the three test sites that re-set the flag
to false on a freshly-constructed BrokerDetails were similarly
redundant.

Signed-off-by: Michael Otteni <MichaelGOtteni@gmail.com>
@miotte miotte marked this pull request as ready for review May 28, 2026 23:31
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.

Race in BrokerDetails.clientDisconnect between Disconnect and connectionWatcher

2 participants