Skip to content

Commit b9f68fe

Browse files
committed
Stop Docker tunnel & intercepted containers in parallel
1 parent 5b02dbf commit b9f68fe

File tree

1 file changed

+46
-45
lines changed

1 file changed

+46
-45
lines changed

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

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -92,52 +92,53 @@ const pendingDeactivations: {
9292
// When a Docker container or the whole server shuts down, we do our best to delete
9393
// every remaining intercepted image or container. None of these will be usable
9494
// without us anyway, as they all depend on HTTP Toolkit for connectivity.
95-
export async function deleteAllInterceptedDockerData(proxyPort: number | 'all') {
95+
export async function deleteAllInterceptedDockerData(proxyPort: number | 'all'): Promise<void> {
9696
if (pendingDeactivations[proxyPort]) return pendingDeactivations[proxyPort];
9797
if (!await isDockerAvailable()) return;
9898

99-
return pendingDeactivations[proxyPort] = (async () => {
100-
const docker = new Docker();
101-
102-
await stopDockerTunnel(proxyPort);
103-
104-
const containers = await docker.listContainers({
105-
all: true,
106-
filters: JSON.stringify({
107-
label: [
108-
proxyPort === 'all'
109-
? DOCKER_CONTAINER_LABEL
110-
: `${DOCKER_CONTAINER_LABEL}=${proxyPort}`
111-
]
112-
})
113-
});
114-
115-
await Promise.all(containers.map(async (containerData) => {
116-
const container = docker.getContainer(containerData.Id);
117-
118-
// Best efforts clean stop & remove:
119-
await container.stop({ t: 1 }).catch(() => {});
120-
await container.remove({ force: true }).catch(() => {});
121-
}));
122-
123-
// We clean up images after containers, in case some containers depended
124-
// on some images that we intercepted.
125-
const images = await docker.listImages({
126-
all: true,
127-
filters: JSON.stringify({
128-
label: [
129-
proxyPort === 'all'
130-
? DOCKER_BUILD_LABEL
131-
: `${DOCKER_BUILD_LABEL}=${proxyPort}`
132-
]
133-
})
134-
});
135-
136-
await Promise.all(images.map(async (imageData) => {
137-
await docker.getImage(imageData.Id).remove().catch(() => {});
138-
}));
139-
140-
// Unmark this deactivation as pending
141-
delete pendingDeactivations[proxyPort];
142-
})();
99+
return pendingDeactivations[proxyPort] = Promise.all([
100+
stopDockerTunnel(proxyPort),
101+
(async () => {
102+
const docker = new Docker();
103+
104+
const containers = await docker.listContainers({
105+
all: true,
106+
filters: JSON.stringify({
107+
label: [
108+
proxyPort === 'all'
109+
? DOCKER_CONTAINER_LABEL
110+
: `${DOCKER_CONTAINER_LABEL}=${proxyPort}`
111+
]
112+
})
113+
});
114+
115+
await Promise.all(containers.map(async (containerData) => {
116+
const container = docker.getContainer(containerData.Id);
117+
118+
// Best efforts clean stop & remove:
119+
await container.stop({ t: 1 }).catch(() => {});
120+
await container.remove({ force: true }).catch(() => {});
121+
}));
122+
123+
// We clean up images after containers, in case some containers depended
124+
// on some images that we intercepted.
125+
const images = await docker.listImages({
126+
all: true,
127+
filters: JSON.stringify({
128+
label: [
129+
proxyPort === 'all'
130+
? DOCKER_BUILD_LABEL
131+
: `${DOCKER_BUILD_LABEL}=${proxyPort}`
132+
]
133+
})
134+
});
135+
136+
await Promise.all(images.map(async (imageData) => {
137+
await docker.getImage(imageData.Id).remove().catch(() => {});
138+
}));
139+
140+
// Unmark this deactivation as pending
141+
delete pendingDeactivations[proxyPort];
142+
})()
143+
]) as Promise<unknown> as Promise<void>;
143144
}

0 commit comments

Comments
 (0)