Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: Fix invalid write after end error #36043

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
ronag marked this conversation as resolved.
Show resolved Hide resolved
}

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) {
ronag marked this conversation as resolved.
Show resolved Hide resolved
this.close();
s.end();
}).listen(0, 'localhost', function() {
ronag marked this conversation as resolved.
Show resolved Hide resolved
const socket = net.connect(this.address().port, 'localhost');
socket.on('end', common.mustCall(() => {
assert.strictEqual(socket.writable, true);
lpinca marked this conversation as resolved.
Show resolved Hide resolved
socket.write('hello world');
}));
});
20 changes: 20 additions & 0 deletions test/parallel/test-stream-transform-end.js
@@ -0,0 +1,20 @@
'use strict';
ronag marked this conversation as resolved.
Show resolved Hide resolved
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());