Skip to content

Commit

Permalink
net: handle socket.write(cb) edge case
Browse files Browse the repository at this point in the history
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: nodejs#30841
PR-URL: nodejs#45922
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
Reviewed-By: Paolo Insogna <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
santigimeno authored Jan 1, 2023
1 parent 8ec1b08 commit a03529d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -2573,6 +2573,13 @@ could not be determined.

An attempt was made to operate on an already closed socket.

<a id="ERR_SOCKET_CLOSED_BEFORE_CONNECTION"></a>

### `ERR_SOCKET_CLOSED_BEFORE_CONNECTION`

When calling [`net.Socket.write()`][] on a connecting socket and the socket was
closed before the connection was established.

<a id="ERR_SOCKET_DGRAM_IS_CONNECTED"></a>

### `ERR_SOCKET_DGRAM_IS_CONNECTED`
Expand Down Expand Up @@ -3586,6 +3593,7 @@ The native call from `process.cpuUsage` could not be processed.
[`http`]: http.md
[`https`]: https.md
[`libuv Error handling`]: https://docs.libuv.org/en/v1.x/errors.html
[`net.Socket.write()`]: net.md#socketwritedata-encoding-callback
[`net`]: net.md
[`new URL(input)`]: url.md#new-urlinput-base
[`new URLSearchParams(iterable)`]: url.md#new-urlsearchparamsiterable
Expand Down
3 changes: 3 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,9 @@ E('ERR_SOCKET_BUFFER_SIZE',
'Could not get or set buffer size',
SystemError);
E('ERR_SOCKET_CLOSED', 'Socket is closed', Error);
E('ERR_SOCKET_CLOSED_BEFORE_CONNECTION',
'Socket closed before the connection was established',
Error);
E('ERR_SOCKET_DGRAM_IS_CONNECTED', 'Already connected', Error);
E('ERR_SOCKET_DGRAM_NOT_CONNECTED', 'Not connected', Error);
E('ERR_SOCKET_DGRAM_NOT_RUNNING', 'Not running', Error);
Expand Down
6 changes: 6 additions & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ const {
ERR_SERVER_ALREADY_LISTEN,
ERR_SERVER_NOT_RUNNING,
ERR_SOCKET_CLOSED,
ERR_SOCKET_CLOSED_BEFORE_CONNECTION,
ERR_MISSING_ARGS,
},
aggregateErrors,
Expand Down Expand Up @@ -897,8 +898,13 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
this._pendingData = data;
this._pendingEncoding = encoding;
this.once('connect', function connect() {
this.off('close', onClose);
this._writeGeneric(writev, data, encoding, cb);
});
function onClose() {
cb(new ERR_SOCKET_CLOSED_BEFORE_CONNECTION());
}
this.once('close', onClose);
return;
}
this._pendingData = null;
Expand Down
26 changes: 26 additions & 0 deletions test/parallel/test-net-write-cb-on-destroy-before-connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'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();
}));

0 comments on commit a03529d

Please sign in to comment.