@@ -26,6 +26,8 @@ import * as nodesUtils from '../nodes/utils';
2626import { promisify } from '../utils' ;
2727import { timedCancellable , context } from '../contexts' ;
2828
29+ const clientConnectionClosedReason = Symbol ( 'clientConnectionClosedReason' ) ;
30+
2931interface Proxy extends StartStop { }
3032@StartStop ( )
3133class 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