Skip to content

Commit

Permalink
fix: timed out streams should be destroyed on timeout event
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Jan 4, 2020
1 parent 34fdea4 commit 5319ff9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/cmap/connection.js
Expand Up @@ -54,6 +54,10 @@ class Connection extends EventEmitter {
});

stream.on('close', () => {
if (this.closed) {
return;
}

this.closed = true;
this[kQueue].forEach(op =>
op.cb(new MongoNetworkError(`connection ${this.id} to ${this.address} closed`))
Expand All @@ -64,6 +68,11 @@ class Connection extends EventEmitter {
});

stream.on('timeout', () => {
if (this.closed) {
return;
}

stream.destroy();
this.closed = true;
this[kQueue].forEach(op =>
op.cb(new MongoNetworkError(`connection ${this.id} to ${this.address} timed out`))
Expand Down Expand Up @@ -106,6 +115,10 @@ class Connection extends EventEmitter {
return this[kClusterTime];
}

get stream() {
return this[kStream];
}

markAvailable() {
this[kLastUseTime] = Date.now();
}
Expand Down
30 changes: 30 additions & 0 deletions test/unit/cmap/connection.test.js
Expand Up @@ -38,4 +38,34 @@ describe('Connection', function() {
}
);
});

it('should destroy streams which time out', function(done) {
server.setMessageHandler(request => {
const doc = request.document;
if (doc.ismaster) {
request.reply(mock.DEFAULT_ISMASTER_36);
}

// blackhole all other requests
});

connect(
Object.assign({ bson: new BSON(), connectionType: Connection }, server.address()),
(err, conn) => {
expect(err).to.not.exist;
expect(conn).to.exist;

conn.command('$admin.cmd', { ping: 1 }, { socketTimeout: 50 }, (err, result) => {
expect(err).to.exist;
expect(result).to.not.exist;

expect(conn)
.property('stream')
.property('destroyed').to.be.true;

done();
});
}
);
});
});

0 comments on commit 5319ff9

Please sign in to comment.