Skip to content

Commit 2d3576b

Browse files
authored
fix(NODE-5097): set timeout on write and reset on message (#3590)
1 parent 33208b7 commit 2d3576b

File tree

7 files changed

+407
-196
lines changed

7 files changed

+407
-196
lines changed

src/cmap/connect.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ function makeConnection(options: MakeConnectionOptions, _callback: Callback<Stre
428428
}
429429
}
430430

431-
socket.setTimeout(socketTimeoutMS);
431+
socket.setTimeout(0);
432432
callback(undefined, socket);
433433
}
434434

src/cmap/connection.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,9 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
370370
this[kDelayedTimeoutId] = null;
371371
}
372372

373+
const socketTimeoutMS = this[kStream].timeout ?? 0;
374+
this[kStream].setTimeout(0);
375+
373376
// always emit the message, in case we are streaming
374377
this.emit('message', message);
375378
let operationDescription = this[kQueue].get(message.responseTo);
@@ -410,8 +413,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
410413
// back in the queue with the correct requestId and will resolve not being able
411414
// to find the next one via the responseTo of the next streaming hello.
412415
this[kQueue].set(message.requestId, operationDescription);
413-
} else if (operationDescription.socketTimeoutOverride) {
414-
this[kStream].setTimeout(this.socketTimeoutMS);
416+
this[kStream].setTimeout(socketTimeoutMS);
415417
}
416418

417419
try {
@@ -707,8 +709,9 @@ function write(
707709
}
708710

709711
if (typeof options.socketTimeoutMS === 'number') {
710-
operationDescription.socketTimeoutOverride = true;
711712
conn[kStream].setTimeout(options.socketTimeoutMS);
713+
} else if (conn.socketTimeoutMS !== 0) {
714+
conn[kStream].setTimeout(conn.socketTimeoutMS);
712715
}
713716

714717
// if command monitoring is enabled we need to modify the callback here

src/cmap/message_stream.ts

-1
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

+1-23
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { connect } from '../../../src/cmap/connect';
55
import { Connection } from '../../../src/cmap/connection';
66
import { LEGACY_HELLO_COMMAND } from '../../../src/constants';
77
import { Topology } from '../../../src/sdam/topology';
8-
import { HostAddress, ns } from '../../../src/utils';
8+
import { ns } from '../../../src/utils';
99
import { skipBrokenAuthTestBeforeEachHook } from '../../tools/runner/hooks/configuration';
1010
import { assert as test, setupDatabase } from '../shared';
1111

@@ -74,28 +74,6 @@ describe('Connection', function () {
7474
}
7575
});
7676

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

test/unit/cmap/connect.test.js

-165
This file was deleted.

0 commit comments

Comments
 (0)