Repository navigation
test(mqtt): stop the test broker listening before it drops its clients - #2178
Merged
Merged
Conversation
TestLinkFollowsABrokerThatGoesAndComesBack failed in CI on #2176: the link stayed "connection lost" for the full 15 s wait. mochi's Server.Close drops its clients and only then closes the listener. paho tries again at once after a loss, without the 1 s pause, and when the scheduler pauses mochi's Close goroutine between those two steps, the listener accepts paho's new connection. mochi's Serve loop sees it is ending and neither serves nor closes that connection, so paho waits its 30 s ConnectTimeout for a CONNACK. A broker process that exits closes every socket, so a real box does not meet this. The test broker now owns its listener and closes it before mochi drops the clients, as a broker that exits does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
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.
Problem
TestLinkFollowsABrokerThatGoesAndComesBackfailed incore (Go)on #2176, a pin PR that does not touch MQTT (job log):A flaky test in the one required check blocks every merge.
Cause
mochi's
Server.Closedrops its clients and only then closes the listener (listeners.TCP.Close:closeClients, thenlisten.Close). paho's first try after a loss skips the 1 s pause, so it dials at once. If the scheduler pauses mochi's Close goroutine between those two steps, the listener accepts paho's new connection. mochi'sServeloop sees that it is ending and neither serves nor closes that connection. paho has sent CONNECT on an open socket and waits its fullConnectTimeout(paho's default, 30 s) for a CONNACK. The test gives up after 15 s.The CI log fits: after
connection lostthe link never showsconnection refusedor any other reason. In a normal run, paho's first try meets the closed port and the link readsconnection lost, thenconnection refused, then up.How it was shown
A throwaway test (not in this PR) ran the same steps on mochi's
listeners.Net, which has the sameServeandClosecode, over a spy listener that records each accepted connection and whether anyone read or closed it.GOMAXPROCS=4under CPU load: no hang, no dropped connection. The gap is a few microseconds wide.Close, standing in for the scheduler pausing mochi between the two steps: each run had one connection accepted 0.3–0.5 ms afterClosebegan, never read or closed. Reconnect took 30.5 s each time, and the link wentconnection lost, thenunreachableat 30 s, then up.Fix
The test broker now owns its listener (
listeners.NewNet) and closes it before mochi drops the clients, as a broker process that exits does. paho's first try then meets a closed port, and the next try reaches the new broker. The listener now opens instartBroker, so the dial loop that waited for it to start is gone.The client is unchanged. A real broker that stops is a process that exits, and the kernel closes all its sockets, so paho gets a reset at once rather than a silent socket. paho still waits up to 30 s for a broker that accepts a connection and never answers. That is a separate question, and this PR does not change it.
No changeset: only a test changes, so the PR carries the
no-changesetlabel, as #2085, also a test fix, did.Evidence
All local, on this branch:
go test -race -run TestLinkFollowsABrokerThatGoesAndComesBack -count=200 ./internal/mqtt/: 200 of 200 pass.GOMAXPROCS=4 go test -run TestLinkFollowsABrokerThatGoesAndComesBack -count=100with ten busy processes on the side: 100 of 100 pass.go test -race -count=20 ./internal/mqtt/: pass.make verify: clean.🤖 Generated with Claude Code