Skip to content

Fix native P2P startup adapter race#6614

Merged
matthewevans merged 1 commit into
mainfrom
ship/fix-native-p2p-startup-adapter-race
Jul 25, 2026
Merged

Fix native P2P startup adapter race#6614
matthewevans merged 1 commit into
mainfrom
ship/fix-native-p2p-startup-adapter-race

Conversation

@matthewevans

@matthewevans matthewevans commented Jul 25, 2026

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Bug Fixes

    • Improved game initialization reliability by registering the connection before receiving initial remote updates.
    • Failed initialization attempts now clean up the connection state, preventing stale or unusable connections from being retained.
  • Tests

    • Added coverage confirming correct connection setup order during game initialization.

@matthewevans
matthewevans enabled auto-merge July 25, 2026 00:09
@coderabbitai

coderabbitai Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

initGame now stores the adapter before initialization and removes it if initialization fails. A test verifies the adapter is available in the store while initializeGame runs.

Changes

Game store initialization

Layer / File(s) Summary
Bind adapter before initialization
client/src/stores/gameStore.ts, client/src/stores/__tests__/gameStore.test.ts
initGame binds the adapter before calling initializeGame, clears the matching adapter after an initialization error, and rethrows the error. The test mocks initialization to verify the adapter is already stored.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: fixing the startup adapter race in native P2P initialization.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ship/fix-native-p2p-startup-adapter-race

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@client/src/stores/gameStore.ts`:
- Around line 418-433: The initialization transaction around
adapter.initializeGame and the first getSnapshot/commit must roll back all
matching session state on any failure, not just clear adapter. Extend the
try/catch through the initial snapshot and commit, and on failure remove the
adapter plus restore or clear any game state published by this initialization,
while preserving unrelated newer session updates by checking the adapter/session
identity before rollback; ensure async state updates during initialization
cannot leave partial committed state.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 2d131170-a8d7-4dde-8d03-35d197d1c975

📥 Commits

Reviewing files that changed from the base of the PR and between ee189aa and 07b3057.

📒 Files selected for processing (2)
  • client/src/stores/__tests__/gameStore.test.ts
  • client/src/stores/gameStore.ts

Comment on lines +418 to +433
set({ adapter });
let initResult;
try {
initResult = await adapter.initializeGame(
deckData,
formatConfig,
playerCount,
matchConfig,
firstPlayer,
);
} catch (error) {
// A failed initialization must not leave a transport that never
// produced a playable game attached to the store.
if (get().adapter === adapter) set({ adapter: null });
throw error;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift

Make initialization cleanup transactional.

The catch only clears the adapter. If initializeGame publishes a snapshot and then rejects, committed game state can remain while adapter becomes null. Conversely, if getSnapshot() fails at Line 437, the adapter remains attached despite initialization failure. Extend the failure scope through the first successful snapshot/commit and roll back the matching session state, or stage remote commits until initialization succeeds.

As per path instructions, async races and state updates during initialization must be checked.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@client/src/stores/gameStore.ts` around lines 418 - 433, The initialization
transaction around adapter.initializeGame and the first getSnapshot/commit must
roll back all matching session state on any failure, not just clear adapter.
Extend the try/catch through the initial snapshot and commit, and on failure
remove the adapter plus restore or clear any game state published by this
initialization, while preserving unrelated newer session updates by checking the
adapter/session identity before rollback; ensure async state updates during
initialization cannot leave partial committed state.

Source: Path instructions

@matthewevans
matthewevans added this pull request to the merge queue Jul 25, 2026
Merged via the queue into main with commit 8b39ff1 Jul 25, 2026
15 checks passed
@matthewevans
matthewevans deleted the ship/fix-native-p2p-startup-adapter-race branch July 25, 2026 00:46
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