diff options
author | Ouyang Yadong <oyydoibh@gmail.com> | 2018-10-14 20:13:03 +0800 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2018-10-21 08:56:24 +0200 |
commit | 517955a474877893751d71b33f9c02a21bc25000 (patch) | |
tree | ec4b88bed3396b188830207604042b66ecc60912 /lib | |
parent | beb0f03e78a8fab01169213aafeebf9fabf1db43 (diff) | |
download | android-node-v8-517955a474877893751d71b33f9c02a21bc25000.tar.gz android-node-v8-517955a474877893751d71b33f9c02a21bc25000.tar.bz2 android-node-v8-517955a474877893751d71b33f9c02a21bc25000.zip |
tls: close StreamWrap and its stream correctly
When sockets of the "net" module destroyed, they will call
`this._handle.close()` which will also emit EOF if not emitted
before. This feature makes sockets on the other side emit "end" and
"close" even though we haven't called `end()`. As `stream` of
`StreamWrap` are likely to be instances of `net.Socket`, calling
`destroy()` manually will avoid issues that don't properly close
wrapped connections.
Fixes: https://github.com/nodejs/node/issues/14605
PR-URL: https://github.com/nodejs/node/pull/23654
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/wrap_js_stream.js | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/internal/wrap_js_stream.js b/lib/internal/wrap_js_stream.js index e6de433676..7ca7ff8bf4 100644 --- a/lib/internal/wrap_js_stream.js +++ b/lib/internal/wrap_js_stream.js @@ -68,6 +68,12 @@ class JSStreamWrap extends Socket { if (this._handle) this._handle.emitEOF(); }); + // Some `Stream` don't pass `hasError` parameters when closed. + stream.once('close', () => { + // Errors emitted from `stream` have also been emitted to this instance + // so that we don't pass errors to `destroy()` again. + this.destroy(); + }); super({ handle, manualStart: true }); this.stream = stream; @@ -188,6 +194,14 @@ class JSStreamWrap extends Socket { doClose(cb) { const handle = this._handle; + // When sockets of the "net" module destroyed, they will call + // `this._handle.close()` which will also emit EOF if not emitted before. + // This feature makes sockets on the other side emit "end" and "close" + // even though we haven't called `end()`. As `stream` are likely to be + // instances of `net.Socket`, calling `stream.destroy()` manually will + // avoid issues that don't properly close wrapped connections. + this.stream.destroy(); + setImmediate(() => { // Should be already set by net.js assert.strictEqual(this._handle, null); |