schedule twisted websocket writes onto the reactor thread - #49
Merged
Merged
Conversation
The websocket listener runs in a threadpool thread, but wrote to the transport directly. On TLS connections, this races with the reactor processing incoming TLS records on the same OpenSSL connection, which breaks the connection (`OpenSSL.SSL.Error: []`) or corrupts frames. On TLS connections, `send` is now scheduled onto the reactor without waiting for it, and `accept`, `reject` and `close` wait for their completion. Plain connections keep writing directly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
bentsku
marked this pull request as draft
September 30, 2026 21:09
…ns too Writing to the transport from the listener thread also breaks plain connections: it races with the reactor flushing the same transport, which loses and reorders data once the messages exceed the socket buffers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
bentsku
added this pull request to stack #51
September 30, 2026 21:42
bentsku
marked this pull request as ready for review
September 30, 2026 21:44
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.
Motivation
The twisted websocket listener runs in a threadpool thread, but
TwistedWebSocketAdaptercalled intoWebSocketChanneldirectly, soaccept,send,rejectandclosewrote to the transport (and changed thewsprotostate) from that thread. Twisted is not thread-safe, and this breaks connections in two ways:Connection to remote host was lostwhile reading the upgrade response. This shows up as a flakywss://test in LocalStack's CI.main(for example, message 2505 arrived with the contents of message 5240).Changes
TwistedWebSocketAdapterschedules its operations onto the reactor thread:sendusesreactor.callFromThreadand doesn't wait for the write. Order is kept, since the reactor runs queued calls in order.accept,rejectandcloseuseblockingCallFromThread, the same waytwisted.web.wsgiwrites responses.rejectconsumes the body in the listener thread, so the reactor doesn't run arbitrary iterators.WSGIResource, and calls directly when it's already on the reactor thread or no reactor is running.twisted[tls]to thedevextra for the new TLS test.Testing
test_websocket_tls_send_and_close_from_listener_threadserves a listener over TLS and runs 200 accept → send → close cycles. It fails every run onmainwith the error above.test_send_many_messages(twisted and asgi) streams 10k × 1 KB messages over 3 connections and checks each one arrives intact and in order. It fails every run onmainfor twisted on Linux; asgi passes on both.Benchmark
Linux (
python:3.13-slim,EPollReactor), 32-byte text messages, median of 5 runs.send()is 10k messages streamed from the listener; a cycle is connect → accept → send → close.wssend()wsssend()wscyclewsscyclemainSSL record layer failure)send(blockingCallFromThread)main's plain throughput comes from skipping the thread handoff, which is what loses data. Among the correct variants, not waiting forsendis about 4× faster than waiting.On macOS (
SelectReactor),mainis much slower: about 300 msg/s overwss://, and the plainws://benchmark hung. Writes from another thread don't wake the reactor there. With this PR, both reach about 72k msg/s, with cycles of 0.66 ms (ws) and 1.13 ms (wss).Benchmark script (
python bench_ws.py [--tls])🤖 Generated with Claude Code