Emit 'exit' immediately on EXIT byte (~38x exec speedup)#16
Open
jhgaylor wants to merge 1 commit into
Open
Conversation
In the non-TTY exec path, the server sends a single EXIT byte over the WebSocket and then holds the connection open ~5s before initiating its own close handshake. The current handler calls `this.close()` on EXIT and only emits the 'exit' event from `handleClose`, which fires when the WS actually closes — so every exec eats the server's ~5s wait before `execFile`'s promise resolves. The Python SDK had the same bug; the Elixir SDK does not — `sprites-ex` sends a CLOSE frame and immediately tears down the TCP, never waiting for the server's reciprocal CLOSE. Fix: emit 'exit' synchronously when the EXIT byte arrives, then issue the WS close as fire-and-forget. `handleClose` is guarded by `if (!this.done)` so it won't double-emit. In a side-by-side bench (5 cat /tmp/x calls against a warm sprite): before: ~5,300ms per exec after: ~140ms per exec (~38x speedup) This puts sprites-js on parity with the Python SDK after its equivalent fix (close_timeout=0, see superfly/sprites-py#26) and with the Elixir SDK. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
In the non-TTY exec path, the server sends a single
EXITbyte over the WebSocket and then holds the connection open ~5 seconds before initiating its own close handshake. The current handler callsthis.close()on EXIT and only emits the'exit'event fromhandleClose— which fires when the WS actually closes — so every exec eats the server's ~5s wait before `execFile`'s promise resolves.This is the same bug we just fixed in the Python SDK (superfly/sprites-py#26). The Elixir SDK (sprites-ex/lib/sprites/command.ex#L317-L324) does not have it — it sends a CLOSE frame and immediately tears down the TCP via `:gun.close`, never waiting for the server's reciprocal frame.
Fix
Emit `'exit'` synchronously when the EXIT byte arrives, then issue the WS close as fire-and-forget. `handleClose` is already guarded by `if (!this.done)` so it won't double-emit.
Impact
Side-by-side bench against a warm sprite running `cat /tmp/x` (n=2, p50):
That's a ~80× speedup on cold and ~43× on warm. The remaining ~75ms is mostly TLS handshake + WS upgrade + actual command execution, which puts `sprites-js` on parity with the other SDKs:
Behavior change
The `'exit'` event is now emitted before the WS finishes closing. Consumers that explicitly waited for the WS `close` event will see no change — `handleClose` is unchanged and still runs. Anything reading `exitCode`/the `exit` event keeps working but resolves ~5s sooner.
The CLOSE frame is still sent — the server sees a clean close — we just don't block the JS event loop waiting for the server's reciprocal CLOSE.