-
Notifications
You must be signed in to change notification settings - Fork 5.1k
node:http: close an upgraded socket when the client FINs before the request body completes #35063
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/849169e4/http-upgrade-body-fin-hang
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
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5ed5d9c
node:http: close an upgraded socket when the client FINs before the r…
robobun c5b6e98
fail a tunnel socket write() once the native socket is gone
robobun b9825e4
[autofix.ci] apply automated fixes
autofix-ci[bot] 35a1f16
move the upgrade-body-FIN test to its own file
robobun 3fe03ad
[autofix.ci] apply automated fixes
autofix-ci[bot] adb4ed3
report ERR_STREAM_DESTROYED for a backpressured write flushed at close
robobun ffd3d4b
reword the tunnel-after-body onEnd comment as an invariant
robobun b3f3f2f
destroy the client socket in the upgrade-body-FIN fixture
robobun f7f1eb6
exit the upgrade-body-FIN fixture explicitly once server.close() comp…
robobun 723445a
drop the bug-history clause from the server.close() comment
robobun bb9d44c
drop the #onClose pending-write flush
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe } from "harness"; | ||
|
|
||
| test("upgrade with a body: a mid-body client FIN closes the upgraded socket and lets server.close() complete", async () => { | ||
| // Like Node's UpgradeStream: a FIN before the request body completes closes | ||
| // the upgraded socket instead of leaving the connection half-open. | ||
| const fixture = /* js */ ` | ||
| const http = require("node:http"); | ||
| const net = require("node:net"); | ||
| const { once } = require("node:events"); | ||
|
|
||
| (async () => { | ||
| const server = http.createServer(); | ||
| let upgraded; | ||
| const gotUpgrade = new Promise(r => { upgraded = r; }); | ||
| let socketClosed; | ||
| const gotSocketClose = new Promise(r => { socketClosed = r; }); | ||
| const events = []; | ||
| server.on("upgrade", (req, socket, head) => { | ||
| events.push("upgrade"); | ||
| req.on("data", () => {}); | ||
| socket.on("end", () => { | ||
| events.push("end"); | ||
| socket.write("late", err => events.push("write-cb:" + (err ? err.code : "ok"))); | ||
| }); | ||
| socket.on("error", err => events.push("error:" + err.code)); | ||
| socket.on("close", () => { events.push("close"); socketClosed(); }); | ||
| upgraded(); | ||
| }); | ||
| server.listen(0, "127.0.0.1"); | ||
| await once(server, "listening"); | ||
|
|
||
| const c = net.connect(server.address().port, "127.0.0.1"); | ||
| c.on("error", () => {}); | ||
| await once(c, "connect"); | ||
| c.write("GET / HTTP/1.1\\r\\nHost: x\\r\\nUpgrade: foo\\r\\nConnection: Upgrade\\r\\nContent-Length: 100\\r\\n\\r\\npartial"); | ||
| await gotUpgrade; | ||
| // Half-close the client; the body (100 bytes) is never completed. | ||
| c.end(); | ||
| await gotSocketClose; | ||
| c.destroy(); | ||
|
|
||
| // server.close() only resolves once pending_requests has reached zero. | ||
| await new Promise((resolve, reject) => { | ||
| server.close(err => err ? reject(err) : resolve()); | ||
| }); | ||
| console.log(JSON.stringify(events)); | ||
| process.exit(0); | ||
| })().catch(err => { console.error(err); process.exit(1); }); | ||
| `; | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", fixture], | ||
| env: bunEnv, | ||
| stderr: "pipe", | ||
| timeout: 20_000, | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect({ stdout: stdout.trim(), stderr, exitCode }).toEqual({ | ||
| stdout: JSON.stringify([ | ||
| "upgrade", | ||
| "end", | ||
| "write-cb:ERR_STREAM_WRITE_AFTER_END", | ||
| "error:ERR_STREAM_WRITE_AFTER_END", | ||
| "close", | ||
| ]), | ||
| stderr: expect.not.stringContaining("error"), | ||
| exitCode: 0, | ||
| }); | ||
| }, 30_000); | ||
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.