@@ -3,10 +3,10 @@ import * as Docker from 'dockerode';
33import * as semver from 'semver' ;
44import { Mutex } from 'async-mutex' ;
55
6- import { delay } from '../../util/promise' ;
7-
86import { DOCKER_HOST_HOSTNAME , isImageAvailable } from './docker-commands' ;
97import { isDockerAvailable } from './docker-interception-services' ;
8+ import { delay } from '../../util/promise' ;
9+ import { reportError } from '../../error-tracking' ;
1010
1111const DOCKER_TUNNEL_IMAGE = "httptoolkit/docker-socks-tunnel:v1.1.0" ;
1212const DOCKER_TUNNEL_LABEL = "tech.httptoolkit.docker.tunnel" ;
@@ -113,12 +113,16 @@ export function ensureDockerTunnelRunning(proxyPort: number) {
113113 if ( ! _ . isObject ( portCache [ proxyPort ] ) && localTunnelPort ?. HostPort !== String ( portCache [ proxyPort ] ) ) {
114114 // If the tunnel port may be outdated (port changed, or missing, or container just started so
115115 // port here is undefined) then schedule an async tunnel port refresh:
116- portCache [ proxyPort ] = delay ( 10 ) . then ( ( ) => // Leave time for the port to bind
117- refreshDockerTunnelPortCache ( proxyPort , {
118- // Force, because otherwise we get into a loop here due to the delay().
119- force : true
120- } )
116+
117+ const refreshTunnelPort = delay ( 10 ) . then (
118+ ( ) => // Leave time for the port to bind
119+ refreshDockerTunnelPortCache ( proxyPort , {
120+ // Force, because otherwise we get into a loop here due to the delay().
121+ force : true
122+ } )
121123 ) ;
124+ portCache [ proxyPort ] = refreshTunnelPort ;
125+ refreshTunnelPort . catch ( reportError ) ;
122126 }
123127 } ) . finally ( ( ) => {
124128 // Clean up the promise, so that future calls to ensureRunning re-run this check.
@@ -225,10 +229,11 @@ export async function refreshDockerTunnelPortCache(proxyPort: number, { force }
225229 const localPort = _ . find ( portMappings , ( { HostIp } ) => HostIp === '127.0.0.1' ) ;
226230
227231 if ( ! localPort ) {
228- // This can happen if the networks of the container are changed manually. In some cases
229- // this can result in the mapping being lots . Kill & restart the container.
232+ // This can happen if the networks of the container are changed manually, which can lose some
233+ // mappings, or if the container is being shut down . Kill & restart the container:
230234 await docker . getContainer ( containerName ) . kill ( ) ;
231235 await ensureDockerTunnelRunning ( proxyPort ) ;
236+ await delay ( 10 ) ; // Wait for the port to bind after startup
232237 return refreshDockerTunnelPortCache ( proxyPort , { force : true } ) ;
233238 }
234239
0 commit comments