Skip to content

Commit b3c83b5

Browse files
committed
feat: Proxy connections are cleaned up if client closes before composing
[ci skip]
1 parent 4a99008 commit b3c83b5

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/network/Proxy.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import * as nodesUtils from '../nodes/utils';
2626
import { promisify } from '../utils';
2727
import { timedCancellable, context } from '../contexts';
2828

29+
const clientConnectionClosedReason = Symbol('clientConnectionClosedReason');
30+
2931
interface Proxy extends StartStop {}
3032
@StartStop()
3133
class Proxy {
@@ -418,8 +420,9 @@ class Proxy {
418420
}
419421
await this.connectionLocksForward.withF([proxyAddress, Lock], async () => {
420422
const timer = new Timer({ delay: this.connConnectTime });
423+
let cleanUpConnectionListener = () => {};
421424
try {
422-
await this.connectForward(
425+
const connectForwardProm = this.connectForward(
423426
[nodeId],
424427
proxyHost,
425428
proxyPort,
@@ -428,6 +431,11 @@ class Proxy {
428431
timer,
429432
},
430433
);
434+
cleanUpConnectionListener = () => {
435+
connectForwardProm.cancel(clientConnectionClosedReason);
436+
};
437+
clientSocket.addListener('close', cleanUpConnectionListener);
438+
await connectForwardProm;
431439
} catch (e) {
432440
if (e instanceof networkErrors.ErrorProxyConnectInvalidUrl) {
433441
if (!clientSocket.destroyed) {
@@ -486,6 +494,7 @@ class Proxy {
486494
return;
487495
} finally {
488496
timer.cancel();
497+
clientSocket.removeListener('close', cleanUpConnectionListener);
489498
}
490499
// After composing, switch off this error handler
491500
clientSocket.off('error', handleConnectError);

0 commit comments

Comments
 (0)