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 5 commits
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
7 changes: 6 additions & 1 deletion lib/net.js
Expand Up @@ -30,6 +30,7 @@ const {
NumberParseInt,
ObjectDefineProperty,
ObjectSetPrototypeOf,
ReflectApply,
Symbol,
} = primordials;

Expand Down Expand Up @@ -434,6 +435,11 @@ 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 ReflectApply(
stream.Duplex.prototype.write, this, [chunk, encoding, cb]);
}

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

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

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

const server = net.createServer(common.mustCall(function(s) {
server.close();
s.end();
})).listen(0, 'localhost', common.mustCall(function() {
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());