@@ -3,10 +3,10 @@ import * as Docker from 'dockerode';
3
3
import * as semver from 'semver' ;
4
4
import { Mutex } from 'async-mutex' ;
5
5
6
- import { delay } from '../../util/promise' ;
7
-
8
6
import { DOCKER_HOST_HOSTNAME , isImageAvailable } from './docker-commands' ;
9
7
import { isDockerAvailable } from './docker-interception-services' ;
8
+ import { delay } from '../../util/promise' ;
9
+ import { reportError } from '../../error-tracking' ;
10
10
11
11
const DOCKER_TUNNEL_IMAGE = "httptoolkit/docker-socks-tunnel:v1.1.0" ;
12
12
const DOCKER_TUNNEL_LABEL = "tech.httptoolkit.docker.tunnel" ;
@@ -113,12 +113,16 @@ export function ensureDockerTunnelRunning(proxyPort: number) {
113
113
if ( ! _ . isObject ( portCache [ proxyPort ] ) && localTunnelPort ?. HostPort !== String ( portCache [ proxyPort ] ) ) {
114
114
// If the tunnel port may be outdated (port changed, or missing, or container just started so
115
115
// 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
+ } )
121
123
) ;
124
+ portCache [ proxyPort ] = refreshTunnelPort ;
125
+ refreshTunnelPort . catch ( reportError ) ;
122
126
}
123
127
} ) . finally ( ( ) => {
124
128
// 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 }
225
229
const localPort = _ . find ( portMappings , ( { HostIp } ) => HostIp === '127.0.0.1' ) ;
226
230
227
231
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:
230
234
await docker . getContainer ( containerName ) . kill ( ) ;
231
235
await ensureDockerTunnelRunning ( proxyPort ) ;
236
+ await delay ( 10 ) ; // Wait for the port to bind after startup
232
237
return refreshDockerTunnelPortCache ( proxyPort , { force : true } ) ;
233
238
}
234
239
0 commit comments