Skip to content

Commit

Permalink
fix(NODE-5097): set timeout on write and reset on message (#3590)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Mar 7, 2023
1 parent 33208b7 commit 2d3576b
Show file tree
Hide file tree
Showing 7 changed files with 407 additions and 196 deletions.
2 changes: 1 addition & 1 deletion src/cmap/connect.ts
Expand Up @@ -428,7 +428,7 @@ function makeConnection(options: MakeConnectionOptions, _callback: Callback<Stre
}
}

socket.setTimeout(socketTimeoutMS);
socket.setTimeout(0);
callback(undefined, socket);
}

Expand Down
9 changes: 6 additions & 3 deletions src/cmap/connection.ts
Expand Up @@ -370,6 +370,9 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
this[kDelayedTimeoutId] = null;
}

const socketTimeoutMS = this[kStream].timeout ?? 0;
this[kStream].setTimeout(0);

// always emit the message, in case we are streaming
this.emit('message', message);
let operationDescription = this[kQueue].get(message.responseTo);
Expand Down Expand Up @@ -410,8 +413,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
// back in the queue with the correct requestId and will resolve not being able
// to find the next one via the responseTo of the next streaming hello.
this[kQueue].set(message.requestId, operationDescription);
} else if (operationDescription.socketTimeoutOverride) {
this[kStream].setTimeout(this.socketTimeoutMS);
this[kStream].setTimeout(socketTimeoutMS);
}

try {
Expand Down Expand Up @@ -707,8 +709,9 @@ function write(
}

if (typeof options.socketTimeoutMS === 'number') {
operationDescription.socketTimeoutOverride = true;
conn[kStream].setTimeout(options.socketTimeoutMS);
} else if (conn.socketTimeoutMS !== 0) {
conn[kStream].setTimeout(conn.socketTimeoutMS);
}

// if command monitoring is enabled we need to modify the callback here
Expand Down
1 change: 0 additions & 1 deletion src/cmap/message_stream.ts
Expand Up @@ -36,7 +36,6 @@ export interface OperationDescription extends BSONSerializeOptions {
raw: boolean;
requestId: number;
session?: ClientSession;
socketTimeoutOverride?: boolean;
agreedCompressor?: CompressorName;
zlibCompressionLevel?: number;
$clusterTime?: Document;
Expand Down
Expand Up @@ -5,7 +5,7 @@ import { connect } from '../../../src/cmap/connect';
import { Connection } from '../../../src/cmap/connection';
import { LEGACY_HELLO_COMMAND } from '../../../src/constants';
import { Topology } from '../../../src/sdam/topology';
import { HostAddress, ns } from '../../../src/utils';
import { ns } from '../../../src/utils';
import { skipBrokenAuthTestBeforeEachHook } from '../../tools/runner/hooks/configuration';
import { assert as test, setupDatabase } from '../shared';

Expand Down Expand Up @@ -74,28 +74,6 @@ describe('Connection', function () {
}
});

it.skip('should support socket timeouts', {
// FIXME: NODE-2941
metadata: {
requires: {
os: '!win32' // 240.0.0.1 doesnt work for windows
}
},
test: function (done) {
const connectOptions = {
hostAddress: new HostAddress('240.0.0.1'),
connectionType: Connection,
connectionTimeout: 500
};

connect(connectOptions, err => {
expect(err).to.exist;
expect(err).to.match(/timed out/);
done();
});
}
});

it('should support calling back multiple times on exhaust commands', {
metadata: {
requires: { apiVersion: false, mongodb: '>=4.2.0', topology: ['single'] }
Expand Down
165 changes: 0 additions & 165 deletions test/unit/cmap/connect.test.js

This file was deleted.

0 comments on commit 2d3576b

Please sign in to comment.