Skip to content

Commit 565e1d1

Browse files
committed
Handle async port refresh errors explicitly
1 parent 692c574 commit 565e1d1

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

src/interceptors/docker/docker-interception-services.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import * as Docker from 'dockerode';
22
import { ProxySettingCallback } from 'mockttp';
3-
import { getDnsServer } from '../../dns-server';
43

4+
import { reportError } from '../../error-tracking';
55
import { addShutdownHandler } from '../../shutdown';
66

77
import { DOCKER_BUILD_LABEL } from './docker-build-injection';
88
import { DOCKER_CONTAINER_LABEL } from './docker-commands';
99

10+
import { getDnsServer } from '../../dns-server';
1011
import {
1112
monitorDockerNetworkAliases,
1213
stopMonitoringDockerNetworkAliases
@@ -69,7 +70,7 @@ export async function ensureDockerServicesRunning(proxyPort: number) {
6970
monitorDockerNetworkAliases(proxyPort),
7071
ensureDockerTunnelRunning(proxyPort),
7172
getDnsServer(proxyPort)
72-
]);
73+
]).catch(reportError);
7374
}
7475

7576
export async function stopDockerInterceptionServices(

src/interceptors/docker/docker-tunnel-proxy.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import * as Docker from 'dockerode';
33
import * as semver from 'semver';
44
import { Mutex } from 'async-mutex';
55

6-
import { delay } from '../../util/promise';
7-
86
import { DOCKER_HOST_HOSTNAME, isImageAvailable } from './docker-commands';
97
import { isDockerAvailable } from './docker-interception-services';
8+
import { delay } from '../../util/promise';
9+
import { reportError } from '../../error-tracking';
1010

1111
const DOCKER_TUNNEL_IMAGE = "httptoolkit/docker-socks-tunnel:v1.1.0";
1212
const 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

Comments
 (0)