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: add support for AbortSignal to http2Session.request #36070

Closed
Prev Previous commit
Next Next commit
http2: Goodify test
- Remove redundant socket check
- Add assertion that AbortSignal event listener gets added then removed.
  • Loading branch information
MadaraUchiha committed Nov 21, 2020
commit 05f68a26599724a561ab28f8a94aeb6c38dcce19
2 changes: 1 addition & 1 deletion lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1722,7 +1722,7 @@ class ClientHttp2Session extends Http2Session {
if (options.waitForTrailers)
stream[kState].flags |= STREAM_FLAGS_HAS_TRAILERS;

const signal = options.signal;
const { signal } = options;
if (signal) {
Copy link
Member

Choose a reason for hiding this comment

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

What happens if this is aborted already?

Copy link
Contributor Author

@MadaraUchiha MadaraUchiha Nov 19, 2020

Choose a reason for hiding this comment

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

That's an excellent question.

Looking at the DOM implementation, it appears like the expected behavior is to abort immediately. I shall add this (and a test) after my 🍕 :D

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it would need to abort immediately.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mcollina Done, PTALA.

Also @benjamingr this is likely something we'd want to do in http_client as well.

validateAbortSignal(signal, 'options.signal');
const listener = () => stream.destroy();
Expand Down
16 changes: 10 additions & 6 deletions test/parallel/test-http2-client-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ if (!common.hasCrypto)
const assert = require('assert');
const h2 = require('http2');
const { kSocket } = require('internal/http2/util');
const { kEvents } = require('internal/event_target');
const Countdown = require('../common/countdown');

{
Expand Down Expand Up @@ -177,20 +178,23 @@ const Countdown = require('../common/countdown');
server.listen(0, common.mustCall(() => {
const client = h2.connect(`http://localhost:${server.address().port}`);
client.on('close', common.mustCall());
const socket = client[kSocket];
socket.on('close', common.mustCall(() => {
assert(socket.destroyed);
}));

const req = client.request({}, { signal: controller.signal });
client.on('error', common.mustCall((err) => {
assert.strictEqual(err.code, 'ECONNREFUSED');
assert.strictEqual(signal[kEvents].get('abort'), undefined);
server.close();
}));

const { signal } = controller;
assert.strictEqual(signal[kEvents].get('abort'), undefined);

const req = client.request({}, { signal });

assert.strictEqual(req.aborted, false);
assert.strictEqual(req.destroyed, false);
assert.strictEqual(signal[kEvents].get('abort').size, 1);

controller.abort();

assert.strictEqual(req.aborted, false);
assert.strictEqual(req.destroyed, true);
req.on('close', common.mustCall(() => server.close()));
Expand Down