-
Notifications
You must be signed in to change notification settings - Fork 5.1k
usockets(kqueue): close half-open sockets whose write side hit EV_EOF instead of spinning #37076
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
9 commits
Select commit
Hold shift + click to select a range
b49031b
usockets(kqueue): close half-open sockets whose write side hit EV_EOF…
cirospaciari 90907ef
test: assert fixture stderr is empty
cirospaciari 71548fe
[autofix.ci] apply automated fixes
autofix-ci[bot] 8379d50
yaml: allow needless_pass_by_value on bind_anchor
cirospaciari 17b5cb0
socket: allow clippy::unit_arg in uws_handlers
cirospaciari 07b0109
usockets: preserve read interest in us_internal_rearm_writable
cirospaciari 6bc53ff
test: add half-open peer-reset test matrix
cirospaciari 7c0b0e4
usockets: only skip the peer-gone rearm for paused sockets
cirospaciari 3319b55
usockets: adopt the read_eof latch and write-side EV_EOF error transl…
cirospaciari 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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| // Body of test/js/bun/test/parallel/test-net-half-open-peer-reset-*: a backpressured socket whose peer stops reading, FINs, then RSTs must close (with `end` at most once), not spin. | ||
| import { tls as tlsCert } from "../../../harness"; | ||
| import { once } from "node:events"; | ||
| import http from "node:http"; | ||
| import https from "node:https"; | ||
|
|
||
| const big = Buffer.alloc(4 * 1024 * 1024, 0x78); | ||
| const { key, cert } = tlsCert; | ||
| const upgradeReq = "GET / HTTP/1.1\r\nHost: x\r\nConnection: Upgrade\r\nUpgrade: websocket\r\n\r\n"; | ||
| const getReq = "GET / HTTP/1.1\r\nHost: x\r\n\r\n"; | ||
|
|
||
| export async function run(mode) { | ||
| const issued = Promise.withResolvers(); // victim has queued more than the kernel will buffer | ||
| const ended = Promise.withResolvers(); // victim saw the peer's FIN (half-open modes) | ||
| const closed = Promise.withResolvers(); // victim socket/request was torn down | ||
| let endCount = 0; | ||
| const onEnd = () => { | ||
| if (++endCount > 1) fail("end delivered " + endCount + " times"); | ||
| ended.resolve(); | ||
| }; | ||
| const fail = why => { | ||
| console.error("FAIL", why); | ||
| process.exit(1); | ||
| }; | ||
|
|
||
| // The peer is a raw Bun socket so FIN (shutdown) and RST (terminate) are exactly what hits the wire, in that order. | ||
| async function peer(port, { preface, useTls, waitForEnd }) { | ||
| const opened = Promise.withResolvers(); | ||
| const s = await Bun.connect({ | ||
| hostname: "127.0.0.1", | ||
| port, | ||
| allowHalfOpen: true, | ||
| tls: useTls ? { rejectUnauthorized: false } : false, | ||
| socket: { | ||
| open: s => (useTls ? undefined : opened.resolve(s)), | ||
| handshake: s => opened.resolve(s), | ||
| data() {}, | ||
| end() {}, | ||
| error() {}, | ||
| close() {}, | ||
| }, | ||
| }); | ||
| await opened.promise; | ||
| if (preface) s.write(preface); | ||
| s.flush(); | ||
| s.pause(); | ||
| await issued.promise; | ||
| s.shutdown(); | ||
| if (waitForEnd) await Promise.race([ended.promise, closed.promise]); | ||
| // The FIN→RST gap is where the bug lived; an immediate RST can overtake the FIN and just read as ECONNRESET. | ||
| await Bun.sleep(50); | ||
| s.terminate(); | ||
| const deadline = setTimeout(() => fail("still open 5s after peer reset"), 5000); | ||
| closed.promise.then(() => clearTimeout(deadline)); | ||
| } | ||
|
|
||
| switch (mode) { | ||
| case "bun-listen": | ||
| case "bun-listen-tls": { | ||
| const useTls = mode === "bun-listen-tls"; | ||
| using server = Bun.listen({ | ||
| hostname: "127.0.0.1", | ||
| port: 0, | ||
| allowHalfOpen: true, | ||
| tls: useTls ? { key, cert } : undefined, | ||
| socket: { | ||
| open(s) { | ||
| s.write(big); | ||
| issued.resolve(); | ||
| }, | ||
| data() {}, | ||
| drain(s) { | ||
| s.write(big); | ||
| }, | ||
| end: onEnd, | ||
| error() {}, | ||
| close: () => closed.resolve(""), | ||
| }, | ||
| }); | ||
| await peer(server.port, { useTls, waitForEnd: true }); | ||
| await closed.promise; | ||
| break; | ||
| } | ||
| case "node-http-upgrade": | ||
| case "node-https-upgrade": { | ||
| const useTls = mode === "node-https-upgrade"; | ||
| await using server = (useTls ? https : http).createServer(useTls ? { key, cert } : {}, (_q, res) => res.end()); | ||
| server.on("upgrade", (_req, socket) => { | ||
| socket.on("error", () => {}); | ||
| socket.on("close", () => closed.resolve("")); | ||
| socket.on("end", () => { | ||
| onEnd(); | ||
| socket.end(); | ||
| }); | ||
| socket.write("HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nUpgrade: websocket\r\n\r\n"); | ||
| for (let i = 0; i < 8; i++) socket.write(big); | ||
| issued.resolve(); | ||
| }); | ||
| server.listen(0, "127.0.0.1"); | ||
| await once(server, "listening"); | ||
| await peer(server.address().port, { preface: upgradeReq, useTls, waitForEnd: true }); | ||
| await closed.promise; | ||
| break; | ||
| } | ||
| case "node-http-response": | ||
| case "node-https-response": { | ||
| const useTls = mode === "node-https-response"; | ||
| await using server = (useTls ? https : http).createServer(useTls ? { key, cert } : {}, (_req, res) => { | ||
| res.on("close", () => closed.resolve("res")); | ||
| res.writeHead(200); | ||
| for (let i = 0; i < 8; i++) res.write(big); | ||
| issued.resolve(); | ||
| }); | ||
| server.listen(0, "127.0.0.1"); | ||
| await once(server, "listening"); | ||
| await peer(server.address().port, { preface: getReq, useTls, waitForEnd: false }); | ||
| await closed.promise; | ||
| break; | ||
| } | ||
| case "bun-serve": | ||
| case "bun-serve-tls": { | ||
| const useTls = mode === "bun-serve-tls"; | ||
| using server = Bun.serve({ | ||
| port: 0, | ||
| hostname: "127.0.0.1", | ||
| idleTimeout: 0, | ||
| tls: useTls ? { key, cert } : undefined, | ||
| fetch(req) { | ||
| req.signal.addEventListener("abort", () => closed.resolve("abort")); | ||
| let i = 0; | ||
| return new Response( | ||
| new ReadableStream({ | ||
| pull(ctrl) { | ||
| if (i++ < 64) ctrl.enqueue(big); | ||
| else ctrl.close(); | ||
| issued.resolve(); | ||
| }, | ||
| cancel: () => closed.resolve("cancel"), | ||
| }), | ||
| ); | ||
| }, | ||
| }); | ||
| await peer(server.port, { preface: getReq, useTls, waitForEnd: false }); | ||
| await closed.promise; | ||
| break; | ||
| } | ||
| case "fetch-upload": { | ||
| // Roles flipped: the server is the peer that stops reading / FINs / RSTs; fetch() is the victim with a pending body. | ||
| using server = Bun.listen({ | ||
| hostname: "127.0.0.1", | ||
| port: 0, | ||
| allowHalfOpen: true, | ||
| socket: { | ||
| async open(s) { | ||
| s.pause(); | ||
| await issued.promise; | ||
| s.shutdown(); | ||
| await Bun.sleep(50); | ||
| s.terminate(); | ||
| const deadline = setTimeout(() => fail("fetch still pending 5s after peer reset"), 5000); | ||
| closed.promise.then(() => clearTimeout(deadline)); | ||
| }, | ||
| data() {}, | ||
| end() {}, | ||
| error() {}, | ||
| close() {}, | ||
| }, | ||
| }); | ||
| let i = 0; | ||
| fetch(`http://127.0.0.1:${server.port}/`, { | ||
| method: "POST", | ||
| duplex: "half", | ||
| body: new ReadableStream({ | ||
| pull(c) { | ||
| if (i++ < 64) c.enqueue(big); | ||
| else c.close(); | ||
| issued.resolve(); | ||
| }, | ||
| }), | ||
| }).then( | ||
| r => | ||
| r.text().then( | ||
| () => closed.resolve("resolved"), | ||
| e => closed.resolve("body rejected " + e?.code), | ||
| ), | ||
| e => closed.resolve("rejected " + e?.code), | ||
| ); | ||
| await closed.promise; | ||
| break; | ||
| } | ||
| default: | ||
| fail("unknown mode " + mode); | ||
| } | ||
| console.log("closed", await closed.promise); | ||
| } |
3 changes: 3 additions & 0 deletions
3
test/js/bun/test/parallel/test-net-half-open-peer-reset-bun-listen-tls.mjs
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,3 @@ | ||
| // https://github.com/oven-sh/bun/pull/37077 | ||
| import { run } from "../../net/half-open-peer-reset.mjs"; | ||
| await run("bun-listen-tls"); |
3 changes: 3 additions & 0 deletions
3
test/js/bun/test/parallel/test-net-half-open-peer-reset-bun-listen.mjs
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,3 @@ | ||
| // https://github.com/oven-sh/bun/pull/37077 | ||
| import { run } from "../../net/half-open-peer-reset.mjs"; | ||
| await run("bun-listen"); |
3 changes: 3 additions & 0 deletions
3
test/js/bun/test/parallel/test-net-half-open-peer-reset-bun-serve-tls.mjs
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,3 @@ | ||
| // https://github.com/oven-sh/bun/pull/37077 | ||
| import { run } from "../../net/half-open-peer-reset.mjs"; | ||
| await run("bun-serve-tls"); |
Oops, something went wrong.
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.