Repository navigation
Conversation
|
Review requested:
|
|
Welcome to Node.js, and thank you for your first contribution! Before review, please take a moment to read:
Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal. |
pimterry
left a comment
There was a problem hiding this comment.
Totally agree this is weird behaviour and we should fix it, thanks @WhatCats! I think there are tweaks to make here though:
- I don't think we should expose an option on
closeIdleConnections- I'm not sure when this would be used on servers in scenarios except withserver.close(), boolean arguments are messy, it's a bit confusing as an API. Can we scope the change just toserver.close(), which is the real problem? - As in my comments, I don't think checking
active()works. I think you can probably simplify this though: don't set a marker at all, instead both_storeHeaderandresOnFinishcan reach the server (fromthis[kSocket]?.serveror theserverarg respectively). We could just check if the server is closing down, and if not then don't keep alive. Does that work?
Codecov Reportβ
All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #66000 +/- ##
==========================================
- Coverage 90.67% 90.51% -0.16%
==========================================
Files 800 805 +5
Lines 280918 286073 +5155
Branches 54273 55245 +972
==========================================
+ Hits 254720 258946 +4226
- Misses 16293 17232 +939
+ Partials 9905 9895 -10
π New features to boost your workflow:
|
48691f3 to
d8b770e
Compare
|
Gotcha. I reworked it into just an internal thing |
|
Ah, there's now a conflict here, can you rebase this @WhatCats? |
|
Hmm, also, is this semver-major? Ping @nodejs/http Previously if you had requests in flight during What do we think? I'm inclined to lean yes. |
This comment was marked as resolved.
This comment was marked as resolved.
d8b770e to
0f68125
Compare
2973d1b to
0a8338c
Compare
`server.close()` closes the connections that are idle at that moment. One that is sending a request or waiting for a response is left alone, and once its response finishes nothing rechecks it: it stays keep-alive until `keepAliveTimeout` reaps it. Mark the server as closing, and check that mark where a connection would otherwise be kept alive: the response stops advertising `Connection: keep-alive`, and the socket closes once that response finishes with nothing queued behind it. The mark is cleared when the server is listened on again. Signed-off-by: WhatCats <whatcater@gmail.com>
0a8338c to
8a0ec39
Compare
|
I probably should have mentioned you the first time. But here we go again: |
araujogui
left a comment
There was a problem hiding this comment.
with pipelined requests, the response being written when close() is called closes the socket, so accepted requests queued behind it get no response. Could the check apply only to the last queued response?
Do not decide at header time that the connection will close. A response ended from inside the request callback cannot know whether more pipelined requests from the same chunk follow, and closing after it dropped them. Keep the normal headers and close the socket in resOnFinish() once the last queued response is done. Assisted-by: a closed-source coding agent Signed-off-by: WhatCats <whatcater@gmail.com>
|
@araujogui Now the headers are left alone, and the connection only closes once the last queued response has finished. By then all pipelined requests have been parsed, so they all get answered. I added a test for that too. The tradeoff is that the last response still says Also, the first commit message still says the response stops advertising keep-alive. That's no longer true, so that part should be dropped when this lands. |
|
Also, my closed-source coding agent flagged that a client that keeps pipelining can hold the connection open for as long as it keeps sending. This matters for this PR because the point is that |
A client that keeps pipelining could keep a connection of a closed server open forever, since the queue never ran empty. Cap it when the first response finishes after close(): the requests queued at that point are still answered, and the socket closes after the last of them. Assisted-by: a closed-source coding agent Signed-off-by: WhatCats <whatcater@gmail.com>
@araujogui @WhatCats I think the original behaviour here was reasonable, and probably better than the updated version. RFC says:
It is normal that a server closes a connection with pending pipelined requests, it's actually required to send no response in that case, and clients should be able to handle that. In practice pipelining support really doesn't matter much anyway imo, because roughly zero real clients use it - no browsers have used it for a decade, curl dropped it in entirely 2019, etc. By a large margin the most common pipelining clients are benchmarking tools. We shouldn't actually break pipelining, but I think anywhere there are tradeoffs (like not signalling close or stopping server shutdown, as in @WhatCats's last message) I'd rather have better & safer behaviour for normal use and accept mild inconvenience for pipelining, instead of trying to actively support pipelining. OTOH short keep-alive + close really has caused us plenty of real-world issues in the past (see #59193) because normal client send requests at the same time as the connection close arrives, get RSTs back, and rarely handle it well. Does that make sense? What do you think @araujogui? |
|
I tested this PR and the alternatives against Node, Bun, Go and Rust HTTP clients. I used undici as the pipelining client, with pipelining: 3 and blocking: false. The Node variants were ported by hand onto v26.13.1's _http_server.js. I haven't built main. Problems before the changes
Problems after the PR
New problems found
Other serversI ran the same shutdown test against Go net/http and Rust hyper servers.
Proposed fixes (theoretical)
If keeping already-queued pipelined requests isn't worth it, the simpler option matching Go and hyper is the first commit plus fix 2. |
|
Since it looks like this solution to an issue related to idle connection termination is being actively developed, I just wanted to drop a link to an issue #67910 and a PR #67915 I just filed which are also about idle connection termination. Mine is about a case where |
Summary
server.close()callscloseIdleConnections()internally (since bd7a808), which closes every connection that is idle at that exact moment. A connection that's actively sending a request or waiting for a response whenclose()runs is correctly left alone and once that response finishes, nothing ever rechecks it. It reverts to being a normal keep-alive connection and stays open untilkeepAliveTimeoutreaps it (65s by default), rather than closing as part of theserver.close()call.Interestingly, when investigating, I noticed this exact gap is also independently documented in Bun's source: https://github.com/oven-sh/bun/blob/df573dace6178d4ff94a797313bc8b5fdcf515e4/src/runtime/server/mod.rs#L1747-L1751
Reproduce
Call
server.close()while a request is in flight. Thecloseevent doesn't fire untilkeepAliveTimeout(+keepAliveTimeoutBuffer) elapses, even though the response completed almost immediately. (seetest/parallel/test-http-server-close-when-idle.js)Fix
Mark the server as closing. When a connection's last queued response finishes, close the socket instead of keeping it alive. Pipelined requests that were already accepted still get answered first. Response headers are unchanged. The mark is cleared when the server is listened on again.
AI disclosure
I used Claude Code to help with this PR: investigating the original issue, drafting the fix and tests, and later the pipelining fix from review. I reviewed all the changes myself and verified them by building Node and running the new test plus the
test-http-*,test-https-*andtest-net-server-*suites.