feat: support concurrent requests against a shared server (fixes #726) - #895
Open
viniciusamc wants to merge 1 commit into
Open
feat: support concurrent requests against a shared server (fixes #726)#895viniciusamc wants to merge 1 commit into
viniciusamc wants to merge 1 commit into
Conversation
viniciusamc
force-pushed
the
feat/concurrent-requests
branch
from
August 4, 2026 02:52
c1e2120 to
fff316b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Fixes #726.
When multiple requests share a server that supertest started (
request(server)with a non-listening server, orrequest.agent(app)), the first request to finish closes the server while the others are still in flight — they die withECONNRESET/ECONNREFUSED/EHOSTUNREACH. This is the flake reported in #726 (and the "2-3 flakes per run in a ~2000-test suite" comment there).The root cause is in
lib/test.js: only theTestthat calledlisten(0)holdsthis._server, and itsend()unconditionally closes the server as soon as its own response arrives.Fix
Replace "first request owns and closes the server" with in-flight counting:
serverAddress(): when supertest starts the server it marks it with an in-flight counter; everyTestsharing that server increments it and keeps the reference.end(): decrements exactly once per test (guarded, sinceend()can run twice via.expect(status, fn)+await) and closes the server only when the counter reaches zero.Servers the user started are never counted and never closed, as before. Single-request behavior is unchanged: the counter goes 1→0 in that request's own
end(), closing the server before assertions exactly as today.New API:
.concurrently(n, build)With the lifecycle fixed, concurrent testing (races, idempotency) becomes a first-class use case, so this also adds an ergonomic entry point:
It runs
nrequests against one shared server (a function app is wrapped in a single server, instead of one per request) and resolves with the responses in build order. PlainPromise.allover hand-built requests works too — the method just removes the boilerplate.Tests
read ECONNRESETwithout the fix.Known trade-off: a
Testthat is built but never dispatched now holds its slot, keeping the shared server open (previously this leak existed only when the abandoned test was the server-starting one).Checklist