Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3ad584c

Browse filesBrowse files
santigimenojuanarbol
authored andcommitted
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: #30841 PR-URL: #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>
1 parent 782b6f6 commit 3ad584c
Copy full SHA for 3ad584c

File tree

Expand file treeCollapse file tree

4 files changed

+43
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+43
-0
lines changed
Open diff view settings
Collapse file

‎doc/api/errors.md‎

Copy file name to clipboardExpand all lines: doc/api/errors.md
+8Lines changed: 8 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -2565,6 +2565,13 @@ could not be determined.
25652565

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

2568+
<a id="ERR_SOCKET_CLOSED_BEFORE_CONNECTION"></a>
2569+
2570+
### `ERR_SOCKET_CLOSED_BEFORE_CONNECTION`
2571+
2572+
When calling [`net.Socket.write()`][] on a connecting socket and the socket was
2573+
closed before the connection was established.
2574+
25682575
<a id="ERR_SOCKET_DGRAM_IS_CONNECTED"></a>
25692576

25702577
### `ERR_SOCKET_DGRAM_IS_CONNECTED`
@@ -3576,6 +3583,7 @@ The native call from `process.cpuUsage` could not be processed.
35763583
[`http`]: http.md
35773584
[`https`]: https.md
35783585
[`libuv Error handling`]: https://docs.libuv.org/en/v1.x/errors.html
3586+
[`net.Socket.write()`]: net.md#socketwritedata-encoding-callback
35793587
[`net`]: net.md
35803588
[`new URL(input)`]: url.md#new-urlinput-base
35813589
[`new URLSearchParams(iterable)`]: url.md#new-urlsearchparamsiterable
Collapse file

‎lib/internal/errors.js‎

Copy file name to clipboardExpand all lines: lib/internal/errors.js
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,9 @@ E('ERR_SOCKET_BUFFER_SIZE',
15661566
'Could not get or set buffer size',
15671567
SystemError);
15681568
E('ERR_SOCKET_CLOSED', 'Socket is closed', Error);
1569+
E('ERR_SOCKET_CLOSED_BEFORE_CONNECTION',
1570+
'Socket closed before the connection was established',
1571+
Error);
15691572
E('ERR_SOCKET_DGRAM_IS_CONNECTED', 'Already connected', Error);
15701573
E('ERR_SOCKET_DGRAM_NOT_CONNECTED', 'Not connected', Error);
15711574
E('ERR_SOCKET_DGRAM_NOT_RUNNING', 'Not running', Error);
Collapse file

‎lib/net.js‎

Copy file name to clipboardExpand all lines: lib/net.js
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ const {
9898
ERR_SERVER_ALREADY_LISTEN,
9999
ERR_SERVER_NOT_RUNNING,
100100
ERR_SOCKET_CLOSED,
101+
ERR_SOCKET_CLOSED_BEFORE_CONNECTION,
101102
ERR_MISSING_ARGS,
102103
},
103104
aggregateErrors,
@@ -903,8 +904,13 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
903904
this._pendingData = data;
904905
this._pendingEncoding = encoding;
905906
this.once('connect', function connect() {
907+
this.off('close', onClose);
906908
this._writeGeneric(writev, data, encoding, cb);
907909
});
910+
function onClose() {
911+
cb(new ERR_SOCKET_CLOSED_BEFORE_CONNECTION());
912+
}
913+
this.once('close', onClose);
908914
return;
909915
}
910916
this._pendingData = null;
Collapse file
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const net = require('net');
6+
7+
const server = net.createServer();
8+
server.listen(0, common.mustCall(() => {
9+
const socket = new net.Socket();
10+
11+
socket.on('connect', common.mustNotCall());
12+
13+
socket.connect({
14+
port: server.address().port,
15+
});
16+
17+
assert(socket.connecting);
18+
19+
socket.write('foo', common.expectsError({
20+
code: 'ERR_SOCKET_CLOSED_BEFORE_CONNECTION',
21+
name: 'Error'
22+
}));
23+
24+
socket.destroy();
25+
server.close();
26+
}));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.