Skip to content

fix(id-web): retire world socket on rejoin so its async close cannot spawn a ghost (#1461) - #1475

Merged
dezren39 merged 1 commit into
mainfrom
queue/1470-1461-world-ghost-ws
Oct 28, 2026
Merged

dezren39 merged 1 commit into
mainfrom
queue/1470-1461-world-ghost-ws

Conversation

@dezren39

@dezren39 dezren39 commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Fixes #1461.

Root cause (verified on main 7eca828)

In pkgs/id/web/src/world.ts, connect() registered all handlers with addEventListener, but the handlers read the shared mutable socket variable, and the join handler tried to retire the old socket with socket.onclose = null — a no-op, since no onclose handler was ever registered. It also reset intentionalClose in the same task, so when the old socket's close event arrived asynchronously it saw intentionalClose === false and scheduled a reconnect: a ghost third socket, a second join sent via the shared variable, and every broadcast frame appended twice.

Fix

  • connect() captures the new socket as ws; every handler (open/message/error/close) first checks ws === socket and ignores events from a socket that is no longer current. The join frame is sent on ws itself.
  • A genuine close clears the current reference; the reconnect timer additionally requires socket === null, so a manual rejoin during the backoff window cannot be doubled by the pending timer either.
  • The join handler retires the old socket before closing it (reference cleared first), so its async close event is treated as stale: no reconnect, no status overwrite. The onclose = null line is gone.

Genuine unexpected drops keep the existing backoff reconnect (500ms × 2^n, capped at 10s).

Tests

New pkgs/id/web/src/world-rejoin.test.ts with a FakeSocket whose close event dispatches asynchronously (the timing that hid the bug — the existing contract-test fake dispatches synchronously):

  • init → open → rejoin → old socket's async close → backoff elapse = exactly 2 sockets created, 1 join frame per socket, the retired socket's frames ignored, a broadcast chat line appended exactly once, status untouched by the retired close.
  • A genuine server drop still sets "Disconnected; retrying…" and auto-reconnects, joining once on the new socket.

CI on this PR is the verification for the web unit suite (CI-first; no local build run).

Queue task 1470 (Lane D worker shift).


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

@dezren39
dezren39 requested a review from a team as a code owner October 27, 2026 17:15
@coderabbitai

coderabbitai Bot commented Oct 27, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 35 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 6a6ff975-a8ad-47b2-9bc3-a2246419d264
Commits

Reviewing files that changed from the base of the PR and between 7eca828 and fc3031f.

Files selected for processing: 2
  • pkgs/id/web/src/world-rejoin.test.ts
  • pkgs/id/web/src/world.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T17:17:20.687579Z fc3031f 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.

@dezren39

Copy link
Copy Markdown
Member Author

🤖 Agent note · packet-premise correction (Lane BK, queue task 17645, 2026-11-22)

The dispatch packet for queue task 17645 named this issue — #1475 — and "design #1473" in this repo. Both numbers are wrong for the packet's title ("Hermetic-suite inventory + shared stat double + test-harness-conformance ratchet (design #1473, phase 2)"): this issue is the closed id-web world-socket fix, and code#1473 is a closed id/web innerHTML fix. The task's own dedupe key (repo-health:v1:triage:dezren39/darwin-nix:issue:1475) names the correct home: dezren39/darwin-nix#1475, under design dezren39/darwin-nix#1473 ("Same suite, same verdict — one hermetic harness for the repo's test suites"). The triage was performed there and its record is on darwin-nix#1475. This note exists so the next lane handed this packet does not re-triage the mismatch. No action is requested on this issue, which remains closed and complete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant