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.67% -0.01%
==========================================
Files 800 800
Lines 280918 280935 +17
Branches 54273 54290 +17
==========================================
+ Hits 254720 254728 +8
- Misses 16293 16306 +13
+ Partials 9905 9901 -4
🚀 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: |
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, 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.