Repository navigation
node:http: a tunnel holds the event loop only while it reads or has bytes left to send #44885
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
a2f80f4
5d6cddc
361a937
fe3f956
d74e586
c434739
37dd19b
2c26d01
5af7e9d
835a3ed
eabb827
66a58c9
8f729a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -203,6 +203,7 @@ void JSNodeHTTPServerSocket::readStop() | |
| } | ||
| tunnelReadsStopped = true; | ||
| applyTunnelReads(); | ||
| updateTunnelIdle(); | ||
| } | ||
|
|
||
| void JSNodeHTTPServerSocket::readStart() | ||
|
|
@@ -212,6 +213,7 @@ void JSNodeHTTPServerSocket::readStart() | |
| } | ||
| tunnelReadsStopped = false; | ||
| applyTunnelReads(); | ||
| updateTunnelIdle(); | ||
| } | ||
|
|
||
| void JSNodeHTTPServerSocket::didDeliverQueuedTunnelBytes(size_t length) | ||
|
|
@@ -884,14 +886,14 @@ extern "C" bool Bun__NodeHTTPServerSocket__writeBehindResponse(us_socket_t* sock | |
|
|
||
| void JSNodeHTTPServerSocket::updateTunnelIdle() | ||
| { | ||
| if (!tunnelReadEnded || upgraded || isClosed()) { | ||
| if (!isTunnel(this)) { | ||
| return; | ||
| } | ||
|
Comment on lines
+902
to
904
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟣 pre-existing, not blocking: pre-existing: on an Upgrade request that carries a body, bytes a listener writes before the body completes neither hold the event loop nor get flushed, so behind server.unref() the process exits with them unsent and socket.end() after them never sends its FIN. updateTunnelIdle() at JSNodeHTTPServerSocket.cpp:902 returns while isConnectRequest is still false (HTTP_NODE_TUNNEL_AFTER_BODY), so no +5 fires, and HttpContext.h:940 skips onSocketDrain for the same reason, so the writable event drops its interest with streamBuffer still full. Fix: treat a HTTP_NODE_TUNNEL_AFTER_BODY socket as a tunnel for drain and sending/idle accounting, so the rule "bytes left to send hold the loop" covers this sibling path too. Why this was flaggedAn 'upgrade' listener for a request with Content-Length > 0 calls socket.write(data) larger than the kernel takes while the body is still arriving. jsFunctionNodeHTTPServerSocketWrite (JSNodeHTTPServerSocketPrototype.cpp:281) parks the rest in streamBuffer, then calls updateTunnelIdle(), which returns at JSNodeHTTPServerSocket.cpp:902 because isTunnel() reads isConnectRequest and that is only set at HttpContext.h:672 when the body's fin is parsed; so setNodeHttpTunnelSending(true) never runs and sending_tunnel_ref (mod.rs:1633) is never taken. The writable event reaches HttpContext::onWritable, but :940 gates onSocketDrain on isConnectRequest, so JSNodeHTTPServerSocket::onDrain never flushes streamBuffer and loop.c:665 drops the writable interest. With server.unref() and nothing else holding the loop, the process exits with the bytes unsent; without unref, a later socket.end() returns at JSNodeHTTPServerSocketPrototype.cpp:317-320 waiting for a drain that never comes, so the FIN is never sent. The base branch behaves the same on this path. Verification: pre-existing. An 'upgrade' listener for an Upgrade request that carries a body writes more than the kernel takes while the body is still arriving.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stays as is, and the description lists it under "Not changed by this PR": the same output on 1.4.3, on a canary of main, on 5af7e9d and here, for Generated by Claude Code |
||
| const bool sent = streamBuffer.bufferedSize() == 0; | ||
| const bool idle = (tunnelReadEnded || tunnelReadsStopped) && streamBuffer.bufferedSize() == 0; | ||
| if (is_ssl) { | ||
| reinterpret_cast<uWS::HttpResponse<true>*>(socket)->setNodeHttpTunnelIdle(sent && reinterpret_cast<uWS::AsyncSocket<true>*>(socket)->hasFullyDrained()); | ||
| reinterpret_cast<uWS::HttpResponse<true>*>(socket)->setNodeHttpTunnelIdle(idle && reinterpret_cast<uWS::AsyncSocket<true>*>(socket)->hasFullyDrained(), tunnelReadEnded); | ||
| } else { | ||
| reinterpret_cast<uWS::HttpResponse<false>*>(socket)->setNodeHttpTunnelIdle(sent && reinterpret_cast<uWS::AsyncSocket<false>*>(socket)->hasFullyDrained()); | ||
| reinterpret_cast<uWS::HttpResponse<false>*>(socket)->setNodeHttpTunnelIdle(idle && reinterpret_cast<uWS::AsyncSocket<false>*>(socket)->hasFullyDrained(), tunnelReadEnded); | ||
| } | ||
| } | ||
|
|
||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.