Skip to content

rta: Replace WebSocket connections that stop answering pings - #54

Open
HashimTheArab wants to merge 17 commits into
df-mc:mainfrom
HashimTheArab:rta/ping-liveness
Open

HashimTheArab wants to merge 17 commits into
df-mc:mainfrom
HashimTheArab:rta/ping-liveness

Conversation

@HashimTheArab

@HashimTheArab HashimTheArab commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Problem

An RTA WebSocket can stay open while delivering nothing. This happens when its network path fails without a reset. read blocks in wsjson.Read forever, no reconnect starts, and the subscription stays Active. As a result, mpsd.Client.subscribe keeps returning the old ConnectionId.

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 Publish on the same xsapi.Client creates the session (201) with the dead connection ID, and then POST /handles fails with 403 Forbidden. Only a new xsapi.Client recovers, because it opens a new socket with a new connection ID.

Evidence

This comes from a production host that keeps one xsapi.Client for its whole lifetime and publishes one MPSD session (all times UTC):

Time Event
16:26:58 The RTA socket is reset by the peer. It reconnects and gets a new ConnectionId.
until 18:02:27 RTA session events arrive every ~3 s.
18:02:27 → 18:08:14 No RTA frames arrive. No read error and no reconnect are logged.
18:02:28–18:02:48 MPSD GETs time out. By ~18:05, plain HTTP calls work again (presence, handles/query, session PUT).
18:05:33 The host's activity handle is gone, so the host publishes a new session.
18:05–18:08 6 attempts. Each gives a session PUT 201, then POST /handles 403. All of them use the cached ConnectionId.
18:08:21 After a process restart (new 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

  • Probe: read starts a probe for each socket. It pings every 30 s. If no pong arrives within 15 s, it calls CloseNow, which wakes the blocked reader. The existing reconnect path then dials and subscribes again, and mpsd moves tracked sessions to the new connection ID in HandleSubscribe. A socket that answers pings is not replaced by the probe, so quiet subscriptions are kept.
  • Redial deadline: each reconnect dial now has 30 s. Before, one upgrade that never finished blocked the only reconnect until Close.
  • Ping before resubscribing: a replacement must answer one ping before subscriptions move to it. If it does not, it is dropped and dialed again. Otherwise a stall that also silences the replacement would end every subscription, because a resubscribe that times out is terminal.

Live check

I ran 1d69d6b against rta.xboxlive.com with one account:

Check Result
Raw pings before any subscribe, and after up to 60 s idle 7 of 7 answered, in 236–289 ms
Subscribed to sessiondirectory, 215 s with the probe running 1 dial, subscription active
Connection frozen on the client side: reads block, writes are dropped, no error Replaced and subscribed again after 41 s, with a new connection ID. 2 dials in total, still 2 after 50 s more

This did not publish a session, so the move of an MPSD activity to the new ID was not checked live.

Tests

Test Case Failure without the fix
TestSilentWebSocketIsReplaced The incident. Socket 1 stays open but never answers pings. silent socket was not replaced
TestStalledReplacementIsRetried/upgrade_never_finishes The redial's upgrade never finishes. stalled replacement was not retried
TestStalledReplacementIsRetried/silent_after_upgrade The replacement does not answer pings. dial count = 2, want 3
TestRespondingWebSocketIsKept A socket that answers at least 5 pings is not replaced. (control)

go test -race -count=3 ./..., 40 race runs of these tests, 10 race runs of the rta package at -cpu=1,4, and make lint pass.

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 Conn keeps 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:

    • A replacement lost while its resubscribe callback runs stays current with no reader.
    • A socket lost during the first HandleSubscribe leaves the subscription active but untracked.
    • A replacement that goes silent between its ping and the resubscribe reply still ends the subscription after 15 s.
    • An interrupted resubscribe is put back under its old ID, which can overwrite one that just succeeded on the new socket.
    • Unsubscribe during a reconnect can send an old ID on a newer socket.
    • A subscribe ACK that arrives after the caller's deadline leaves a registration on the server. Events are routed by numeric ID alone.

    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.Publish can still read the old connection ID.

  • Keeping the response body in unexpected-status errors, so a 403 like this can be explained.

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.
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.

1 participant