-
Notifications
You must be signed in to change notification settings - Fork 5k
fix(http): preserve server reference across close() for closeAllConnections() #30505
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 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7f65d17
fix(http): preserve server reference across close() for closeAllConne…
robobun 282497a
[autofix.ci] apply automated fixes
autofix-ci[bot] e66460b
fix(http): gate ref()/setTimeout()/listening on kServerClosed after c…
robobun 73af2d7
fix(http): reset kServerClosed only after Bun.serve() succeeds
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
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
120 changes: 120 additions & 0 deletions
120
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,120 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe } from "harness"; | ||
| import { once } from "node:events"; | ||
| import http from "node:http"; | ||
| import type { AddressInfo } from "node:net"; | ||
| import { connect } from "node:net"; | ||
|
|
||
| // Regression: `@azure/msal-node`'s `LoopbackClient.closeServer` calls | ||
| // server.close(); | ||
| // server.closeAllConnections(); | ||
| // server.unref(); | ||
| // in sequence. Bun used to null out the internal server reference in | ||
| // `close()`, so the subsequent `closeAllConnections()` was a no-op — | ||
| // the keep-alive socket kept the event loop alive and the process hung. | ||
| // Issue: https://github.com/oven-sh/bun/issues/30501 | ||
| test("closeAllConnections() after close() force-closes in-flight sockets", async () => { | ||
| const { promise: requestReceived, resolve: resolveReceived } = Promise.withResolvers<void>(); | ||
| const server = http.createServer((req, _res) => { | ||
| // Signal receipt but DO NOT reply — socket is "in flight" (not idle) | ||
| // when the teardown sequence runs. This is the case where close() | ||
| // alone (which only closes idle connections) cannot reclaim the | ||
| // socket, and closeAllConnections() must do it. | ||
| resolveReceived(); | ||
| }); | ||
| await once(server.listen(0, "127.0.0.1"), "listening"); | ||
| const { port } = server.address() as AddressInfo; | ||
|
|
||
| const sock = connect(port, "127.0.0.1"); | ||
| const { promise: sockClosed, resolve: resolveClosed } = Promise.withResolvers<void>(); | ||
| sock.on("close", () => resolveClosed()); | ||
| sock.on("error", () => {}); | ||
| await once(sock, "connect"); | ||
| sock.write("GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"); | ||
| await requestReceived; | ||
|
|
||
| // msal-style teardown — no waiting between calls | ||
| server.close(); | ||
| server.closeAllConnections(); | ||
| server.unref(); | ||
|
|
||
| // The client socket must be force-closed by closeAllConnections(). | ||
| // Without the fix, the socket stays open indefinitely (msal hang). | ||
| await sockClosed; | ||
| }); | ||
|
|
||
| // Regression: when a caller does close() and then listen() again before | ||
| // the previous shutdown's allClosed promise has fulfilled, the stale | ||
| // callback must not null out the newly-created server handle. | ||
| test("listen() during an in-flight close() doesn't corrupt the new server", async () => { | ||
| const server = http.createServer((_req, res) => res.end("ok")); | ||
|
|
||
| await once(server.listen(0, "127.0.0.1"), "listening"); | ||
|
|
||
| // Fire-and-forget close; don't wait for the allClosed callback. | ||
| server.close(); | ||
|
|
||
| // Re-listen immediately while the previous shutdown is still settling. | ||
| await once(server.listen(0, "127.0.0.1"), "listening"); | ||
| const secondAddress = server.address() as AddressInfo | null; | ||
| expect(secondAddress).not.toBeNull(); | ||
| const secondPort = secondAddress!.port; | ||
| expect(secondPort).toBeInteger(); | ||
|
|
||
| // Drain the new server — address() must still return a port after | ||
| // microtasks run (this is where the stale close callback would have | ||
| // hit, pre-fix). | ||
| await new Promise(r => setImmediate(r)); | ||
| expect((server.address() as AddressInfo | null)?.port).toBe(secondPort); | ||
|
|
||
| // Confirm the new server actually serves requests, not just has a port. | ||
| const res = await fetch(`http://127.0.0.1:${secondPort}`); | ||
| expect(await res.text()).toBe("ok"); | ||
|
|
||
| await new Promise<void>(r => server.close(() => r())); | ||
| }); | ||
|
|
||
| // End-to-end: spawn a child that opens an HTTP server, accepts a | ||
| // keep-alive connection, and calls the msal teardown. Must exit | ||
| // immediately — not wait for the keep-alive idle timeout to reclaim | ||
| // the in-flight socket. | ||
| test("process exits after close() + closeAllConnections() + unref() teardown", async () => { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [ | ||
| bunExe(), | ||
| "-e", | ||
| ` | ||
| const http = require("node:http"); | ||
| const net = require("node:net"); | ||
| const server = http.createServer((req, _res) => { | ||
| // Never reply — keep the socket in-flight (not idle) so that | ||
| // only closeAllConnections() (abrupt) can reclaim it. | ||
| server.close(); | ||
| server.closeAllConnections(); | ||
| server.unref(); | ||
| process.stdout.write("TEARDOWN_DONE\\n"); | ||
| }); | ||
| server.listen(0, "127.0.0.1", () => { | ||
| const port = server.address().port; | ||
| const sock = net.connect(port, "127.0.0.1", () => { | ||
| sock.write("GET / HTTP/1.1\\r\\nHost: localhost\\r\\nConnection: keep-alive\\r\\n\\r\\n"); | ||
| }); | ||
| sock.on("data", () => {}); | ||
| sock.on("error", () => {}); | ||
| }); | ||
| `, | ||
| ], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| // If the teardown path is broken, the subprocess never exits and | ||
| // `proc.exited` never resolves — the bun:test runner's default 5s | ||
| // timeout catches that. | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| // Surface any uncaught exception / ASAN trace before the exit-code | ||
| // assertion so failures point at the real cause. | ||
| expect(stderr).toBe(""); | ||
| expect(stdout).toContain("TEARDOWN_DONE"); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
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.