Skip to content
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

fix(ext/node): session close during stream setup #25170

Merged
merged 1 commit into from
Aug 30, 2024

Conversation

caleblloyd
Copy link
Contributor

@CLAassistant
Copy link

CLAassistant commented Aug 23, 2024

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@satyarohith Can you take another look at this?

@caleblloyd
Copy link
Contributor Author

@satyarohith need me to make any other changes to this PR?

@caleblloyd
Copy link
Contributor Author

The fix for stream closing was required to correctly implement the test, so that is why I put the fix and the test in the same PR as opposed to putting them in separate PRs

Copy link
Member

@satyarohith satyarohith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My concern with this patch is that it doesn't match node's behaviour exactly but improves it by propagating the error via "error" event instead of throwing it.

Logs:

➜ deno run -A test.mjs 
Script started
Connect callback triggered
'connect' event triggered
Connection established
Request created
Closing client
error: Uncaught (in promise) BadResource: Bad resource ID
    at clientHttp2Request (node:http2:638:16)
    at Object.runMicrotasks (ext:core/01_core.js:690:26)
    at processTicksAndRejections (ext:deno_node/_next_tick.ts:53:10)
    at runNextTicks (ext:deno_node/_next_tick.ts:71:3)
    at eventLoopTick (ext:core/01_core.js:181:21)
    at async node:http2:670:37

➜ denod run -A test.mjs # this PR
Script started
Connect callback triggered
'connect' event triggered
Connection established
Request created
Closing client
'close' event triggered on client
'error' event triggered on request: The pending stream has been canceled
'close' event triggered on request
Script finished

➜ node test.mjs 
Script started
Connect callback triggered
'connect' event triggered
Connection established
Request created
Closing client
'response' event triggered on request
'data' event triggered on request
'end' event triggered on request
'close' event triggered on request
Script finished
'close' event triggered on client
script
import http2 from "node:http2";

const url = "http://127.0.0.1:4246";
const connectPromise = Promise.withResolvers();

console.log("Script started");

const client = http2.connect(url, {}, () => {
  console.log("Connect callback triggered");
  connectPromise.resolve();
});

client.on('connect', () => console.log("'connect' event triggered"));
client.on('error', (err) => console.log("'error' event triggered on client:", err.message));
client.on('close', () => console.log("'close' event triggered on client"));

await connectPromise.promise;
console.log("Connection established");

// leave a request open to prevent immediate destroy
const req = client.request();
console.log("Request created");

req.on("response", (headers) => console.log("'response' event triggered on request"));
req.on("data", (chunk) => console.log("'data' event triggered on request"));
req.on("end", () => console.log("'end' event triggered on request"));
req.on("error", (err) => console.log("'error' event triggered on request:", err.message));
req.on("close", () => {
  console.log("'close' event triggered on request");
  reqClosePromise.resolve();
});

const reqClosePromise = Promise.withResolvers();

console.log("Closing client");
client.close();

await reqClosePromise.promise;
console.log("Script finished");

LGTM - we should match node's behaviour in future iterations.

Copy link
Member

@satyarohith satyarohith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution @caleblloyd!

@satyarohith satyarohith merged commit 4639ae6 into denoland:main Aug 30, 2024
17 checks passed
@caleblloyd
Copy link
Contributor Author

Thanks for the review! I agree, once session.close() implements graceful connection closing the pending request should attempt to be completed, and there should be no error on the request with the test case you posted.

lucacasonato pushed a commit that referenced this pull request Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HTTP2 request opened at same time Close is called throws
4 participants