Repository navigation
node:http2: refuse pushStream() and ping() after session.close() #43531
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
9
commits into
main
Choose a base branch
from
robobun/3eee54d8/http2-close-push-ping
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
9 commits
Select commit
Hold shift + click to select a range
423a90d
node:http2: refuse pushStream() and ping() after session.close()
robobun 105f6ee
node:http2: treat the TLS handshake as connecting and check the close…
robobun 4c81c81
node:http2: shorten the comments on the ping and connecting guards
robobun 861b380
node:http2: derive the client session's connected getter from connecting
robobun a5afa26
test(http2): move the session close tests to their own file
robobun c9f91f8
test(http2): assert only the getters node has in the TLS handshake test
robobun 46006ec
test(http2): reject the awaited promises on 'error' in the session cl…
robobun e13996c
ci: retrigger
robobun a03c282
node:http2: declare secureConnecting on the session socket types
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| // What a node:http2 session still accepts after close(), and while it is still connecting. | ||
| // node refuses pushStream() and ping() on a closed session, and cancels a ping() issued before | ||
| // the session is ready (for https, that is until the TLS handshake completes). | ||
| import { expect, it } from "bun:test"; | ||
| import http2 from "node:http2"; | ||
| import tls from "node:tls"; | ||
| import { TLS_CERT, TLS_OPTIONS } from "./http2-helpers"; | ||
|
|
||
| // Any 'error' on a tracked emitter rejects the awaited promise, so a failed connect or handshake | ||
| // reports its cause instead of a timeout. | ||
| function errorGuard(...emitters) { | ||
| const failure = Promise.withResolvers<never>(); | ||
| const watch = emitter => emitter.on("error", failure.reject); | ||
| for (const emitter of emitters) watch(emitter); | ||
| const settled = <T>(promise: Promise<T>) => Promise.race([promise, failure.promise]); | ||
| return { watch, settled }; | ||
| } | ||
|
|
||
| it("http2 pushStream() and ping() are refused after session.close(), like node", async () => { | ||
| const out = {}; | ||
| const serverDone = Promise.withResolvers(); | ||
| const server = http2.createServer(); | ||
| server.on("stream", stream => { | ||
| stream.on("error", () => {}); | ||
| const session = stream.session; | ||
| out.pushAllowedBefore = stream.pushAllowed; | ||
| session.close(); | ||
| out.sessionClosed = session.closed; | ||
| out.sessionDestroyed = session.destroyed; | ||
| out.pushAllowedAfter = stream.pushAllowed; | ||
| try { | ||
| stream.pushStream({ ":path": "/pushed" }, (err, pushed) => { | ||
| out.pushCallback = err ? err.code : "pushed"; | ||
| if (pushed) { | ||
| pushed.on("error", () => {}); | ||
| pushed.respond({ ":status": 200 }); | ||
| pushed.end(); | ||
| } | ||
| }); | ||
| out.pushStream = "no throw"; | ||
| } catch (e) { | ||
| out.pushStream = e.code; | ||
| } | ||
| // node checks pushAllowed before it validates the callback. | ||
| try { | ||
| stream.pushStream({ ":path": "/pushed" }); | ||
| out.pushStreamNoCallback = "no throw"; | ||
| } catch (e) { | ||
| out.pushStreamNoCallback = e.code; | ||
| } | ||
| out.pingReturn = session.ping((err, duration) => { | ||
| out.pingCallback = err ? err.code : "ok"; | ||
| out.pingDuration = duration; | ||
| serverDone.resolve(); | ||
| }); | ||
| out.pingCallbackRanSync = "pingCallback" in out; | ||
| stream.respond({ ":status": 200 }); | ||
| stream.end("ok"); | ||
| }); | ||
| await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)); | ||
| const client = http2.connect(`http://127.0.0.1:${server.address().port}`); | ||
| try { | ||
| const clientClosed = new Promise(resolve => client.once("close", resolve)); | ||
|
robobun marked this conversation as resolved.
Resolved
|
||
| client.on("stream", (pushed, headers) => { | ||
| out.clientGotPush = headers[":path"]; | ||
| pushed.on("error", () => {}); | ||
| pushed.resume(); | ||
| }); | ||
| const req = client.request({ ":path": "/" }); | ||
| const { settled } = errorGuard(client, req); | ||
| req.resume(); | ||
| await settled(new Promise(resolve => req.once("close", resolve))); | ||
| await settled(serverDone.promise); | ||
| await settled(clientClosed); | ||
| } finally { | ||
| client.destroy(); | ||
| server.close(); | ||
| } | ||
|
|
||
| expect(out).toEqual({ | ||
| pushAllowedBefore: true, | ||
| sessionClosed: true, | ||
| sessionDestroyed: false, | ||
| pushAllowedAfter: false, | ||
| pushStream: "ERR_HTTP2_PUSH_DISABLED", | ||
| pushStreamNoCallback: "ERR_HTTP2_PUSH_DISABLED", | ||
| pingReturn: undefined, | ||
| pingCallbackRanSync: false, | ||
| pingCallback: "ERR_HTTP2_PING_CANCEL", | ||
| pingDuration: undefined, | ||
| }); | ||
| }); | ||
|
|
||
| it("http2 client ping() after session.close() is cancelled on the next tick, like node", async () => { | ||
| const server = http2.createServer(); | ||
| const serverStream = Promise.withResolvers(); | ||
| server.on("stream", stream => { | ||
| stream.on("error", () => {}); | ||
| serverStream.resolve(stream); | ||
| }); | ||
| await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)); | ||
| const client = http2.connect(`http://127.0.0.1:${server.address().port}`); | ||
| try { | ||
| const clientClosed = new Promise(resolve => client.once("close", resolve)); | ||
| const { watch, settled } = errorGuard(client); | ||
| await settled(new Promise(resolve => client.once("connect", resolve))); | ||
| const req = client.request({ ":path": "/" }); | ||
| watch(req); | ||
| req.resume(); | ||
| const stream = await settled(serverStream.promise); | ||
|
|
||
| // The open request keeps the session alive, so close() marks it closed without destroying it. | ||
| client.close(); | ||
| expect(client.closed).toBe(true); | ||
| expect(client.destroyed).toBe(false); | ||
|
|
||
| const pingResult = Promise.withResolvers(); | ||
| let pingCallbackRan = false; | ||
| const pingReturn = client.ping((err, duration) => { | ||
| pingCallbackRan = true; | ||
| pingResult.resolve({ code: err?.code, duration }); | ||
| }); | ||
| expect(pingReturn).toBeUndefined(); | ||
| // The callback runs on the next tick, never synchronously. | ||
| expect(pingCallbackRan).toBe(false); | ||
| expect(await settled(pingResult.promise)).toEqual({ code: "ERR_HTTP2_PING_CANCEL", duration: undefined }); | ||
|
|
||
| stream.respond({ ":status": 200 }); | ||
| stream.end("ok"); | ||
| await settled(clientClosed); | ||
| } finally { | ||
| client.destroy(); | ||
| server.close(); | ||
| } | ||
| }); | ||
|
|
||
| it("http2 client ping() during the TLS handshake is cancelled, like node", async () => { | ||
| const server = http2.createSecureServer({ cert: TLS_CERT.cert, key: TLS_CERT.key }); | ||
| await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)); | ||
| const { port } = server.address(); | ||
| const socket = tls.connect({ host: "127.0.0.1", port, ALPNProtocols: ["h2"], ...TLS_OPTIONS }); | ||
| const client = http2.connect(`https://127.0.0.1:${port}`, { createConnection: () => socket }); | ||
| try { | ||
| const { settled } = errorGuard(client, socket); | ||
| const connected = new Promise(resolve => client.once("connect", resolve)); | ||
| // Wait for the TCP connect. The TLS handshake is still in flight. | ||
| await settled(new Promise(resolve => socket.once("connect", resolve))); | ||
| expect(socket.connecting).toBe(false); | ||
| expect(socket.secureConnecting).toBe(true); | ||
| expect(client.connecting).toBe(true); | ||
|
|
||
| const handshakePing = Promise.withResolvers(); | ||
| const handshakeReturn = client.ping(err => handshakePing.resolve(err?.code)); | ||
| expect(handshakeReturn).toBeUndefined(); | ||
| expect(await settled(handshakePing.promise)).toBe("ERR_HTTP2_PING_CANCEL"); | ||
|
|
||
| await settled(connected); | ||
| expect(client.connecting).toBe(false); | ||
| const connectedPing = Promise.withResolvers(); | ||
| const connectedReturn = client.ping((err, duration) => connectedPing.resolve(err ? err.code : typeof duration)); | ||
| expect(connectedReturn).toBe(true); | ||
| expect(await settled(connectedPing.promise)).toBe("number"); | ||
| } finally { | ||
| client.destroy(); | ||
| server.close(); | ||
| } | ||
| }); | ||
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.