Skip to content

Commit a63fb37

Browse files
committed
rework test to better reproduce mentioned issue
1 parent a70ef63 commit a63fb37

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

lib/net.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,10 @@ Socket.prototype._final = function(cb) {
533533
debug('_final: not yet connected');
534534
if (!this._finalizingOnConnect) {
535535
this._finalizingOnConnect = true;
536-
this.once('connect', () => this._final(cb));
536+
this.once('connect', () => {
537+
this._final(cb);
538+
this._finalizingOnConnect = false;
539+
});
537540
}
538541
return;
539542
}
@@ -806,7 +809,10 @@ Socket.prototype.destroySoon = function() {
806809
this.destroy();
807810
else if (!this._destroyingOnFinish) {
808811
this._destroyingOnFinish = true;
809-
this.once('finish', this.destroy);
812+
this.once('finish', () => {
813+
this.destroy();
814+
this._destroyingOnFinish = false;
815+
});
810816
}
811817
};
812818

test/parallel/test-net-socket-not-duplicates-destroy-soon-listeners.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ const socket = new net.Socket();
99
socket.on('error', () => {
1010
// noop
1111
});
12-
socket.connect({ host: addresses.INVALID_HOST, port: 1234 });
13-
socket.destroySoon();
14-
socket.destroySoon();
15-
const finishListenersCount = socket.listeners('finish').length;
16-
const connectListenersCount = socket.listeners('connect').length;
17-
assert.strictEqual(finishListenersCount, 1);
18-
assert.strictEqual(connectListenersCount, 1);
12+
const connectOptions = { host: addresses.INVALID_HOST, port: 1234 };
13+
14+
socket.connect(connectOptions);
15+
socket.destroySoon(); // Adds "connect" and "finish" event listeners when socket has "writable" state
16+
socket.destroy(); // Makes imideditly socket again "writable"
17+
18+
socket.connect(connectOptions);
19+
socket.destroySoon(); // Should not duplicate "connect" and "finish" event listeners
20+
21+
assert.strictEqual(socket.listeners('finish').length, 1);
22+
assert.strictEqual(socket.listeners('connect').length, 1);

0 commit comments

Comments
 (0)