Skip to content

Commit 4a7b5ec

Browse files
authored
fix(NODE-4854): set timeout on write and reset on message (#3582)
1 parent 2484ea4 commit 4a7b5ec

File tree

7 files changed

+394
-218
lines changed

7 files changed

+394
-218
lines changed

src/cmap/connect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ function makeConnection(options: MakeConnectionOptions, _callback: Callback<Stre
430430
}
431431
}
432432

433-
socket.setTimeout(socketTimeoutMS);
433+
socket.setTimeout(0);
434434
callback(undefined, socket);
435435
}
436436

src/cmap/connection.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,9 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
332332
this[kDelayedTimeoutId] = null;
333333
}
334334

335+
const socketTimeoutMS = this[kStream].timeout ?? 0;
336+
this[kStream].setTimeout(0);
337+
335338
// always emit the message, in case we are streaming
336339
this.emit('message', message);
337340
let operationDescription = this[kQueue].get(message.responseTo);
@@ -372,8 +375,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
372375
// back in the queue with the correct requestId and will resolve not being able
373376
// to find the next one via the responseTo of the next streaming hello.
374377
this[kQueue].set(message.requestId, operationDescription);
375-
} else if (operationDescription.socketTimeoutOverride) {
376-
this[kStream].setTimeout(this.socketTimeoutMS);
378+
this[kStream].setTimeout(socketTimeoutMS);
377379
}
378380

379381
try {
@@ -699,8 +701,9 @@ function write(
699701
}
700702

701703
if (typeof options.socketTimeoutMS === 'number') {
702-
operationDescription.socketTimeoutOverride = true;
703704
conn[kStream].setTimeout(options.socketTimeoutMS);
705+
} else if (conn.socketTimeoutMS !== 0) {
706+
conn[kStream].setTimeout(conn.socketTimeoutMS);
704707
}
705708

706709
// if command monitoring is enabled we need to modify the callback here
@@ -710,7 +713,7 @@ function write(
710713
operationDescription.started = now();
711714
operationDescription.cb = (err, reply) => {
712715
// Command monitoring spec states that if ok is 1, then we must always emit
713-
// a command suceeded event, even if there's an error. Write concern errors
716+
// a command succeeded event, even if there's an error. Write concern errors
714717
// will have an ok: 1 in their reply.
715718
if (err && reply?.ok !== 1) {
716719
conn.emit(

src/cmap/message_stream.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ export interface OperationDescription extends BSONSerializeOptions {
3636
raw: boolean;
3737
requestId: number;
3838
session?: ClientSession;
39-
socketTimeoutOverride?: boolean;
4039
agreedCompressor?: CompressorName;
4140
zlibCompressionLevel?: number;
4241
$clusterTime?: Document;

test/integration/connection-monitoring-and-pooling/connection.test.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { expect } from 'chai';
33
import {
44
connect,
55
Connection,
6-
HostAddress,
76
LEGACY_HELLO_COMMAND,
87
MongoClient,
98
MongoServerError,
@@ -79,28 +78,6 @@ describe('Connection', function () {
7978
}
8079
});
8180

82-
it.skip('should support socket timeouts', {
83-
// FIXME: NODE-2941
84-
metadata: {
85-
requires: {
86-
os: '!win32' // 240.0.0.1 doesnt work for windows
87-
}
88-
},
89-
test: function (done) {
90-
const connectOptions = {
91-
hostAddress: new HostAddress('240.0.0.1'),
92-
connectionType: Connection,
93-
connectionTimeout: 500
94-
};
95-
96-
connect(connectOptions, err => {
97-
expect(err).to.exist;
98-
expect(err).to.match(/timed out/);
99-
done();
100-
});
101-
}
102-
});
103-
10481
it('should support calling back multiple times on exhaust commands', {
10582
metadata: {
10683
requires: { apiVersion: false, mongodb: '>=4.2.0', topology: ['single'] }

test/unit/cmap/connect.test.js

Lines changed: 0 additions & 188 deletions
This file was deleted.

0 commit comments

Comments
 (0)