Skip to content

Commit

Permalink
net: Fix invalid write after end error
Browse files Browse the repository at this point in the history
Don't error if not ended.

Fixes: #36029
  • Loading branch information
ronag committed Nov 8, 2020
1 parent adae822 commit 1f73c8a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/net.js
Expand Up @@ -434,6 +434,10 @@ function afterShutdown() {
// of the other side sending a FIN. The standard 'write after end'
// is overly vague, and makes it seem like the user's code is to blame.
function writeAfterFIN(chunk, encoding, cb) {
if (!this.writabledEnded) {
return stream.Duplex.prototype.write.call(this, chunk, encoding, cb);
}

if (typeof encoding === 'function') {
cb = encoding;
encoding = null;
Expand Down Expand Up @@ -947,7 +951,6 @@ Socket.prototype.connect = function(...args) {
this._unrefTimer();

this.connecting = true;
this.writable = true;

if (pipe) {
validateString(path, 'options.path');
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-net-writable.js
@@ -0,0 +1,16 @@
'use strict';
const common = require('../common');
const assert = require('assert');

const net = require('net');

net.createServer(function(s) {
this.close();
s.end();
}).listen(0, 'localhost', function() {
const socket = net.connect(this.address().port, 'localhost');
socket.on('end', common.mustCall(() => {
assert.strictEqual(socket.writable, true);
socket.write('hello world');
}));
});
20 changes: 20 additions & 0 deletions test/parallel/test-stream-transform-end.js
@@ -0,0 +1,20 @@
'use strict';
const common = require('../common');
const { Readable, Transform } = require('stream');

const src = new Readable({
read() {
this.push(Buffer.alloc(20000));
}
});

const dst = new Transform({
transform(chunk, output, fn) {
this.push(null);
fn();
}
});

src.pipe(dst);
dst.resume();
dst.once('end', common.mustCall());

0 comments on commit 1f73c8a

Please sign in to comment.