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. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #67292 +/- ##
==========================================
+ Coverage 90.69% 90.71% +0.01%
==========================================
Files 797 802 +5
Lines 278787 282202 +3415
Branches 53610 54386 +776
==========================================
+ Hits 252849 255992 +3143
- Misses 16310 16324 +14
- Partials 9628 9886 +258
🚀 New features to boost your workflow:
|
|
I pushed one more commit, test only (13a482f). It adds a case, for I ran the updated test on a release build on Linux, and it passed. Agent-run validation: the new case fails without the first commit of this PR and passes with it; the regression test, the HTTP/HTTPS tests and the full |
…6171) The keep-alive timer of an idle socket cannot fire while the event loop is blocked. When the next request is dispatched from the poll phase (an I/O callback, a server request handler, or a continuation after awaiting I/O), the idle socket validation added for GHSA-35p6-xmwp-9g52 completes in the check phase of the same loop iteration, before that timer fires and before a FIN received during the block is read. The request is then written to a socket that the server may already have closed, and fails with ECONNRESET. Record when the keep-alive timer is armed, and destroy the socket with an InformationalError instead of reusing it once its keep-alive timeout has passed. Nothing is running on the socket, so the pending requests are sent on a new connection. The check does not depend on kSocketUsed, so it also covers the experimental Milo parser, which never sets that symbol. Refs: nodejs/node#67292 Claude-Session: https://claude.ai/code/session_01Sfj4nEWQ4ovycNDjbvXcRs Signed-off-by: Matteo Collina <hello@matteocollina.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
A free socket with an idle timeout is normally destroyed by its timer. When the event loop is blocked long enough for that timeout to expire, the timer cannot run, and `addRequest()` can reuse the socket even though the server may already have closed the connection. This can cause the request to fail with `ECONNRESET`. Record when a socket enters the free pool and, before reuse, remove and destroy a free socket whose idle timeout has expired according to the timers' clock. Refs: nodejs#54293 Assisted-by: a closed-source coding agent Signed-off-by: dennev <dennev13@gmail.com>
A destroyed socket can stay in the agent's free pool until its 'close' event. `addRequest()` only dropped destroyed sockets from the head of the pool, so a request made before that event could still take one, for example from the tail of the pool with the default `lifo` scheduling, and fail with `ECONNRESET`. Skip destroyed sockets when selecting a socket to reuse. Destroyed sockets rejected during selection are removed at once, as expired sockets are. Leading destroyed sockets are still pruned and counted until their 'close' event. Assisted-by: a closed-source coding agent Signed-off-by: dennev <dennev13@gmail.com>
When the agent takes a socket from the free pool, it removes expired sockets until it finds one that has not expired. No case had a pool that holds both kinds, so nothing checked that the request then reuses the unexpired socket and that only the expired one is removed. Assisted-by: a closed-source coding agent Signed-off-by: dennev <dennev13@gmail.com>
Instead of recording when a socket enters the free pool, check the socket's idle timer, which records on the timers' clock when it was last started or refreshed. This removes the clock read and the property write from the 'free' handler, and a pooled socket whose timeout is set again is now expired by its restarted timer. With an agent timeout, the client clears the socket's timeout when it keeps the socket alive, and the agent restarts the timer as the socket enters the pool, so in that flow the expiry is measured from the same moment as before. socket.timeout is still checked, because a timer that fired before it was cleared keeps its _idleTimeout. Assisted-by: a closed-source coding agent Signed-off-by: dennev <dennev13@gmail.com>
9ac5f02 to
078c6d9
Compare
When the event loop is blocked long enough for a free socket's idle timeout to expire, its timeout callback cannot run. The next request can then reuse that socket from
agent.freeSocketseven though the server may already have closed the connection, causing the request to fail withECONNRESET.This change addresses the timeout-expiry case discussed in #54293. In
addRequest(), the agent reads the timers' clock (timersBinding.getLibuvNow()) once and checks each free candidate against its idle timer (socket[kTimeout]), which records on the same clock when it was last started or refreshed. Expired candidates are removed and destroyed until an unexpired socket is found or the pool is empty. A socket without a timeout (socket.timeoutis 0), or a custom stream that does not have this timer, is never expired this way.Following review, a second commit also keeps
addRequest()from handing out a free socket that is already destroyed, wherever it is in the pool. A destroyed candidate is removed from the agent at once, like an expired one. Destroyed sockets at the head of the pool are still dropped by the existing loop, as before, and stay counted inagent.totalSocketCountuntil their'close'event. A third commit adds a test for a pool that holds an expired and an unexpired socket, and a fourth, following a review suggestion, reads the expiry from the socket's idle timer instead of a timestamp recorded when the socket entered the pool.The clock call preserves the binding object as its receiver, as required by the fast API call. The regression test also exercises an optimized
addRequest().Not covered by this change:
timeout(http: Agent with keepAlive and no timeout ignores the server's Keep-Alive timeout hint #66734, http: respect Keep-Alive timeout hint when agent has no explicit timeout #66779)'free'handler to a queued request'data'or'end'handler: with an agent timeout, the client clears the socket's timeout when it keeps the socket alive, and the agent restarts the idle timer as the socket enters the pool, so time spent blocked before that point is not countedTesting: I personally ran the regression test on a release build in a Linux container at the current head (9ac5f02), and it passed. I also ran a reproduction based on #54293, with
server.keepAliveTimeout = 5000and a 6-second block between two requests throughhttp.globalAgent. It fails withECONNRESETon v26.12.1 and succeeds with the first commit, using a new socket.Agent-run validation confirmed that the regression test fails without this change and passes with it. It covers LIFO and FIFO expiry, reuse before expiry, unchanged reuse without an idle timeout,
maxSocketsaccounting after a block, a pool that holds an expired and an unexpired socket (LIFO and FIFO), a pooled socket whose timeout is set again, a request timeout that fired and was cleared, a destroyed socket at the tail of the pool with LIFO, and optimizedaddRequest().Agent-run validation at the current head (Linux, release build): the regression test, the HTTP/HTTPS and agent-related tests and the full
make -j4 testpassed, and the regression test also passed on a debug build, where its fast API call assertion runs. In agent runs of the same cases, the mixed-pool case fails without the first commit, the destroyed-socket case fails without the second, and the restarted-timeout case fails without the fourth. An agent-run reproduction based on #54293 also fails withECONNRESETon v26.13.1 (official Docker image, Linux).AI disclosure: Claude Code (Claude Opus 5.5) was used to investigate the cause, write and review the code, test and documentation changes, and run the agent-run validation described above. Before opening this pull request, Claude and Codex (gpt-6.1-sol) reviewed the first commit, its commit message and the description as first posted. The second and third commits were prepared the same way with Claude Code (Claude Fable 5.2), and the fourth with Claude Code (Claude Opus 5.5). Claude reviewed the later commits and their messages before I pushed them, and drafts of my comments and of this updated description before I posted them; Codex also reviewed the third and fourth commits.
Refs: #54293