fix(state.js): stop the event loop spinning while the socket is down

Closed
FA
9d ago
+81-5

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

Description

closes #7774

The freeze. Two places looped while (event_queue.length > 0) await processEvent(...): the main event loop effect in useEventLoop and the connect handler's drain.

  • While a backend event waits for a disconnected socket, processEvent returns without dispatching. connect()/reconnect() resolve synchronously.
  • Suppose an iteration is awaiting real I/O when the socket drops: an rx.call_script / rx.call_function whose callback awaits a promise. The loop then runs on already-resolved promises forever. The tab freezes, and socket.io never gets a turn to reconnect.

The fix. processEvent already drains the queue through its finally recursion and stops early only while the socket is down. The connect handler drains the queue on reconnect. So the outer loops could only finish or spin, and each is now a single processEvent call.

  • queueEvents is the only path that adds events, and it always calls processEvent, so nothing is left behind.
  • The processEvent doc comment now states this contract. It replaces a stale "not already processing" comment that described a guard that no longer exists.

Listener leak. The visibilitychange listener that connect() adds now removes itself once its socket is no longer socket.current. Before, its !socket.current → connect() branch could only run after the event loop unmounted, where it built a socket that no loop processed events for. socket.current is set to null only in the unmount cleanup.

Not changed: the effect loop, queueEvents and the connect drain can still dispatch concurrently, so an awaited client event can be overtaken by the next queued event. A guard would change dispatch ordering, so it is left for a separate discussion.

Tests

  • New Playwright test: test_connection_banner.py::test_reconnect_after_drop_during_awaited_script. It extends the existing connection-banner app.
    1. While offline, queue [backend event, call_script(pending promise, callback)].
    2. Reconnect, so the drain waits on the pending promise.
    3. Drop the connection again and resolve the promise.
    4. Assert the callback's result arrives after reconnecting.
  • On main the tab freezes. A bounded liveness probe makes the test fail in about 15 s instead of hanging in a Playwright call without a timeout.
  • Full tests/integration/tests_playwright suite with this change: 560 passed, 3 skipped. biome (the pinned 2.4.8) and ruff are clean.

https://claude.ai/code/session_01LwJUCTHFBNvyeCk38wALTX


Generated by Claude Code