fix(proxy): add backpressure handling to prevent hang on large responses#16
Open
nik-localstack wants to merge 2 commits into
Open
fix(proxy): add backpressure handling to prevent hang on large responses#16nik-localstack wants to merge 2 commits into
nik-localstack wants to merge 2 commits into
Conversation
When forwarding large responses, the proxy's send to the destination socket can block: the receiver's TCP buffer fills up, which fills the proxy's send buffer, which stalls the sender via flow control. With no EVENT_WRITE handling the proxy had no way to retry the stalled send, causing a deadlock for responses larger than the TCP send buffer (~400KB on macOS, ~256KB on Linux). Catch BlockingIOError explicitly (before the generic OSError handler) and register the socket for EVENT_WRITE so the selector retries the flush when buffer space becomes available. Also add return guards after connection close in the EVENT_READ path to prevent fall-through into the now-stale redirect_conn state. Add test_various_payload_sizes covering 1B, 1KB, 100KB, 1MB, 10MB and 10k/100k rows, over both plain and SSL connections, to catch regressions. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
ssl.SSLSocket.send() raises ssl.SSLWantWriteError (not BlockingIOError) when the underlying TCP buffer is full on a non-blocking SSL socket. SSLWantWriteError is a subclass of OSError, so it was caught by the generic connection-close handler, closing the connection mid-response. The client socket stayed open, leaving the caller hanging indefinitely. Catch SSLWantWriteError alongside BlockingIOError in both send paths so SSL connections correctly register EVENT_WRITE and retry when buffer space becomes available. Co-Authored-By: Claude Sonnet 4.6 <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
EVENT_WRITEhandling the proxy had no way to retry the stalled send.BlockingIOErrorandssl.SSLWantWriteError(both subclasses ofOSError) were being swallowed by the generic connection-close handler, tearing down the connection instead of retrying.SSLWantWriteErroris raised by non-blocking SSL sockets when the underlying TCP buffer is full — distinct fromBlockingIOErroron plain sockets.Changes
proxy.pyBlockingIOError/ssl.SSLWantWriteErrorexplicitly beforeOSErrorin both send paths; registerEVENT_WRITEon the destination socket so the selector retries the flush when buffer space is available.EVENT_WRITEhandler onconnitself to drain any backloggedout_bytesafter a previous partial-send stall.returnguards after connection close in theEVENT_READpath to prevent fall-through into staleredirect_connstate (double-unregister / use-after-close).tests/test_proxy.pytest_various_payload_sizesparametrized over 1 B, 1 KB, 100 KB, 1 MB, 10 MB and 10 k / 100 k rows, run over both plain and SSL connections. Without the fix the 1 MB+ cases hang.Test plan
pytest tests/test_proxy.py -k test_various_payload_sizes— all 14 cases passpytest tests/test_proxy.py— existing tests unchanged🤖 Generated with Claude Code