Node: net: `write(cb)` not called if `destroy()`:ed before `'connect'`

Created on 7 Dec 2019  路  7Comments  路  Source: nodejs/node

See https://github.com/nodejs/node/pull/30839

_writeGeneric waits for 'connect' https://github.com/nodejs/node/blob/cf5ce2c9e1aab5eadbae107c697fdd11c6fb93a9/lib/net.js#L759 which might never be emitted due to https://github.com/nodejs/node/blob/cf5ce2c9e1aab5eadbae107c697fdd11c6fb93a9/lib/net.js#L1106-L1108

I believe this will fail:

const socket = createSocketBeforeConnect();
socket.write('asd', common.mustCall());
socket.destroy();
confirmed-bug net

All 7 comments

This is fixed

If the expectation is that callback is called this is not fixed.

const assert = require('assert');
const net = require('net');

const socket = new net.Socket();

socket.connect({
  port: 80,
  host: 'example.com',
  lookup() {}
});

assert(socket.connecting);

let called = false;

socket.write('foo', function() {
  called = true;
});

socket.destroy();

process.on('exit', function() {
  assert(called);
});

Yea, we should add a test for that.

This seems related to the whole constructing state which in this case never gets resolved, i.e. _write never finishes https://github.com/nodejs/node/blob/master/lib/net.js#L763. I can dig into this once/if https://github.com/nodejs/node/pull/29656 lands.

yes, _writeGeneric() is never called as 'connect' is never emitted.

pending resolution to discussion in https://github.com/nodejs/node/pull/31179

'use strict';

const common = require('../common');
const assert = require('assert');
const net = require('net');

const socket = new net.Socket();

socket.connect({
  port: 80,
  host: 'example.com',
  lookup() {}
});

assert(socket.connecting);

socket.write('foo', common.mustCall());
socket.destroy();

This requires https://github.com/nodejs/node/pull/29656 in order to be fully resolved.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fanjunzhi picture fanjunzhi  路  3Comments

danielstaleiny picture danielstaleiny  路  3Comments

srl295 picture srl295  路  3Comments

filipesilvaa picture filipesilvaa  路  3Comments

Icemic picture Icemic  路  3Comments