Skip to content

Commit ed83f36

Browse files
authored
fix(NODE-6602): only wrap errors from SOCKS in network errors (#4347)
1 parent ed2bdbe commit ed83f36

File tree

2 files changed

+33
-38
lines changed

2 files changed

+33
-38
lines changed

src/cmap/connect.ts

+33-34
Original file line numberDiff line numberDiff line change
@@ -386,15 +386,35 @@ export async function makeSocket(options: MakeConnectionOptions): Promise<Stream
386386
if (existingSocket) {
387387
resolve(socket);
388388
} else {
389+
const start = performance.now();
389390
const connectEvent = useTLS ? 'secureConnect' : 'connect';
390391
socket
391392
.once(connectEvent, () => resolve(socket))
392-
.once('error', error => reject(connectionFailureError('error', error)))
393-
.once('timeout', () => reject(connectionFailureError('timeout')))
394-
.once('close', () => reject(connectionFailureError('close')));
393+
.once('error', cause =>
394+
reject(new MongoNetworkError(MongoError.buildErrorMessage(cause), { cause }))
395+
)
396+
.once('timeout', () => {
397+
reject(
398+
new MongoNetworkTimeoutError(
399+
`Socket '${connectEvent}' timed out after ${(performance.now() - start) | 0}ms (connectTimeoutMS: ${connectTimeoutMS})`
400+
)
401+
);
402+
})
403+
.once('close', () =>
404+
reject(
405+
new MongoNetworkError(
406+
`Socket closed after ${(performance.now() - start) | 0} during connection establishment`
407+
)
408+
)
409+
);
395410

396411
if (options.cancellationToken != null) {
397-
cancellationHandler = () => reject(connectionFailureError('cancel'));
412+
cancellationHandler = () =>
413+
reject(
414+
new MongoNetworkError(
415+
`Socket connection establishment was cancelled after ${(performance.now() - start) | 0}`
416+
)
417+
);
398418
options.cancellationToken.once('cancel', cancellationHandler);
399419
}
400420
}
@@ -447,9 +467,11 @@ async function makeSocks5Connection(options: MakeConnectionOptions): Promise<Str
447467

448468
socks ??= loadSocks();
449469

470+
let existingSocket: Stream;
471+
450472
try {
451473
// Then, establish the Socks5 proxy connection:
452-
const { socket } = await socks.SocksClient.createConnection({
474+
const connection = await socks.SocksClient.createConnection({
453475
existing_socket: rawSocket,
454476
timeout: options.connectTimeoutMS,
455477
command: 'connect',
@@ -466,35 +488,12 @@ async function makeSocks5Connection(options: MakeConnectionOptions): Promise<Str
466488
password: options.proxyPassword || undefined
467489
}
468490
});
469-
470-
// Finally, now treat the resulting duplex stream as the
471-
// socket over which we send and receive wire protocol messages:
472-
return await makeSocket({
473-
...options,
474-
existingSocket: socket,
475-
proxyHost: undefined
476-
});
477-
} catch (error) {
478-
throw connectionFailureError('error', error);
491+
existingSocket = connection.socket;
492+
} catch (cause) {
493+
throw new MongoNetworkError(MongoError.buildErrorMessage(cause), { cause });
479494
}
480-
}
481495

482-
function connectionFailureError(type: 'error', cause: Error): MongoNetworkError;
483-
function connectionFailureError(type: 'close' | 'timeout' | 'cancel'): MongoNetworkError;
484-
function connectionFailureError(
485-
type: 'error' | 'close' | 'timeout' | 'cancel',
486-
cause?: Error
487-
): MongoNetworkError {
488-
switch (type) {
489-
case 'error':
490-
return new MongoNetworkError(MongoError.buildErrorMessage(cause), { cause });
491-
case 'timeout':
492-
return new MongoNetworkTimeoutError('connection timed out');
493-
case 'close':
494-
return new MongoNetworkError('connection closed');
495-
case 'cancel':
496-
return new MongoNetworkError('connection establishment was cancelled');
497-
default:
498-
return new MongoNetworkError('unknown network error');
499-
}
496+
// Finally, now treat the resulting duplex stream as the
497+
// socket over which we send and receive wire protocol messages:
498+
return await makeSocket({ ...options, existingSocket, proxyHost: undefined });
500499
}

test/benchmarks/driverBench/index.js

-4
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,6 @@ benchmarkRunner
5959
]);
6060
const multiBench = average(Object.values(microBench.multiBench));
6161

62-
// ldjsonMultiFileUpload and ldjsonMultiFileExport cause connection errors.
63-
// While we investigate, we will use the last known good values:
64-
// https://spruce.mongodb.com/task/mongo_node_driver_next_performance_tests_run_spec_benchmark_tests_node_server_4bc3e500b6f0e8ab01f052c4a1bfb782d6a29b4e_f168e1328f821bbda265e024cc91ae54_24_11_18_15_37_24/logs?execution=0
65-
6662
const parallelBench = average([
6763
microBench.parallel.ldjsonMultiFileUpload,
6864
microBench.parallel.ldjsonMultiFileExport,

0 commit comments

Comments
 (0)