-
Notifications
You must be signed in to change notification settings - Fork 5k
node:http: make closeAllConnections()/closeIdleConnections() leave the listener alone and work after close() #35839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robobun
wants to merge
11
commits into
main
Choose a base branch
from
farm/6ccde698/http-close-connections-after-close
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ba8e513
node:http: make closeIdleConnections/closeAllConnections work after c…
robobun 61aa232
node:http: spare upgraded and in-progress-head sockets in the connect…
robobun cc0449b
trim drain-method comments
robobun b5a1b9d
node:http: match Node's closeIdleConnections _httpMessage.finished check
robobun b60e4c4
ci: retrigger
robobun f72eca6
Merge remote-tracking branch 'origin/main' into farm/6ccde698/http-cl…
robobun d857ae1
test: cover the msal-node teardown sequence and multi-connection clos…
robobun 21d17f4
test: closeAllConnections() with no connections established leaves th…
robobun 2bb18af
Merge remote-tracking branch 'origin/main' into farm/6ccde698/http-cl…
robobun 4a10e91
node:http: keep an upgrade connection listed until its request body h…
robobun 96f948f
test: drop stale note about closeAllConnections() stopping the server
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
184 changes: 184 additions & 0 deletions
184
test/js/node/http/node-http-server-close-connections.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| // server.closeIdleConnections() / server.closeAllConnections() must keep | ||
| // working after server.close() has run: that is the canonical graceful-drain | ||
| // pattern (close(); wait; closeIdleConnections()) and the force path used by | ||
| // http-terminator. These tests also pass on Node.js. | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { once } from "node:events"; | ||
| import { createServer, type Server } from "node:http"; | ||
| import { connect, type AddressInfo, type Socket } from "node:net"; | ||
|
|
||
| async function listen(server: Server) { | ||
| server.listen(0, "127.0.0.1"); | ||
| await once(server, "listening"); | ||
| return (server.address() as AddressInfo).port; | ||
| } | ||
|
|
||
| async function openConnection(server: Server, port: number) { | ||
| const gotConnection = once(server, "connection"); | ||
| const client = connect(port, "127.0.0.1"); | ||
| client.on("error", () => {}); | ||
| client.on("data", () => {}); | ||
| await once(client, "connect"); | ||
| return { client, gotConnection }; | ||
| } | ||
|
|
||
| function waitClose(client: Socket) { | ||
| // once() rejects on 'error'; the client may see ECONNRESET on a forced | ||
| // close, which for this test still means "the connection was reaped". | ||
| return new Promise<void>(resolve => client.once("close", () => resolve())); | ||
| } | ||
|
|
||
| describe.each(["closeIdleConnections", "closeAllConnections"] as const)("%s", method => { | ||
| test("reaps a connection that went idle after close()", async () => { | ||
| let finishResponse!: () => void; | ||
| const responseGate = new Promise<void>(r => (finishResponse = r)); | ||
| const { promise: responded, resolve: onResponded } = Promise.withResolvers<void>(); | ||
| const server = createServer(async (req, res) => { | ||
| await responseGate; | ||
| res.on("finish", () => onResponded()); | ||
| res.end("ok"); | ||
| }); | ||
| server.keepAliveTimeout = 60_000; | ||
| try { | ||
| const port = await listen(server); | ||
| const { client, gotConnection } = await openConnection(server, port); | ||
| const clientClosed = waitClose(client); | ||
|
|
||
| // Request is in flight when close() runs, so close() on its own leaves | ||
| // this connection open. | ||
| client.write("GET / HTTP/1.1\r\nHost: x\r\nConnection: keep-alive\r\n\r\n"); | ||
| const [serverSocket] = await gotConnection; | ||
| server.close(); | ||
|
|
||
| // Let the response finish: the connection is now idle but still open | ||
| // (kept alive). | ||
| finishResponse(); | ||
| await responded; | ||
| expect(serverSocket.destroyed).toBe(false); | ||
|
|
||
| // The post-close call must reap it. | ||
| server[method](); | ||
| expect(serverSocket.destroyed).toBe(true); | ||
| await clientClosed; | ||
| client.destroy(); | ||
| } finally { | ||
| server.closeAllConnections(); | ||
| if (server.listening) server.close(); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe("closeIdleConnections", () => { | ||
| test("skips in-flight connections and reaps idle ones", async () => { | ||
| const inflightResponses: import("node:http").ServerResponse[] = []; | ||
| const server = createServer((req, res) => { | ||
| if (req.url === "/inflight") { | ||
| inflightResponses.push(res); | ||
| return; // never respond | ||
| } | ||
| res.end("ok"); | ||
| }); | ||
| server.keepAliveTimeout = 60_000; | ||
| try { | ||
| const port = await listen(server); | ||
|
|
||
| const { client: idle, gotConnection: idleConn } = await openConnection(server, port); | ||
| const idleResponse = once(idle, "data"); | ||
| const idleClosed = waitClose(idle); | ||
| idle.write("GET /idle HTTP/1.1\r\nHost: x\r\nConnection: keep-alive\r\n\r\n"); | ||
| const [idleServerSocket] = await idleConn; | ||
| await idleResponse; | ||
|
|
||
| const { client: busy, gotConnection: busyConn } = await openConnection(server, port); | ||
| const busyClosed = waitClose(busy); | ||
| busy.write("GET /inflight HTTP/1.1\r\nHost: x\r\nConnection: keep-alive\r\n\r\n"); | ||
| const [busyServerSocket] = await busyConn; | ||
| while (inflightResponses.length === 0) await new Promise(r => setImmediate(r)); | ||
|
|
||
| server.closeIdleConnections(); | ||
|
|
||
| expect(idleServerSocket.destroyed).toBe(true); | ||
| expect(busyServerSocket.destroyed).toBe(false); | ||
| await idleClosed; | ||
|
|
||
| server.closeAllConnections(); | ||
| await busyClosed; | ||
| idle.destroy(); | ||
| busy.destroy(); | ||
| await new Promise<void>(r => server.close(() => r())); | ||
| } finally { | ||
| server.closeAllConnections(); | ||
| if (server.listening) server.close(); | ||
| } | ||
| }); | ||
|
robobun marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| describe("closeAllConnections", () => { | ||
| test("after close(), destroys in-flight connections so the close callback runs", async () => { | ||
| const { promise: requestReceived, resolve: onRequest } = Promise.withResolvers<void>(); | ||
| // Never respond: the connection stays in-flight, so close() alone cannot | ||
| // finish. | ||
| const server = createServer(() => onRequest()); | ||
| try { | ||
| const port = await listen(server); | ||
| const { client, gotConnection } = await openConnection(server, port); | ||
| const clientClosed = waitClose(client); | ||
| client.write("GET / HTTP/1.1\r\nHost: x\r\n\r\n"); | ||
| const [serverSocket] = await gotConnection; | ||
| await requestReceived; | ||
|
|
||
| const { promise: closed, resolve: onClosed } = Promise.withResolvers<Error | undefined>(); | ||
| server.close(onClosed); | ||
| server.closeAllConnections(); | ||
|
|
||
| expect(serverSocket.destroyed).toBe(true); | ||
| await clientClosed; | ||
| expect(await closed).toBeUndefined(); | ||
| client.destroy(); | ||
| } finally { | ||
| server.closeAllConnections(); | ||
| if (server.listening) server.close(); | ||
| } | ||
| }); | ||
|
|
||
| test("does not stop the listen socket", async () => { | ||
| const server = createServer((req, res) => res.end("ok")); | ||
| let closeEvents = 0; | ||
| server.on("close", () => closeEvents++); | ||
| try { | ||
| const port = await listen(server); | ||
| const { client } = await openConnection(server, port); | ||
| const firstResponse = once(client, "data"); | ||
| client.write("GET / HTTP/1.1\r\nHost: x\r\nConnection: keep-alive\r\n\r\n"); | ||
| await firstResponse; | ||
|
|
||
| const clientClosed = waitClose(client); | ||
| server.closeAllConnections(); | ||
| await clientClosed; | ||
|
|
||
| // The listener is untouched: still listening, no 'close' event, and a | ||
| // fresh request is served. | ||
| expect(server.listening).toBe(true); | ||
| expect(closeEvents).toBe(0); | ||
|
|
||
| const res = await fetch(`http://127.0.0.1:${port}/`); | ||
| expect(await res.text()).toBe("ok"); | ||
| expect(res.status).toBe(200); | ||
|
|
||
| const { promise, resolve } = Promise.withResolvers<Error | undefined>(); | ||
| server.close(resolve); | ||
| expect(await promise).toBeUndefined(); | ||
| expect(server.listening).toBe(false); | ||
| expect(closeEvents).toBe(1); | ||
| } finally { | ||
| server.closeAllConnections(); | ||
| if (server.listening) server.close(); | ||
| } | ||
| }); | ||
|
|
||
| test("is a no-op on a server that never listened", () => { | ||
| const server = createServer(); | ||
| expect(() => server.closeAllConnections()).not.toThrow(); | ||
| expect(() => server.closeIdleConnections()).not.toThrow(); | ||
| }); | ||
| }); | ||
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.