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: fix missing 'timeout' event emit in request and response #20918

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
5 changes: 4 additions & 1 deletion lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,12 +705,15 @@ class Http2ServerResponse extends Stream {
}
}

function onServerStream(ServerRequest, ServerResponse,
function onServerStream(ServerRequest, ServerResponse, kStreamEventsComposite,
stream, headers, flags, rawHeaders) {
const server = this;
const request = new ServerRequest(stream, headers, undefined, rawHeaders);
const response = new ServerResponse(stream);

stream[kStreamEventsComposite].push(request);
stream[kStreamEventsComposite].push(response);
Copy link
Member

Choose a reason for hiding this comment

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

Instead of adding things to an array, I would add an event listener to 'timeout' on the stream.

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 as I described in the 2nd comment here, if we add a timeout listener to an EventEmitter, the checks with if (...emit(...)) will not work, because they all are always true then.


// Check for the CONNECT method
const method = headers[HTTP2_HEADER_METHOD];
if (method === 'CONNECT') {
Expand Down
18 changes: 16 additions & 2 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ const kState = Symbol('state');
const kType = Symbol('type');
const kUpdateTimer = Symbol('update-timer');
const kWriteGeneric = Symbol('write-generic');
const kStreamEventsComposite = Symbol('streamEventsComposite');

const kDefaultSocketTimeout = 2 * 60 * 1000;

Expand Down Expand Up @@ -1551,6 +1552,8 @@ class Http2Stream extends Duplex {
trailersReady: false
};

this[kStreamEventsComposite] = [];

this.on('pause', streamOnPause);
}

Expand Down Expand Up @@ -1640,7 +1643,17 @@ class Http2Stream extends Duplex {
}
}

this.emit('timeout');
// if there is no timeout event listener, destroy session
let hasTimeoutCallback = this.emit('timeout');
for (const component of this[kStreamEventsComposite]) {
if (component.emit('timeout')) {
hasTimeoutCallback = true;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

I would just check if if(!this.emit('timeout')) { this.session.destroy() }. It would need a test on the change in http2 core behavior.

Copy link
Contributor Author

@DaAitch DaAitch May 31, 2018

Choose a reason for hiding this comment

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

@mcollina I think, this is not what the docs are saying. We have 3 EventEmitters (req, res, stream) and if all of them return false, which means "there is no timeout listener" (according to the docs), then we can destroy the session.


if (!hasTimeoutCallback) {
this.session.destroy();
}
}

// true if the HEADERS frame has been sent
Expand Down Expand Up @@ -2662,7 +2675,8 @@ function setupCompat(ev) {
this.on('stream', onServerStream.bind(
this,
this[kOptions].Http2ServerRequest,
this[kOptions].Http2ServerResponse)
this[kOptions].Http2ServerResponse,
kStreamEventsComposite)
);
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-http2-compat-serverrequest-settimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ server.on('request', (req, res) => {
req.setTimeout(msecs, common.mustCall(() => {
res.end();
}));

res.on('timeout', common.mustCall());
req.on('timeout', common.mustCall());
Copy link
Member

Choose a reason for hiding this comment

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

Can you please add some assertions that verifies that 'timeout'  is not emitted?

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 sure I can. You mean between the setTimeout call and its callback, there should be no timeout events on req and res?


res.on('finish', common.mustCall(() => {
req.setTimeout(msecs, common.mustNotCall());
process.nextTick(() => {
Expand Down