fix: Brotli stream corruption via concurrent writes to compressor#16
Merged
rschmukler merged 1 commit intomainfrom Feb 27, 2026
Merged
fix: Brotli stream corruption via concurrent writes to compressor#16rschmukler merged 1 commit intomainfrom
rschmukler merged 1 commit intomainfrom
Conversation
- **Introduced a per-tab “single writer” actor** (a dedicated virtual thread) that *owns*:
- the http-kit `AsyncChannel`
- the tab’s streaming Brotli state (`br-out` + `br-stream`, when compression is enabled)
- Changed `send-sse!` from “compress + `http/send!` right now” to **“enqueue message and return immediately”**:
- Messages go into a `LinkedBlockingQueue`
- The actor thread drains the queue and performs the actual compression + network writes sequentially
- Ensured the SSE response headers are sent **exactly once**:
- The actor’s *first* send writes the `{:headers ... :body ...}` map (and includes `Content-Encoding: br` when applicable)
- Subsequent sends are raw chunks on the same connection
- Made teardown safer:
- `unregister-sse-channel!` now **signals the actor to close via a sentinel**, so the actor closes the Brotli stream/channel on its own thread (avoiding “close while writing” races)
- Re-registering a tab now stops any previous writer first (reconnect safety)
- Updated/added tests to match and validate the new behavior:
- Tests now account for the async actor (latches/timeouts)
- Added coverage that `send-sse!` **doesn’t block on Brotli compression** and that Brotli compression is **never invoked concurrently** for a tab.
5b3ec01 to
085c802
Compare
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.
AsyncChannelbr-out+br-stream, when compression is enabled)send-sse!from “compress +http/send!right now” to “enqueue message and return immediately”:LinkedBlockingQueue{:headers ... :body ...}map (and includesContent-Encoding: brwhen applicable)unregister-sse-channel!now signals the actor to close via a sentinel, so the actor closes the Brotli stream/channel on its own thread (avoiding “close while writing” races)send-sse!doesn’t block on Brotli compression and that Brotli compression is never invoked concurrently for a tab.