diff options
author | Denys Otrishko <shishugi@gmail.com> | 2019-12-08 14:23:19 +0300 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2019-12-25 13:07:04 +0300 |
commit | be3c7aceba58f0a58ee4813a9894681d9c8ba56e (patch) | |
tree | 5bca75734b6ebde18202b43d37c88b4d97a0698d /lib | |
parent | a8c2c667ce896f3b168e8622eefa878b355d7c9a (diff) |
http2: wait for session socket writable end on close/destroy
This slightly alters the behaviour of session close by first using
.end() on a session socket to finish writing the data and only then
calls .destroy() to make sure the Readable side is closed. This allows
the socket to finish transmitting data, receive proper FIN packet and
avoid ECONNRESET errors upon graceful close.
onStreamClose now directly calls stream.destroy() instead of
kMaybeDestroy because the latter will first check that the stream has
writableFinished set. And that may not be true as we have just
(synchronously) called .end() on the stream if it was not closed and
that doesn't give it enough time to finish. Furthermore there is no
point in waiting for 'finish' as the other party have already closed the
stream and we won't be able to write anyway.
This also changes a few tests to correctly handle graceful session
close. This includes:
* not reading request data (on client side)
* not reading push stream data (on client side)
* relying on socket.destroy() (on client) to finish server session
due to the destroy of the socket without closing the server session.
As the goaway itself is *not* a session close.
Added few 'close' event mustCall checks.
PR-URL: https://github.com/nodejs/node/pull/30854
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/http2/core.js | 107 |
1 files changed, 71 insertions, 36 deletions
diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 3376dc97d2e..7e3f217617c 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -488,7 +488,10 @@ function onStreamClose(code) { if (!stream || stream.destroyed) return false; - debugStreamObj(stream, 'closed with code %d', code); + debugStreamObj( + stream, 'closed with code %d, closed %s, readable %s', + code, stream.closed, stream.readable + ); if (!stream.closed) closeStream(stream, code, kNoRstStream); @@ -497,7 +500,7 @@ function onStreamClose(code) { // Defer destroy we actually emit end. if (!stream.readable || code !== NGHTTP2_NO_ERROR) { // If errored or ended, we can destroy immediately. - stream[kMaybeDestroy](code); + stream.destroy(); } else { // Wait for end to destroy. stream.on('end', stream[kMaybeDestroy]); @@ -985,22 +988,76 @@ function emitClose(self, error) { self.emit('close'); } -function finishSessionDestroy(session, error) { - debugSessionObj(session, 'finishSessionDestroy'); - +function cleanupSession(session) { const socket = session[kSocket]; - if (!socket.destroyed) - socket.destroy(error); - + const handle = session[kHandle]; session[kProxySocket] = undefined; session[kSocket] = undefined; session[kHandle] = undefined; session[kNativeFields] = new Uint8Array(kSessionUint8FieldCount); - socket[kSession] = undefined; - socket[kServer] = undefined; + if (handle) + handle.ondone = null; + if (socket) { + socket[kSession] = undefined; + socket[kServer] = undefined; + } +} + +function finishSessionClose(session, error) { + debugSessionObj(session, 'finishSessionClose'); + + const socket = session[kSocket]; + cleanupSession(session); + + if (socket && !socket.destroyed) { + // Always wait for writable side to finish. + socket.end((err) => { + debugSessionObj(session, 'finishSessionClose socket end', err); + // Due to the way the underlying stream is handled in Http2Session we + // won't get graceful Readable end from the other side even if it was sent + // as the stream is already considered closed and will neither be read + // from nor keep the event loop alive. + // Therefore destroy the socket immediately. + // Fixing this would require some heavy juggling of ReadStart/ReadStop + // mostly on Windows as on Unix it will be fine with just ReadStart + // after this 'ondone' callback. + socket.destroy(error); + emitClose(session, error); + }); + } else { + process.nextTick(emitClose, session, error); + } +} + +function closeSession(session, code, error) { + debugSessionObj(session, 'start closing/destroying'); + + const state = session[kState]; + state.flags |= SESSION_FLAGS_DESTROYED; + state.destroyCode = code; + + // Clear timeout and remove timeout listeners. + session.setTimeout(0); + session.removeAllListeners('timeout'); + + // Destroy any pending and open streams + if (state.pendingStreams.size > 0 || state.streams.size > 0) { + const cancel = new ERR_HTTP2_STREAM_CANCEL(error); + state.pendingStreams.forEach((stream) => stream.destroy(cancel)); + state.streams.forEach((stream) => stream.destroy(error)); + } - // Finally, emit the close and error events (if necessary) on next tick. - process.nextTick(emitClose, session, error); + // Disassociate from the socket and server. + const socket = session[kSocket]; + const handle = session[kHandle]; + + // Destroy the handle if it exists at this point. + if (handle !== undefined) { + handle.ondone = finishSessionClose.bind(null, session, error); + handle.destroy(code, socket.destroyed); + } else { + finishSessionClose(session, error); + } } // Upon creation, the Http2Session takes ownership of the socket. The session @@ -1327,6 +1384,7 @@ class Http2Session extends EventEmitter { destroy(error = NGHTTP2_NO_ERROR, code) { if (this.destroyed) return; + debugSessionObj(this, 'destroying'); if (typeof error === 'number') { @@ -1338,30 +1396,7 @@ class Http2Session extends EventEmitter { if (code === undefined && error != null) code = NGHTTP2_INTERNAL_ERROR; - const state = this[kState]; - state.flags |= SESSION_FLAGS_DESTROYED; - state.destroyCode = code; - - // Clear timeout and remove timeout listeners - this.setTimeout(0); - this.removeAllListeners('timeout'); - - // Destroy any pending and open streams - const cancel = new ERR_HTTP2_STREAM_CANCEL(error); - state.pendingStreams.forEach((stream) => stream.destroy(cancel)); - state.streams.forEach((stream) => stream.destroy(error)); - - // Disassociate from the socket and server - const socket = this[kSocket]; - const handle = this[kHandle]; - - // Destroy the handle if it exists at this point - if (handle !== undefined) { - handle.ondone = finishSessionDestroy.bind(null, this, error); - handle.destroy(code, socket.destroyed); - } else { - finishSessionDestroy(this, error); - } + closeSession(this, code, error); } // Closing the session will: |