Fix attach session race that drops live frames#25
Open
jhgaylor wants to merge 1 commit into
Open
Conversation
WSCommand.start() was creating the _run_io background task BEFORE calling _wait_for_session_info(). Both coroutines iterate `async for message in self.ws`, but the websockets library does not support concurrent reads on a single connection — messages are split between the two coroutines unpredictably. _wait_for_session_info silently drops any binary frames (stdout/stderr/exit) it consumes while waiting for the session_info text frame, so attach clients reliably receive the replayed scrollback but lose subsequent live output. Reorder so _wait_for_session_info runs to completion before the _run_io task is scheduled. There is then only ever one reader on the WebSocket at a time, and live binary frames are delivered in full to attached clients. Verified against a live sprite: a second client attached via sprite.attach_session(id) now receives both the replay and the ongoing tick stream in lockstep with the original exec connection, where previously it only received the replay.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
WSCommand.start()creates the_run_iobackground task before calling_wait_for_session_info()on attach. Both coroutines then iterateasync for message in self.wsagainst the same WebSocket. Thewebsocketslibrary does not support concurrent reads on a single connection — messages get split between the two coroutines unpredictably, and_wait_for_session_infosilently drops any binary frames it happens to consume while waiting for thesession_infotext frame.The visible symptom: a client that calls
sprite.attach_session(id)reliably receives the replayed scrollback but loses subsequent live stdout/stderr/exit frames.Fix
Drain
_wait_for_session_infoto completion before scheduling the_run_iotask. There is then only ever one reader on the WebSocket at a time, and live binary frames are delivered in full.Reproduction
Against a live sprite running
bash -c 'for i in $(seq 1 30); do echo tick=$i; sleep 1; done':Before — second client opened via
sprite.attach_session(id)sees the replay and then nothing:After — same setup, second client sees replay AND live frames in lockstep with the original exec:
A raw-WebSocket probe (bypassing the SDK) confirmed the server has always been streaming live frames to attach connections — the bug was purely client-side.
Test plan
tests/still pass (pytest tests/)sprite.attach_session(id)from the patched sourcewebsocketsasync iterator semantics would balloon scope; happy to add one if you'd like