diff --git a/lib/internal/http2/compat.js b/lib/internal/http2/compat.js index 88879484ebaab7..14f20bc3d4583d 100644 --- a/lib/internal/http2/compat.js +++ b/lib/internal/http2/compat.js @@ -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); + // Check for the CONNECT method const method = headers[HTTP2_HEADER_METHOD]; if (method === 'CONNECT') { diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 39d456fb75905f..47e2408908b3f5 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -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; @@ -1551,6 +1552,8 @@ class Http2Stream extends Duplex { trailersReady: false }; + this[kStreamEventsComposite] = []; + this.on('pause', streamOnPause); } @@ -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; + } + } + + if (!hasTimeoutCallback) { + this.session.destroy(); + } } // true if the HEADERS frame has been sent @@ -2662,7 +2675,8 @@ function setupCompat(ev) { this.on('stream', onServerStream.bind( this, this[kOptions].Http2ServerRequest, - this[kOptions].Http2ServerResponse) + this[kOptions].Http2ServerResponse, + kStreamEventsComposite) ); } } diff --git a/test/parallel/test-http2-compat-serverrequest-settimeout.js b/test/parallel/test-http2-compat-serverrequest-settimeout.js index 81184d70752563..769486f6222d43 100644 --- a/test/parallel/test-http2-compat-serverrequest-settimeout.js +++ b/test/parallel/test-http2-compat-serverrequest-settimeout.js @@ -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()); + res.on('finish', common.mustCall(() => { req.setTimeout(msecs, common.mustNotCall()); process.nextTick(() => {