Skip to content

fix: keep shared server alive for a request built before another finishes - #910

Open
Dmao233 wants to merge 1 commit into
forwardemail:masterfrom
Dmao233:fix-shared-server-late-dispatch
Open

Dmao233 wants to merge 1 commit into
forwardemail:masterfrom
Dmao233:fix-shared-server-late-dispatch

Conversation

@Dmao233

@Dmao233 Dmao233 commented Oct 5, 2026

Copy link
Copy Markdown

Fixes #909

Building a request against the shared server calls serverAddress(), which listens, but state.pending is only incremented in end(). An inner request that finishes first sees pending === 0, closes the server, and deletes the state. The outer end() then dispatches at the closed address. On 7.3.1 that is TypeError: Cannot read properties of null (reading 'address') when server.address() is null. If the URL already contains the port, the same case is ECONNREFUSED.

pending is not incremented at construction. A Test that is built and never sent would otherwise keep the port after every other request finished. When end() finds the shared server it bound already closed, or closing, it listens again on 127.0.0.1 with an ephemeral port, waits for listening, rewrites the URL with the current port, and dispatches. Requests that have already called end() still share one state, and the last one closes the server. A server the caller passed in already listening is left alone.

A Test that is built and never end()ed no longer blocks a later request from closing the server. If nothing in the process calls end(), the server stays listening. That is the existing behavior: construction calls listen(), and only an end() that drops pending to 0 calls close().

Testing

  • npx mocha --exit --require should --reporter spec --check-leaks test/supertest.js --grep "shared server late dispatch": before this change, request(server) and request.agent(server) failed with the TypeError (0 passing, 2 failing). After, 4 passing, including the case where the port is already in the URL
  • npm test: eslint clean, mocha 141 passing

…shes

A request built against the shared server did not hold it open until
end(), so another request finishing in between closed the server and
the late send dispatched at a dead address.

fixes forwardemail#909
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7.3.0: a request built before another request finishes is sent to a closed server

1 participant