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

http2: send RST code 8 on AbortController signal #48573

Merged
merged 1 commit into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -2318,10 +2318,17 @@ class Http2Stream extends Duplex {
// this stream's close and destroy operations.
// Previously, this always overrode a successful close operation code
// NGHTTP2_NO_ERROR (0) with sessionCode because the use of the || operator.
const code = (err != null ?
(sessionCode || NGHTTP2_INTERNAL_ERROR) :
(this.closed ? this.rstCode : sessionCode)
);
let code = this.closed ? this.rstCode : sessionCode;
if (err != null) {
if (sessionCode) {
code = sessionCode;
} else if (err instanceof AbortError) {
// Enables using AbortController to cancel requests with RST code 8.
code = NGHTTP2_CANCEL;
} else {
code = NGHTTP2_INTERNAL_ERROR;
}
}
const hasHandle = handle !== undefined;

if (!this.closed)
Expand Down
29 changes: 29 additions & 0 deletions test/parallel/test-http2-client-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,32 @@ const { getEventListeners } = require('events');
testH2ConnectAbort(false);
testH2ConnectAbort(true);
}

// Destroy ClientHttp2Stream with AbortSignal
{
const server = h2.createServer();
const controller = new AbortController();

server.on('stream', common.mustCall((stream) => {
stream.on('error', common.mustNotCall());
stream.on('close', common.mustCall(() => {
assert.strictEqual(stream.rstCode, h2.constants.NGHTTP2_CANCEL);
server.close();
}));
controller.abort();
}));
server.listen(0, common.mustCall(() => {
const client = h2.connect(`http://localhost:${server.address().port}`);
client.on('close', common.mustCall());

const { signal } = controller;
const req = client.request({}, { signal });
assert.strictEqual(getEventListeners(signal, 'abort').length, 1);
req.on('error', common.mustCall((err) => {
assert.strictEqual(err.code, 'ABORT_ERR');
assert.strictEqual(err.name, 'AbortError');
client.close();
}));
req.on('close', common.mustCall());
}));
}