Skip to content

Commit 6343179

Browse files
committed
net: fix race write() before and after connect()
Fixes #2827.
1 parent 692bcbe commit 6343179

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

lib/net.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,8 +617,6 @@ function afterConnect(status, handle, req, readable, writable) {
617617
handle.readStart();
618618
}
619619

620-
self.emit('connect');
621-
622620
if (self._connectQueue) {
623621
debug('Drain the connect queue');
624622
for (var i = 0; i < self._connectQueue.length; i++) {
@@ -627,6 +625,8 @@ function afterConnect(status, handle, req, readable, writable) {
627625
self._connectQueueCleanUp();
628626
}
629627

628+
self.emit('connect');
629+
630630
if (self._flags & FLAG_SHUTDOWNQUED) {
631631
// end called before connected - call end now with no data
632632
self._flags &= ~FLAG_SHUTDOWNQUED;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var net = require('net');
25+
26+
var received = '';
27+
28+
var server = net.createServer(function(socket) {
29+
socket.pipe(socket);
30+
}).listen(common.PORT, function() {
31+
var conn = net.connect(common.PORT);
32+
conn.setEncoding('utf8');
33+
conn.write('before');
34+
conn.on('connect', function() {
35+
conn.write('after');
36+
});
37+
conn.on('data', function(buf) {
38+
received += buf;
39+
conn.end();
40+
});
41+
conn.on('end', function() {
42+
server.close();
43+
});
44+
});
45+
46+
process.on('exit', function() {
47+
assert.equal(received, 'before' + 'after');
48+
});

0 commit comments

Comments
 (0)