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: guard against destroy in socket timeout #15106

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ function onFrameError(id, type, code) {
function emitGoaway(state, code, lastStreamID, buf) {
this.emit('goaway', code, lastStreamID, buf);
// Tear down the session or destroy
if (state.destroying || state.destroyed)
return;
if (!state.shuttingDown && !state.shutdown) {
this.shutdown({}, this.destroy.bind(this));
} else {
Expand Down Expand Up @@ -970,7 +972,7 @@ class Http2Session extends EventEmitter {
state.destroying = true;

// Unenroll the timer
unenroll(this);
this.setTimeout(0);

// Shut down any still open streams
const streams = state.streams;
Expand Down Expand Up @@ -1530,7 +1532,7 @@ class Http2Stream extends Duplex {
session.removeListener('close', this[kState].closeHandler);

// Unenroll the timer
unenroll(this);
this.setTimeout(0);

setImmediate(finishStreamDestroy.bind(this, handle));

Expand Down Expand Up @@ -2180,7 +2182,7 @@ function socketDestroy(error) {
const type = this[kSession][kType];
debug(`[${sessionName(type)}] socket destroy called`);
delete this[kServer];
this.removeListener('timeout', socketOnTimeout);
this.setTimeout(0);
// destroy the session first so that it will stop trying to
// send data while we close the socket.
this[kSession].destroy();
Expand Down Expand Up @@ -2249,10 +2251,13 @@ function socketOnTimeout() {
process.nextTick(() => {
const server = this[kServer];
const session = this[kSession];
// If server or session are undefined, then we're already in the process of
// shutting down, do nothing.
// If server or session are undefined, or session.destroyed is true
// then we're already in the process of shutting down, do nothing.
if (server === undefined || session === undefined)
return;
const state = session[kState];
if (state.destroyed || state.destroying)
return;
if (!server.emit('timeout', session, this)) {
session.shutdown(
{
Expand Down
25 changes: 25 additions & 0 deletions test/parallel/test-http2-client-destroy-goaway.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Flags: --expose-http2
'use strict';

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const http2 = require('http2');

const server = http2.createServer();
server.on('stream', common.mustCall((stream) => {
stream.on('error', common.mustCall());
stream.session.shutdown();
}));

server.listen(0, common.mustCall(() => {
const client = http2.connect(`http://localhost:${server.address().port}`);

client.on('goaway', common.mustCall(() => {
// We ought to be able to destroy the client in here without an error
server.close();
client.destroy();
}));

client.request();
}));
5 changes: 3 additions & 2 deletions test/parallel/test-http2-server-shutdown-before-respond.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ server.on('listening', common.mustCall(() => {

const client = h2.connect(`http://localhost:${server.address().port}`);

const req = client.request({ ':path': '/' });
client.on('goaway', common.mustCall());

const req = client.request();

req.resume();
req.on('end', common.mustCall(() => server.close()));
req.end();
}));