Skip to content

Commit db79d2b

Browse files
James HartigColin Ihrig
authored andcommitted
net: use cached peername to resolve remote props
Allows socket.remote* properties to still be accessed even after the socket is closed. Fixes nodejs#9287 Reviewed-By: Colin Ihrig <[email protected]> PR-URL: nodejs#9366
1 parent a995a6a commit db79d2b

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/net.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -579,10 +579,10 @@ function onread(nread, buffer) {
579579

580580

581581
Socket.prototype._getpeername = function() {
582-
if (!this._handle || !this._handle.getpeername) {
583-
return {};
584-
}
585582
if (!this._peername) {
583+
if (!this._handle || !this._handle.getpeername) {
584+
return {};
585+
}
586586
var out = {};
587587
var err = this._handle.getpeername(out);
588588
if (err) return {}; // FIXME(bnoordhuis) Throw?
@@ -868,6 +868,7 @@ Socket.prototype.connect = function(options, cb) {
868868
this._writableState.errorEmitted = false;
869869
this.destroyed = false;
870870
this._handle = null;
871+
this._peername = null;
871872
}
872873

873874
var self = this;

test/simple/test-net-remote-address-port.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ var server = net.createServer(function(socket) {
4141
socket.on('end', function() {
4242
if (++conns_closed == 2) server.close();
4343
});
44+
socket.on('close', function() {
45+
assert.notEqual(-1, remoteAddrCandidates.indexOf(socket.remoteAddress));
46+
assert.notEqual(-1, remoteFamilyCandidates.indexOf(socket.remoteFamily));
47+
});
4448
socket.resume();
4549
});
4650

@@ -53,12 +57,20 @@ server.listen(common.PORT, 'localhost', function() {
5357
assert.equal(common.PORT, client.remotePort);
5458
client.end();
5559
});
60+
client.on('close', function() {
61+
assert.notEqual(-1, remoteAddrCandidates.indexOf(client.remoteAddress));
62+
assert.notEqual(-1, remoteFamilyCandidates.indexOf(client.remoteFamily));
63+
});
5664
client2.on('connect', function() {
5765
assert.notEqual(-1, remoteAddrCandidates.indexOf(client2.remoteAddress));
5866
assert.notEqual(-1, remoteFamilyCandidates.indexOf(client2.remoteFamily));
5967
assert.equal(common.PORT, client2.remotePort);
6068
client2.end();
6169
});
70+
client2.on('close', function() {
71+
assert.notEqual(-1, remoteAddrCandidates.indexOf(client2.remoteAddress));
72+
assert.notEqual(-1, remoteFamilyCandidates.indexOf(client2.remoteFamily));
73+
});
6274
});
6375

6476
process.on('exit', function() {

0 commit comments

Comments
 (0)