Skip to content

fix(ui): reset slug-connect latches on Leave so next game opens WS#159

Merged
slabgorb merged 2 commits intodevelopfrom
fix/lobby-ws-open-race
Apr 24, 2026
Merged

fix(ui): reset slug-connect latches on Leave so next game opens WS#159
slabgorb merged 2 commits intodevelopfrom
fix/lobby-ws-open-race

Conversation

@slabgorb
Copy link
Copy Markdown
Owner

Summary

Playtest 2026-04-24 BLOCKING bug: leaving a game and starting a fresh one (any genre) hung on the post-POST navigate. POST /api/games returned 201, URL flipped to /solo/, but the UI stuck on "The pages are turning..." indefinitely. Server logs showed no ws.connection_accepted, no mp.slug_connect — the client never opened the WebSocket for the new session.

Root cause

react-router-dom v6 reconciles <Routes> children by element type + position. Our routes /, /solo/:slug, and /play/:slug all render <LobbyRoot /> at the same reconciler slot, so React reuses the AppInner instance across navigate(). All refs survive route changes, including slugConnectFired.current which latches to true after the first session's successful fetch + connect. handleLeave reset autoReconnectAttempted, seenEventKeys, and sessionPhaseRef but missed slugConnectFired and justConnectedRef. On the second game's slug-connect effect run, the if (slugConnectFired.current) return; gate short-circuited — no fetch, no WebSocket, no recovery. Typing the URL manually "worked" only because it triggers a full page reload (remounts AppInner with ref defaults).

Not a race — a stale-ref session-lifecycle bug.

Fix

  • Move slugConnectFired ref declaration up to the other session-lifecycle refs so handleLeave can reset it alongside autoReconnectAttempted.
  • handleLeave now clears slugConnectFired.current, justConnectedRef.current, gameMetaError, and currentGenre so the next game's slug-connect effect starts from a clean slate.

Test plan

  • New wiring test src/__tests__/lobby-start-ws-open.test.tsx drives the exact playtest sequence (mount at /solo/first-slug, drive through connect → ready, click Leave, click Start again) and asserts a fresh WebSocket + SESSION_EVENT{connect, game_slug: second-slug} without any manual re-navigation.
  • Verified this test fails (5s timeout) when the fix is reverted — confirms the reproduction.
  • slug-routing.test.tsx and ConnectScreen.test.tsx still pass (38/38 in the three touched suites).
  • Full npx vitest run shows 45 pre-existing failures (unchanged) and 1116 passing — no new regressions.
  • Manual playtest: start a game, play a turn, click Leave, pick a new genre, click Start — WS must open and chargen must load on the first try.

🤖 Generated with Claude Code

slabgorb and others added 2 commits April 24, 2026 04:03
…e roll

After a combat roll resolved, the InlineDiceTray's TARGET banner and
"Rolled N vs M — Fail" badge stayed pinned through the next narration
cycle. When the next action buttons became clickable, the previous
roll's numbers were still on screen — players read the stale DC as
the target for their next click.

App.tsx now clears diceRequest and diceResult inside the NARRATION_END
branch (the same turn-boundary signal that re-enables input and clears
a resolved confrontation). The InlineDiceTray and DiceOverlay are
prop-driven, so both widgets return to a neutral resting state without
further changes — the 3D die stays idle until a new DICE_REQUEST
triggers the next throw.

Added a wiring test in dice-overlay-wiring-34-5.test.ts that asserts
both setters are invoked inside the NARRATION_END branch, plus a
regression guard that the overlay props still read from the cleared
state.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Playtest 2026-04-24 BLOCKING bug: leaving a game and starting a fresh one
(any genre) hung on the post-POST navigate. POST /api/games returned 201
with the new slug, URL flipped to /solo/<new-slug>, but the UI stuck on
"The pages are turning..." indefinitely. Server logs showed NO
ws.connection_accepted, NO mp.slug_connect, NO chargen.phase — the
client never even opened the WebSocket for the new session.

Root cause: react-router-dom v6 reconciles <Routes> children by element
type + position. Our routes "/", "/solo/:slug", and "/play/:slug" all
render <LobbyRoot /> at the same reconciler slot, so React reuses the
AppInner instance across navigate(). All refs survive route changes,
including slugConnectFired.current which latches to true after the first
session's successful fetch + connect. handleLeave reset several refs
(autoReconnectAttempted, seenEventKeys, sessionPhaseRef) but missed this
one and justConnectedRef. On the second game's slug-connect effect run,
the if (slugConnectFired.current) return; gate short-circuited — no
fetch, no WebSocket, no recovery. Typing the URL manually "worked" only
because it triggers a full page reload, which remounts AppInner with
ref defaults.

Not a race — a stale-ref session-lifecycle bug.

Fix:
- Move slugConnectFired ref declaration up to the other session-lifecycle
  refs so handleLeave can reset it alongside autoReconnectAttempted.
- handleLeave now clears slugConnectFired.current, justConnectedRef.current,
  gameMetaError, and currentGenre so the next game's slug-connect effect
  starts from a clean slate.

Wiring test: src/__tests__/lobby-start-ws-open.test.tsx drives the exact
playtest sequence — mount at /solo/first-slug, drive through connect →
ready, click Leave, click Start again. Asserts a fresh WebSocket connects
and SESSION_EVENT{connect, game_slug: second-slug} arrives without any
manual re-navigation. Verified this test FAILS (5s timeout waiting for
secondServer.connected) when the fix is reverted.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@slabgorb slabgorb merged commit f28de8c into develop Apr 24, 2026
1 check failed
@slabgorb slabgorb deleted the fix/lobby-ws-open-race branch April 24, 2026 09:32
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