Skip to content

Commit a723963

Browse files
committed
Handle some race conditions in Docker proxy & network monitoring
Not totally clear if these are the culprit, but there's some untraceable missing container errors coming from somewhere, and there's certainly possible race conditions in these cases.
1 parent 846e43d commit a723963

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/interceptors/docker/docker-networking.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,13 @@ class DockerNetworkMonitor {
280280
return undefined;
281281
}
282282

283-
const networkContainers = await Promise.all(
283+
const networkContainers = (await Promise.all(
284284
Object.values(networkDetails.Containers ?? {}).map((networkContainer) =>
285-
this.docker.getContainer(networkContainer.Name).inspect()
285+
this.docker.getContainer(networkContainer.Name)
286+
.inspect()
287+
.catch(() => undefined) // There's a race condition here - skip any now-missing containers
286288
)
287-
);
289+
)).filter((container) => !!container) as Docker.ContainerInspectInfo[];
288290

289291
if (!networkContainers.find((container) => this.isInterceptedContainer(container))) {
290292
// If we're not tracking any containers in this network, we don't need its aliases.
@@ -375,7 +377,11 @@ class DockerNetworkMonitor {
375377

376378
const linkedContainer = networkContainers.find(c => c.Name === linkedContainerName)
377379
// Container should always be in the same network AFAICT, but fallback to lookup just in case:
378-
?? await this.docker.getContainer(linkedContainerName).inspect();
380+
?? await this.docker.getContainer(linkedContainerName)
381+
.inspect()
382+
.catch(() => undefined); // There's a race condition here - skip any now missing containers
383+
384+
if (!linkedContainer) return [];
379385

380386
const linkedContainerIp = linkedContainer.NetworkSettings.Networks[networkId]?.IPAddress ||
381387
linkedContainer.NetworkSettings.IPAddress;

src/interceptors/docker/docker-proxy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ async function createDockerProxy(proxyPort: number, httpsConfig: { certPath: str
152152
const startContainerMatch = START_CONTAINER_MATCHER.exec(reqPath);
153153
if (startContainerMatch) {
154154
const containerId = startContainerMatch[1];
155-
const containerData = await docker.getContainer(containerId).inspect();
155+
const containerData = await docker.getContainer(containerId).inspect().catch(() => undefined);
156156

157-
if (!isInterceptedContainer(containerData, proxyPort)) {
157+
if (containerData && !isInterceptedContainer(containerData, proxyPort)) {
158158
res.writeHead(400).end(
159159
"HTTP Toolkit cannot intercept startup of preexisting non-intercepted containers. " +
160160
"The container must be recreated here first - try `docker run <image>` instead."

0 commit comments

Comments
 (0)