Skip to content

Commit

Permalink
http2: check if stream is not destroyed before sending trailers
Browse files Browse the repository at this point in the history
Fixes: #22855
Backport-PR-URL: #22850
PR-URL: #22896
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
mcollina authored and BethGriggs committed Oct 16, 2018
1 parent 2de17ea commit 9f79341
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,14 @@ function afterShutdown() {
}

function finishSendTrailers(stream, headersList) {
// The stream might be destroyed and in that case
// there is nothing to do.
// This can happen because finishSendTrailers is
// scheduled via setImmediate.
if (stream.destroyed) {
return;
}

stream[kState].flags &= ~STREAM_FLAGS_HAS_TRAILERS;

const ret = stream[kHandle].trailers(headersList);
Expand Down
42 changes: 42 additions & 0 deletions test/parallel/test-http2-compat-socket-destroy-delayed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

const common = require('../common');
const { mustCall } = common;

if (!common.hasCrypto)
common.skip('missing crypto');

const http2 = require('http2');
const assert = require('assert');

const {
HTTP2_HEADER_PATH,
HTTP2_HEADER_METHOD,
} = http2.constants;

// This tests verifies that calling `req.socket.destroy()` via
// setImmediate does not crash.
// Fixes https://github.com/nodejs/node/issues/22855.

const app = http2.createServer(mustCall((req, res) => {
res.end('hello');
setImmediate(() => req.socket.destroy());
}));

app.listen(0, mustCall(() => {
const session = http2.connect(`http://localhost:${app.address().port}`);
const request = session.request({
[HTTP2_HEADER_PATH]: '/',
[HTTP2_HEADER_METHOD]: 'get'
});
request.once('response', mustCall((headers, flags) => {
let data = '';
request.on('data', (chunk) => { data += chunk; });
request.on('end', mustCall(() => {
assert.strictEqual(data, 'hello');
session.close();
app.close();
}));
}));
request.end();
}));

0 comments on commit 9f79341

Please sign in to comment.