test: stop the sim-ferroamp broker deadlocking on close - #706
Merged
Conversation
The package hung for the full 10-minute go test timeout in CI, then passed on rerun. The goroutine dump from the failing run points at mochi-mqtt v2.7.9, not at our test logic: Clients.GetByListener takes the read lock and then calls Clients.Len, which takes it again. sync.RWMutex is not reentrant, so a Clients.Delete landing between the two read locks blocks the second one while the writer waits for the first. Both goroutines stop for good. Server.Close walks GetByListener, and the broker runs Clients.Delete the moment a client drops, so `defer cli.Disconnect(100)` followed by `defer s.Close()` is the race. v2.7.9 is the newest release, so there is nothing to upgrade to. Route every shutdown through closeBroker, which waits for the client count to fall back to the inline client before closing. No disconnect is then in flight and no writer can queue between the two read locks. A 5-second deadline on Close is the backstop: should the broker still wedge, the test says so in seconds rather than burning ten minutes on an unrelated pull request. Cap the core Go job at 120s per package for the same reason. The slowest package takes about 25s, so the margin is wide and any future hang costs two minutes instead of ten. Also check the connect token properly. WaitTimeout reports whether the token finished in time, so `tok.WaitTimeout(...) && tok.Error() != nil` let a timed-out connect through and the test failed later for a reason that read like a broker bug. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This was referenced Jul 30, 2026
Draft
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.
What hangs
go/cmd/sim-ferroamphung for the full 10-minutego testtimeout in run 30378575736 (attempt 1, job "core (Go)"), failing #704, which had only touched Lua drivers. The rerun passed in 1m19s.The goroutine dump from the failing run points at mochi-mqtt v2.7.9, not at our test logic. Two goroutines waiting on each other:
s.Close():Server.Close→closeListenerClients→Clients.GetByListener→Clients.Len→ blocked onRWMutex.RLockEstablishConnection→attachClient→Clients.Delete→ blocked onRWMutex.Lockclients.gois the reason:sync.RWMutexis not reentrant. A writer queued between the two read locks blocks the second one, and the writer waits for the first. Neither moves again.Server.ClosewalksGetByListener, and the broker runsClients.Deletethe moment a client drops, sodefer cli.Disconnect(100)followed bydefer s.Close()is the race. A narrow window, which is why it is rare and clears on rerun. v2.7.9 is the newest release, so there is nothing to upgrade to.The fix
Every shutdown now goes through
closeBroker, which waits for the client count to fall back to the inline client before closing. No disconnect is then in flight and no writer can queue between the two read locks. A 5-second deadline onCloseis the backstop: should the broker still wedge, the test says so in seconds rather than burning ten minutes on an unrelated pull request.The core Go job is capped at 120s per package for the same reason.
-timeoutis per package and the slowest takes about 25s, so the margin is wide and any future hang costs two minutes instead of ten.One more, spotted alongside:
tok.WaitTimeout(...) && tok.Error() != nillet a timed-out connect through, becauseWaitTimeoutreports whether the token finished in time. The test then ran against a client that never attached and failed later for a reason that read like a broker bug.connectClientchecks both.Verification
-timeout 120s, no package near the cap.-race: all green, no races, and the barrier converges at once — the "still holds N clients" line never appears.closeBrokerreported after 5.001s with a clear message and the binary exited normally despite the wedged goroutine. That file is not part of this change.Left out
go/test/e2e/stack_test.go:410closes its broker the same way, right afterShutdownAll()disconnects the driver's client. It is already bounded bymake e2e's-timeout 180s, so it costs three minutes rather than ten. Worth a follow-up rather than widening this change.🤖 Generated with Claude Code