diff --git a/lib/internal/wrap_js_stream.js b/lib/internal/wrap_js_stream.js index 1c494e57e1f9dd..aed26ed8c6976a 100644 --- a/lib/internal/wrap_js_stream.js +++ b/lib/internal/wrap_js_stream.js @@ -137,7 +137,6 @@ class JSStreamWrap extends Socket { doWrite(req, bufs) { assert.strictEqual(this[kCurrentWriteRequest], null); assert.strictEqual(this[kCurrentShutdownRequest], null); - this[kCurrentWriteRequest] = req; const handle = this._handle; const self = this; @@ -149,6 +148,9 @@ class JSStreamWrap extends Socket { this.stream.write(bufs[i], done); this.stream.uncork(); + // Only set the request here, because the `write()` calls could throw. + this[kCurrentWriteRequest] = req; + function done(err) { if (!err && --pending !== 0) return;