Skip to content

Commit 5319ff9

Browse files
committed
fix: timed out streams should be destroyed on timeout event
1 parent 34fdea4 commit 5319ff9

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

lib/cmap/connection.js

+13
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ class Connection extends EventEmitter {
5454
});
5555

5656
stream.on('close', () => {
57+
if (this.closed) {
58+
return;
59+
}
60+
5761
this.closed = true;
5862
this[kQueue].forEach(op =>
5963
op.cb(new MongoNetworkError(`connection ${this.id} to ${this.address} closed`))
@@ -64,6 +68,11 @@ class Connection extends EventEmitter {
6468
});
6569

6670
stream.on('timeout', () => {
71+
if (this.closed) {
72+
return;
73+
}
74+
75+
stream.destroy();
6776
this.closed = true;
6877
this[kQueue].forEach(op =>
6978
op.cb(new MongoNetworkError(`connection ${this.id} to ${this.address} timed out`))
@@ -106,6 +115,10 @@ class Connection extends EventEmitter {
106115
return this[kClusterTime];
107116
}
108117

118+
get stream() {
119+
return this[kStream];
120+
}
121+
109122
markAvailable() {
110123
this[kLastUseTime] = Date.now();
111124
}

test/unit/cmap/connection.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,34 @@ describe('Connection', function() {
3838
}
3939
);
4040
});
41+
42+
it('should destroy streams which time out', function(done) {
43+
server.setMessageHandler(request => {
44+
const doc = request.document;
45+
if (doc.ismaster) {
46+
request.reply(mock.DEFAULT_ISMASTER_36);
47+
}
48+
49+
// blackhole all other requests
50+
});
51+
52+
connect(
53+
Object.assign({ bson: new BSON(), connectionType: Connection }, server.address()),
54+
(err, conn) => {
55+
expect(err).to.not.exist;
56+
expect(conn).to.exist;
57+
58+
conn.command('$admin.cmd', { ping: 1 }, { socketTimeout: 50 }, (err, result) => {
59+
expect(err).to.exist;
60+
expect(result).to.not.exist;
61+
62+
expect(conn)
63+
.property('stream')
64+
.property('destroyed').to.be.true;
65+
66+
done();
67+
});
68+
}
69+
);
70+
});
4171
});

0 commit comments

Comments
 (0)