Skip to content

Commit 0c93b12

Browse files
addaleaxrvagg
authored andcommitted
tls: do not rely on 'drain' handlers in StreamWrap
`'drain'` event handlers may not be invoked if the stream is currently finishing. Instead, use the fact that we know when writes are active or not, and invoke the delayed shutdown handler from our own after-write callback. PR-URL: #24290 Refs: #24288 Refs: #24075 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]> Reviewed-By: Ouyang Yadong <[email protected]>
1 parent 5748e86 commit 0c93b12

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

lib/internal/wrap_js_stream.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const { ERR_STREAM_WRAP } = require('internal/errors').codes;
1111

1212
const kCurrentWriteRequest = Symbol('kCurrentWriteRequest');
1313
const kCurrentShutdownRequest = Symbol('kCurrentShutdownRequest');
14+
const kPendingShutdownRequest = Symbol('kPendingShutdownRequest');
1415

1516
function isClosing() { return this[owner_symbol].isClosing(); }
1617
function onreadstart() { return this[owner_symbol].readStart(); }
@@ -79,6 +80,7 @@ class JSStreamWrap extends Socket {
7980
this.stream = stream;
8081
this[kCurrentWriteRequest] = null;
8182
this[kCurrentShutdownRequest] = null;
83+
this[kPendingShutdownRequest] = null;
8284
this.readable = stream.readable;
8385
this.writable = stream.writable;
8486

@@ -115,8 +117,10 @@ class JSStreamWrap extends Socket {
115117
// Working around that on the native side is not quite trivial (yet?),
116118
// so for now that is supported here.
117119

118-
if (this[kCurrentWriteRequest] !== null)
119-
return this.once('drain', () => this.doShutdown(req));
120+
if (this[kCurrentWriteRequest] !== null) {
121+
this[kPendingShutdownRequest] = req;
122+
return 0;
123+
}
120124
assert.strictEqual(this[kCurrentWriteRequest], null);
121125
assert.strictEqual(this[kCurrentShutdownRequest], null);
122126
this[kCurrentShutdownRequest] = req;
@@ -189,6 +193,11 @@ class JSStreamWrap extends Socket {
189193
this[kCurrentWriteRequest] = null;
190194

191195
handle.finishWrite(req, errCode);
196+
if (this[kPendingShutdownRequest]) {
197+
const req = this[kPendingShutdownRequest];
198+
this[kPendingShutdownRequest] = null;
199+
this.doShutdown(req);
200+
}
192201
}
193202

194203
doClose(cb) {

0 commit comments

Comments
 (0)