Skip to content

Commit 02955d5

Browse files
committed
chore: revert socket dupe clean up
1 parent 0b41b6d commit 02955d5

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/client-side-encryption/state_machine.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,16 @@ export class StateMachine {
338338
const message = request.message;
339339
const buffer = new BufferPool();
340340

341-
const socket = new net.Socket();
341+
const netSocket: net.Socket = new net.Socket();
342+
let socket: tls.TLSSocket;
343+
344+
function destroySockets() {
345+
for (const sock of [socket, netSocket]) {
346+
if (sock) {
347+
sock.destroy();
348+
}
349+
}
350+
}
342351

343352
function onerror(cause: Error) {
344353
return new MongoCryptError('KMS request failed', { cause });
@@ -370,8 +379,7 @@ export class StateMachine {
370379
reject: rejectOnNetSocketError,
371380
resolve: resolveOnNetSocketConnect
372381
} = promiseWithResolvers<void>();
373-
374-
socket
382+
netSocket
375383
.once('error', err => rejectOnNetSocketError(onerror(err)))
376384
.once('close', () => rejectOnNetSocketError(onclose()))
377385
.once('connect', () => resolveOnNetSocketConnect());
@@ -385,14 +393,14 @@ export class StateMachine {
385393
host: this.options.proxyOptions.proxyHost,
386394
port: this.options.proxyOptions.proxyPort || 1080
387395
};
388-
socketOptions.socket = socket.connect(netSocketOptions);
396+
netSocket.connect(netSocketOptions);
389397
await willConnect;
390398

391399
try {
392400
socks ??= loadSocks();
393401
socketOptions.socket = (
394402
await socks.SocksClient.createConnection({
395-
existing_socket: socket,
403+
existing_socket: netSocket,
396404
command: 'connect',
397405
destination: { host: socketOptions.host, port: socketOptions.port },
398406
proxy: {
@@ -410,7 +418,7 @@ export class StateMachine {
410418
}
411419
}
412420

413-
tls.connect(socketOptions, () => {
421+
socket = tls.connect(socketOptions, () => {
414422
socket.write(message);
415423
});
416424

@@ -421,7 +429,7 @@ export class StateMachine {
421429
} = promiseWithResolvers<void>();
422430

423431
abortListener = addAbortListener(options?.signal, function () {
424-
socket.destroy();
432+
destroySockets();
425433
rejectOnTlsSocketError(this.reason);
426434
});
427435

@@ -446,13 +454,12 @@ export class StateMachine {
446454
])
447455
: willResolveKmsRequest);
448456
} catch (error) {
449-
if (TimeoutError.is(error)) {
457+
if (error instanceof TimeoutError)
450458
throw new MongoOperationTimeoutError('KMS request timed out');
451-
}
452459
throw error;
453460
} finally {
454461
// There's no need for any more activity on this socket at this point.
455-
socket.destroy();
462+
destroySockets();
456463
abortListener?.[kDispose]();
457464
}
458465
}

0 commit comments

Comments
 (0)