node/test/parallel/test-net-write-cb-on-destroy-before-connect.js
Santiago Gimeno a03529d828
net: handle socket.write(cb) edge case
Make sure that when calling `write()` on a connecting socket, the
callback is called if the socket is destroyed before the connection is
established.

Fixes: https://github.com/nodejs/node/issues/30841
PR-URL: https://github.com/nodejs/node/pull/45922
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2023-01-01 23:07:34 +00:00

27 lines
525 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');
const server = net.createServer();
server.listen(0, common.mustCall(() => {
const socket = new net.Socket();
socket.on('connect', common.mustNotCall());
socket.connect({
port: server.address().port,
});
assert(socket.connecting);
socket.write('foo', common.expectsError({
code: 'ERR_SOCKET_CLOSED_BEFORE_CONNECTION',
name: 'Error'
}));
socket.destroy();
server.close();
}));