Skip to content

Replace a reconnect attempt the page hid on when it comes back - #528

Merged
masnwilliams merged 3 commits into
mainfrom
hypeship/reconnect-wake-while-connecting
Nov 25, 2026
Merged

masnwilliams merged 3 commits into
mainfrom
hypeship/reconnect-wake-while-connecting

Conversation

@masnwilliams

@masnwilliams masnwilliams commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes a stall in the live view reconnect from #524. If the page hid after a reconnect attempt had already opened its websocket, coming back did nothing: wake() only acts while waiting, so the page stayed on "Reconnecting..." until the suspended attempt hit its connection or media timeout.

  • The live view client now tells the reconnector when the page hides (hid()). If that happens after the current attempt called connect(), the attempt is marked stale.
  • On wake, a stale attempt is closed through a new abort dependency: the client's disconnect(), which clears the socket and peer handlers first so no drop is reported, followed by the same store reset a drop does, so any tracks the closed attempt received don't linger. A fresh attempt then starts at once, with no backoff and no failure counted. The new attempt only connects after its page check resolves, so two peer connections never overlap.
  • A wake without a hide (online, or pageshow while visible) still leaves a running attempt alone. So does a hide during the page check, which already went back to waiting.

Paused video after replacing an attempt

If the aborted attempt had already received its track, the reconnected video could stay paused while the store said it was playing, so no tap prompt showed. Two things in video.vue combined:

  • The store reset leaves stream undefined, and onStreamChanged returned early, so the <video> element kept the closed attempt's stream. That stream's canplaythrough then set playable and playing back to true.
  • Swapping srcObject to the new stream pauses the element without a pause event. The new stream's canplaythrough called the store's play(), but playing was already true, so the watcher never ran video.play().

Now onStreamChanged clears srcObject when the stream goes away, and marks the store unplayable before attaching a new stream. The new stream's canplaythrough then moves playing from false to true, which plays the element, or shows the tap prompt if the browser refuses. This also covers main's existing path, where an attempt drops during the media stage and the next one connects.

Testing

  • bun test tests: 306 pass. New tests:

    • hide during a connecting attempt, then wake: the old attempt is aborted and a new one connects at once. Fails on main.
    • hide before connect() ran: no abort, one connect.
    • the client's abort closes the socket without reporting a drop and resets the video store. The fake socket runs its onclose on close(), so the test fails if abort is a bare close(), and also fails without the reset.
    • video.vue: a new stream after an aborted attempt that had its own stream plays, and so does a stream that replaces one already playing. Both fail on c2bffc9.
  • Real Chromium against a production headful browser with this client hot-patched (desktop viewer, page hidden for 2s right after the attempt received its tracks; UDP and the TURN relay blocked so ICE couldn't connect, unblocked before showing):

    Client Runs that reached the abort Element after reconnect
    c2bffc9 8 paused in all 8, store playing, no prompt
    this branch 6 playing in all 6

    Runs where no TURN relay was in use connected ICE anyway and never reached the abort; they aren't counted. Not tested: a real phone, Safari, Firefox.

  • tsc --noEmit and prettier are clean.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit cb77eab. Configure here.

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

Approving. I built main and this branch locally and ran both against a real Chromium with real media.

Socket held after dial, tab hidden 2s, then shown: main took 14.0s to reconnect (transport timeout plus backoff), this branch took about 0.1s with no failure counted and the video playing. So the fix does what it says in Chromium. I have not tested a real phone lock, Safari or Firefox.

One bug to fix before the next image release. If the aborted attempt already received its track (ws open, signal/provide done, ICE not connected), the reconnect succeeds but the video element ends up paused while the store says playing, so no prompt shows. I hit it 8 of 8 times with UDP blocked during the attempt. On desktop it stayed paused through a 4s wait and another hide and show. I did not try other ways to recover it. With touch emulation the next tap or hide and show restarted it.

My read of the trace: right after the peer is closed, canplaythrough fires on the element still holding the old stream and the autoplay handler sets playing back to true after the store reset. The new stream's srcObject swap then leaves the element paused with no pause event, and the new stream's canplaythrough calls a store play() that is now a no-op. The first step is inferred from timing, I did not isolate it.

This looks older than this PR. On main I saw the same freeze once when an attempt dropped in the media stage and the next one connected. But abort adds a second way in, and in the one run where the tab was hidden during a stalled media stage, main let the old attempt finish and played fine where this branch froze. Resetting playable when a new stream is attached, or clearing srcObject when the store resets, look like the places to start. I have not tested a fix.

Smaller notes:

  1. In reconnect-abort.test.ts the dropped assertion is vacuous because the fake socket never fires onclose. I replaced abort with a bare close() and the test still passed.
  2. The dialed = false in dropped() looks redundant since attempt() resets it.

Also not covered: the live view proxy. The test went straight to neko in a local container.

@masnwilliams
masnwilliams merged commit 955d9b3 into main Nov 25, 2026
12 checks passed
@masnwilliams
masnwilliams deleted the hypeship/reconnect-wake-while-connecting branch November 25, 2026 19:25
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.

2 participants