-
Notifications
You must be signed in to change notification settings - Fork 5k
http: closeAllConnections() must not stop the listener #33394
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
119 changes: 119 additions & 0 deletions
119
test/js/node/http/node-http-close-all-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,119 @@ | ||
| // These tests also pass on Node.js. `server.closeAllConnections()` destroys the | ||
| // tracked connections and leaves the listen socket alone, so the server keeps | ||
| // accepting traffic and a later `close(cb)` still completes normally. | ||
| import { 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 listenAndConnect(server: Server) { | ||
| const connected = once(server, "connection"); | ||
| server.listen(0, "127.0.0.1"); | ||
| await once(server, "listening"); | ||
| const { port } = server.address() as AddressInfo; | ||
|
|
||
| const client = connect(port, "127.0.0.1"); | ||
| client.on("error", () => {}); | ||
| await once(client, "connect"); | ||
| client.write("GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"); | ||
|
|
||
| const [serverSocket] = await connected; | ||
| return { port, client, serverSocket }; | ||
| } | ||
|
|
||
| test("closeAllConnections() destroys the connections but keeps the server listening", async () => { | ||
| const server = createServer((req, res) => res.end("ok")); | ||
| let closeEvents = 0; | ||
| server.on("close", () => closeEvents++); | ||
| try { | ||
| const { port, client, serverSocket } = await listenAndConnect(server); | ||
| // Read the response so the connection is idle but still kept alive. | ||
| await once(client, "data"); | ||
|
|
||
| // Node destroys the socket object itself, so anything tracking sockets via | ||
| // 'connection' + socket.on('close') sees them leave. | ||
| const serverSocketClosed = once(serverSocket, "close"); | ||
| const clientClosed = once(client, "close"); | ||
| server.closeAllConnections(); | ||
|
|
||
| expect(serverSocket.destroyed).toBe(true); | ||
| await serverSocketClosed; | ||
| await clientClosed; | ||
|
|
||
| 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("closeAllConnections() destroys in-flight connections after close()", 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 { client, serverSocket } = await listenAndConnect(server); | ||
| await requestReceived; | ||
|
|
||
| const { promise: serverClosed, resolve: onClosed } = Promise.withResolvers<Error | undefined>(); | ||
| const clientClosed = once(client, "close"); | ||
| server.close(onClosed); | ||
| server.closeAllConnections(); | ||
|
|
||
| expect(serverSocket.destroyed).toBe(true); | ||
| await clientClosed; | ||
| expect(await serverClosed).toBeUndefined(); | ||
| expect(server.listening).toBe(false); | ||
| } finally { | ||
| if (server.listening) server.close(); | ||
| } | ||
| }); | ||
|
|
||
| test("closeAllConnections() destroys every tracked connection", async () => { | ||
| const server = createServer((req, res) => res.end("ok")); | ||
| const serverSockets: Socket[] = []; | ||
| server.on("connection", socket => serverSockets.push(socket)); | ||
| try { | ||
| server.listen(0, "127.0.0.1"); | ||
| await once(server, "listening"); | ||
| const { port } = server.address() as AddressInfo; | ||
|
|
||
| const clients: Socket[] = []; | ||
| for (let i = 0; i < 4; i++) { | ||
| const client = connect(port, "127.0.0.1"); | ||
| client.on("error", () => {}); | ||
| await once(client, "connect"); | ||
| client.write("GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"); | ||
| await once(client, "data"); | ||
| clients.push(client); | ||
| } | ||
| expect(serverSockets).toHaveLength(4); | ||
|
|
||
| const allClosed = Promise.all(clients.map(client => once(client, "close"))); | ||
| server.closeAllConnections(); | ||
| expect(serverSockets.map(socket => socket.destroyed)).toEqual([true, true, true, true]); | ||
|
|
||
| await allClosed; | ||
| expect(server.listening).toBe(true); | ||
| } finally { | ||
| server.closeAllConnections(); | ||
| if (server.listening) server.close(); | ||
| } | ||
| }); | ||
|
|
||
| test("closeAllConnections() on a server that never listened does nothing", () => { | ||
| const server = createServer(() => {}); | ||
| expect(() => server.closeAllConnections()).not.toThrow(); | ||
| expect(server.listening).toBe(false); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
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.