Repository navigation
rta: Replace WebSocket connections that stop answering pings - #54
Open
HashimTheArab wants to merge 17 commits into
Open
HashimTheArab wants to merge 17 commits into
HashimTheArab wants to merge 17 commits into
Conversation
A socket whose network path fails without a reset stays open but delivers nothing, so read blocks forever and no reconnect runs. MPSD treats session members that use its connection ID as offline: it drops their activity handles and rejects new ones with 403 until the process makes a new Conn. Each socket is now pinged every 30 s. A ping not answered within 15 s closes the socket, and the existing reconnect path redials, resubscribes, and lets MPSD sessions move to the new connection ID.
…cribing A socket that failed while a reconnect still held the gate started nothing, so a replacement lost during a slow HandleSubscribe stayed current with no reader. The loss is now recorded against that socket, and the reconnect runs again before it releases the gate. A write failure on a socket that a reconnect already replaced no longer starts another reconnect.
connectionLost now checks the current socket under reconnectMu, so a stalled loss report for an older socket cannot overwrite the loss of its replacement. probe closes a socket on every failed ping, which also frees the reader of a socket that a reconnect replaced without closing.
- Subscribe tracks a new subscription only if its socket is still current and not lost. A socket lost while HandleSubscribe ran used to leave the subscription active but untracked, so no reconnect restored it. - A resubscribe that times out on a new socket now counts as interrupted: the socket is dropped and the reconnect tries again, instead of removing the subscription for good. - connectionLost claims the reconnect gate under the same lock as the loss record, so a late report cannot start a redundant reconnect. - A reconnect closes the socket it replaces, so its reader does not leak.
An interrupted resubscribe was tracked again under its ID from the old socket. RTA numbers subscriptions per socket, so a subscription that succeeded on the new socket could take the same ID and one of them dropped out of the map, active but never restored again. The reconnect now holds interrupted subscriptions itself until a later attempt restores them. A resubscribe timeout now counts as a silent socket only if a ping also fails. On a socket that still answers, the ignored subscription fails as before, instead of the reconnect retrying forever with the gate held. Also remove startReconnect, which has no callers left.
finishReconnect cleared the loss record before running the reconnect again, so a subscription whose callback returned during the next dial was tracked on the lost socket and never restored. The record now stays until the next attempt replaces that socket, and a socket that restore drops is recorded before it is closed. HandleSubscribe now documents that it must not call Subscribe or Unsubscribe on the same Conn. Tests get timing margins for slow runners, and the healthy-socket control now requires pongs.
…ounds - A resubscribe skips a subscription that Unsubscribe removed while the reconnect held it, so it is not brought back after Unsubscribe succeeded. - Handshakes that time out together share one liveness ping, because a peer may answer only the latest of several outstanding pings. - When each new socket is lost right after its resubscribes succeed, the next reconnect round waits on a backoff schedule instead of redialing at once. - trackOn refuses to track after Close has cancelled the Conn, so Close's sweep cannot miss a subscription being registered. - probe does not log a missed pong when read already failed.
- probe and the resubscribe liveness check share one ping at a time per Conn, since a peer may answer only the latest outstanding ping. - Unsubscribe checks for a reconnect again after taking its locks. One that started after its wait could hold the subscription for a retry while another reuses its old ID, so Unsubscribe would remove the wrong one. - untrackSubscription deletes an ID only if it still maps to that subscription. - ensureWebSocket checks for Close again under connMu, so a lazy dial cannot install a socket after Close. - Tests release blocked callbacks on failure, and the backoff test waits for the loss instead of sleeping.
Unsubscribe checked for a reconnect under its locks but picked the socket later, so a reconnect in between could make it send the old ID on a new socket where another subscription uses it. It now sends on the socket seen under the locks and treats a replaced or missing socket as interrupted. The cleanup unsubscribe after a failed HandleSubscribe uses the socket that subscribe used.
When every socket closed right after acknowledging a first subscribe, Subscribe redialed at once while holding subscriptionOpMu. It now waits on a backoff schedule between those retries. HandleSubscribe documents that it must not wait on a lock held by a caller of Subscribe or Unsubscribe. TestUnsubscribedWhileHeldIsNotRestored rejects dials by attempt number, so it no longer depends on the test goroutine winning a race.
Keep only the ping probe that closes a silent socket so the existing reconnect replaces it, and its two tests. The reconnect races found in review are older than this change and belong in a separate change that gives each socket its own subscription state.
HashimTheArab
added a commit
to HashimTheArab/go-xsapi
that referenced
this pull request
Nov 18, 2026
Carry the reduced df-mc#54: only the ping probe and its two tests.
A redial had no deadline, so a stalled upgrade blocked the only reconnect. A resubscribe on a silent socket timed out before the first ping, which removed the subscription for good. Bound each redial, ping new sockets at once, and let a resubscribe outlast the probe so silence interrupts it.
Pinging at once and a longer resubscribe timeout made silent sockets reach the old reconnect races. Without them, a silent replacement ends its subscriptions with HandleError, so callers can subscribe again.
Tests now shorten the dialer's own fields, so the package variables that mirrored them are gone.
A resubscribe that times out removes the subscription for good, so a stall that also silenced the replacement ended every subscription. Drop a silent replacement before any subscription moves to it, and redial.
This was referenced Nov 23, 2026
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
An RTA WebSocket can stay open while delivering nothing. This happens when its network path fails without a reset.
readblocks inwsjson.Readforever, no reconnect starts, and the subscription staysActive. As a result,mpsd.Client.subscribekeeps returning the oldConnectionId.MPSD checks a member's activity through that RTA connection. When the RTA connection dies on the service side, MPSD drops the member's activity handle. Every later
Publishon the samexsapi.Clientcreates the session (201) with the dead connection ID, and thenPOST /handlesfails with 403 Forbidden. Only a newxsapi.Clientrecovers, because it opens a new socket with a new connection ID.Evidence
This comes from a production host that keeps one
xsapi.Clientfor its whole lifetime and publishes one MPSD session (all times UTC):ConnectionId.handles/query, session PUT).POST /handles403. All of them use the cachedConnectionId.xsapi.Client), the first publish succeeds, 7 s after the last 403.The 403 body was not captured, because the error helper discards it. So the link between the stale connection and the 403 is inferred, not proven. It matches the active-member rules. The silent socket is shown directly by the log.
Change
readstarts a probe for each socket. It pings every 30 s. If no pong arrives within 15 s, it callsCloseNow, which wakes the blocked reader. The existing reconnect path then dials and subscribes again, andmpsdmoves tracked sessions to the new connection ID inHandleSubscribe. A socket that answers pings is not replaced by the probe, so quiet subscriptions are kept.Close.Live check
I ran
1d69d6bagainstrta.xboxlive.comwith one account:sessiondirectory, 215 s with the probe runningThis did not publish a session, so the move of an MPSD activity to the new ID was not checked live.
Tests
TestSilentWebSocketIsReplacedsilent socket was not replacedTestStalledReplacementIsRetried/upgrade_never_finishesstalled replacement was not retriedTestStalledReplacementIsRetried/silent_after_upgradedial count = 2, want 3TestRespondingWebSocketIsKeptgo test -race -count=3 ./..., 40 race runs of these tests, 10 race runs of thertapackage at-cpu=1,4, andmake lintpass.I also simulated outages of 20 to 300 s with the production timings and two subscriptions (
testing/synctest, not part of this PR). The server stayed silent, stalled upgrades, or refused them. Both subscriptions received events again after all 27 outages. Without the ping before resubscribing, both were lost in every silent outage of 40 s or more.Not in this PR
Older reconnect races. Subscription IDs belong to one socket, but
Connkeeps one ID map, one sequence set and one gate for all sockets. I think one change that gives each socket its own session would be cleaner than fixing these one by one:HandleSubscribeleaves the subscription active but untracked.Unsubscribeduring a reconnect can send an old ID on a newer socket.An older draft with regression tests for most of these is on
rta/reconnect-hardening-ref. I can open an issue to agree on the design first.A forced
Conn.Reconnect(ctx), for a service-side registration loss while pongs still arrive.While a reconnect is still dialing,
mpsd.Publishcan still read the old connection ID.Keeping the response body in unexpected-status errors, so a 403 like this can be explained.