node/test/parallel/test-http2-trailers-after-session-close.js
Michael Lumish 93c4efeb25
http2: allow streams to complete gracefully after goaway
A detailed analysis of the cause of this bug is in my linked comment on
the corresponding issue. The primary fix is the new setImmediate call in
Http2Stream#_destroy, which prevents a re-entrant call into
Http2Session::SendPendingData when sending trailers after the
Http2Session has been shut down, allowing the trailer data to be flushed
properly before the socket is closed.

As a result of this change, writes can be initiated later in the
lifetime of the Http2Session. So, when a JSStreamSocket is used as the
underlying socket reference for an Http2Session, it needs to be able to
accept write calls after it is closed.

In addition, now that outgoing data can be flushed differently after a
session is closed, in two tests clients receive errors that they
previously did not receive. I believe the new errors are more correct,
so I changed the tests to match.

Fixes: https://github.com/nodejs/node/issues/42713
Refs: https://github.com/nodejs/node/issues/42713#issuecomment-1756140062
PR-URL: https://github.com/nodejs/node/pull/50202
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
2023-10-20 13:28:18 +00:00

52 lines
1.3 KiB
JavaScript

'use strict';
// Fixes: https://github.com/nodejs/node/issues/42713
const common = require('../common');
if (!common.hasCrypto) {
common.skip('missing crypto');
}
const assert = require('assert');
const http2 = require('http2');
const {
HTTP2_HEADER_PATH,
HTTP2_HEADER_STATUS,
HTTP2_HEADER_METHOD,
} = http2.constants;
const server = http2.createServer();
server.on('stream', common.mustCall((stream) => {
server.close();
stream.session.close();
stream.on('wantTrailers', common.mustCall(() => {
stream.sendTrailers({ xyz: 'abc' });
}));
stream.respond({ [HTTP2_HEADER_STATUS]: 200 }, { waitForTrailers: true });
stream.write('some data');
stream.end();
}));
server.listen(0, common.mustCall(() => {
const port = server.address().port;
const client = http2.connect(`http://localhost:${port}`);
client.socket.on('close', common.mustCall());
const req = client.request({
[HTTP2_HEADER_PATH]: '/',
[HTTP2_HEADER_METHOD]: 'POST',
});
req.end();
req.on('response', common.mustCall());
let data = '';
req.on('data', (chunk) => {
data += chunk;
});
req.on('end', common.mustCall(() => {
assert.strictEqual(data, 'some data');
}));
req.on('trailers', common.mustCall((headers) => {
assert.strictEqual(headers.xyz, 'abc');
}));
req.on('close', common.mustCall());
}));