Skip to content

test(mqtt): stop the test broker listening before it drops its clients - #2178

Merged
frahlg merged 1 commit into
masterfrom
mqtt-link-reconnect-flake
Dec 5, 2026
Merged

frahlg merged 1 commit into
masterfrom
mqtt-link-reconnect-flake

Conversation

@frahlg

@frahlg frahlg commented Dec 4, 2026

Copy link
Copy Markdown
Member

Problem

TestLinkFollowsABrokerThatGoesAndComesBack failed in core (Go) on #2176, a pin PR that does not touch MQTT (job log):

client_link_test.go:199: link never became connected again; last {... Connected:false ... Reason:connection lost}

A flaky test in the one required check blocks every merge.

Cause

mochi's Server.Close drops its clients and only then closes the listener (listeners.TCP.Close: closeClients, then listen.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's Serve loop sees that it is ending and neither serves nor closes that connection. paho has sent CONNECT on an open socket and waits its full ConnectTimeout (paho's default, 30 s) for a CONNACK. The test gives up after 15 s.

The CI log fits: after connection lost the link never shows connection refused or any other reason. In a normal run, paho's first try meets the closed port and the link reads connection lost, then connection 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 same Serve and Close code, over a spy listener that records each accepted connection and whether anyone read or closed it.

  • 300 runs at rest and 200 runs with GOMAXPROCS=4 under CPU load: no hang, no dropped connection. The gap is a few microseconds wide.
  • With a 20 ms pause in the listener's Close, standing in for the scheduler pausing mochi between the two steps: each run had one connection accepted 0.3–0.5 ms after Close began, never read or closed. Reconnect took 30.5 s each time, and the link went connection lost, then unreachable at 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 in startBroker, 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-changeset label, as #2085, also a test fix, did.

Evidence

All local, on this branch:

  • With the same 20 ms pause injected into the fixed broker: 60 of 60 runs pass in under 1 s.
  • go test -race -run TestLinkFollowsABrokerThatGoesAndComesBack -count=200 ./internal/mqtt/: 200 of 200 pass.
  • GOMAXPROCS=4 go test -run TestLinkFollowsABrokerThatGoesAndComesBack -count=100 with 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

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>
@frahlg frahlg added the no-changeset PR intentionally exempt from the changeset requirement (dev tooling / non-shipping) label Dec 4, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-04T18:47:35.832238Z e41e964 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@frahlg
frahlg merged commit a3b29b9 into master Dec 5, 2026
21 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-changeset PR intentionally exempt from the changeset requirement (dev tooling / non-shipping)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant