-
Notifications
You must be signed in to change notification settings - Fork 4.7k
node:http: send 500 instead of 200 when handler throws synchronously #30075
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
2
commits into
main
Choose a base branch
from
farm/974c90be/node-http-sync-throw-500
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 all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe } from "harness"; | ||
|
|
||
| // When a node:http request handler throws synchronously before any status line | ||
| // has been written, Bun should respond with 500 Internal Server Error rather | ||
| // than defaulting to 200 OK. The cleanup path in server.zig checks whether a | ||
| // status has already been sent via isHttpStatusCalled(); previously the | ||
| // condition was inverted so the 500 branch was unreachable and endStream() | ||
| // wrote a default "HTTP/1.1 200 OK". | ||
| test.concurrent("node:http responds 500 when handler throws synchronously before writing status", async () => { | ||
| const script = /* js */ ` | ||
| const http = require("node:http"); | ||
| const net = require("node:net"); | ||
| const { once } = require("node:events"); | ||
|
|
||
| // Swallow the propagated handler exception so the process stays alive. | ||
| process.on("uncaughtException", err => { | ||
| process.stderr.write("uncaughtException:" + err.message + "\\n"); | ||
| }); | ||
|
|
||
| const server = http.createServer((req, res) => { | ||
| throw new Error("boom"); | ||
| }); | ||
|
|
||
| server.listen(0, "127.0.0.1", async () => { | ||
| const port = server.address().port; | ||
| const socket = net.connect(port, "127.0.0.1"); | ||
| await once(socket, "connect"); | ||
| socket.write("GET / HTTP/1.1\\r\\nHost: 127.0.0.1\\r\\nConnection: close\\r\\n\\r\\n"); | ||
| let raw = ""; | ||
| for await (const chunk of socket) raw += chunk.toString("latin1"); | ||
| const statusLine = raw.slice(0, raw.indexOf("\\r\\n")); | ||
| process.stdout.write("STATUS_LINE:" + statusLine + "\\n"); | ||
| server.close(); | ||
| process.exit(0); | ||
| }); | ||
| `; | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", script], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| stdin: "ignore", | ||
| }); | ||
|
|
||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| expect(stderr).toContain("uncaughtException:boom"); | ||
| expect(stdout.trim()).toBe("STATUS_LINE:HTTP/1.1 500 Internal Server Error"); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
|
|
||
| test.concurrent("node:http still terminates the response when handler throws after writeHead", async () => { | ||
| const script = /* js */ ` | ||
| const http = require("node:http"); | ||
| const net = require("node:net"); | ||
| const { once } = require("node:events"); | ||
|
|
||
| process.on("uncaughtException", err => { | ||
| process.stderr.write("uncaughtException:" + err.message + "\\n"); | ||
| }); | ||
|
|
||
| const server = http.createServer((req, res) => { | ||
| res.writeHead(201, { "x-before-throw": "yes" }); | ||
| res.flushHeaders(); | ||
| throw new Error("boom-after-head"); | ||
| }); | ||
|
|
||
| server.listen(0, "127.0.0.1", async () => { | ||
| const port = server.address().port; | ||
| const socket = net.connect(port, "127.0.0.1"); | ||
| await once(socket, "connect"); | ||
| socket.write("GET / HTTP/1.1\\r\\nHost: 127.0.0.1\\r\\nConnection: close\\r\\n\\r\\n"); | ||
| let raw = ""; | ||
| for await (const chunk of socket) raw += chunk.toString("latin1"); | ||
| const statusLine = raw.slice(0, raw.indexOf("\\r\\n")); | ||
| process.stdout.write("STATUS_LINE:" + statusLine + "\\n"); | ||
| process.stdout.write("HAS_HEADER:" + raw.includes("x-before-throw: yes") + "\\n"); | ||
| server.close(); | ||
| process.exit(0); | ||
| }); | ||
| `; | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", script], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| stdin: "ignore", | ||
| }); | ||
|
|
||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| expect(stderr).toContain("uncaughtException:boom-after-head"); | ||
| // Status was already sent before the throw, so it stays 201, but the | ||
| // connection must still be terminated (we received a complete response). | ||
| expect(stdout.trim()).toBe("STATUS_LINE:HTTP/1.1 201 Created\nHAS_HEADER:true"); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
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.