From 1ff9e2d1035a1ad02992a4497a8c8fccfccb510e Mon Sep 17 00:00:00 2001 From: Christof Marti Date: Mon, 16 Nov 2026 21:09:23 +0100 Subject: [PATCH 1/2] Wait for in-progress container removal before starting Compose services A Rebuild can leave the previous Compose container in the 'removing' state while 'up' runs. Starting it with 'up --no-recreate' then fails with 'container is marked for removal and cannot be started'. Wait for the removal to finish and create a new container instead. removeContainer also gave up after 7 attempts (about 7 seconds) while a removal was in progress. Wait up to 5 minutes instead, since removing large containers can take longer. Fixes https://github.com/microsoft/vscode-remote-release/issues/11967 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/spec-node/dockerCompose.ts | 6 ++++-- src/spec-shutdown/dockerUtils.ts | 12 ++++++++++-- src/test/dockerUtils.test.ts | 31 +++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/spec-node/dockerCompose.ts b/src/spec-node/dockerCompose.ts index 410f7636d..0d87a6d06 100644 --- a/src/spec-node/dockerCompose.ts +++ b/src/spec-node/dockerCompose.ts @@ -51,8 +51,10 @@ async function _openDockerComposeDevContainer(params: DockerResolverParameters, } container = containerId ? await inspectContainer(params, containerId) : undefined; - if (container && (params.removeOnStartup === true || params.removeOnStartup === container.Id)) { - const text = 'Removing existing container.'; + // A container that is already being removed cannot be started and keeps its name until removed. https://github.com/microsoft/vscode-remote-release/issues/11967 + const isRemoving = container?.State.Status === 'removing'; + if (container && (params.removeOnStartup === true || params.removeOnStartup === container.Id || isRemoving)) { + const text = isRemoving ? 'Waiting for existing container to be removed.' : 'Removing existing container.'; const start = common.output.start(text); await removeContainer(params, container.Id); common.output.stop(text, start); diff --git a/src/spec-shutdown/dockerUtils.ts b/src/spec-shutdown/dockerUtils.ts index 487093c81..60b6441a0 100644 --- a/src/spec-shutdown/dockerUtils.ts +++ b/src/spec-shutdown/dockerUtils.ts @@ -176,21 +176,29 @@ export async function listContainers(params: DockerCLIParameters | PartialExecPa .filter(s => !!s); } +const removalInProgressTimeoutMs = 5 * 60 * 1000; + export async function removeContainer(params: DockerCLIParameters | PartialExecParameters | DockerResolverParameters, nameOrId: string) { const useEvents = !('cliVariant' in params && params.cliVariant === CLIVariant.Wslc); let eventsProcess: Exec | undefined; let removedSeenP: Promise | undefined; + let deadline: number | undefined; try { - for (let i = 0, n = 7; i < n; i++) { + while (true) { try { await dockerCLI(params, 'rm', '-f', nameOrId); return; } catch (err) { // https://github.com/microsoft/vscode-remote-release/issues/6509 const stderr: string = err?.stderr?.toString().toLowerCase() || ''; - if (i === n - 1 || !stderr.includes('already in progress')) { + if (!stderr.includes('already in progress') || (deadline !== undefined && Date.now() >= deadline)) { throw err; } + if (deadline === undefined) { + // Removing large containers can take a while. https://github.com/microsoft/vscode-remote-release/issues/11967 + deadline = Date.now() + removalInProgressTimeoutMs; + toExecParameters(params).output.write(`Waiting for the removal of container ${nameOrId} to finish.`); + } if (useEvents && !removedSeenP) { eventsProcess = await getEvents(params, { container: [nameOrId], diff --git a/src/test/dockerUtils.test.ts b/src/test/dockerUtils.test.ts index af27206f1..79440ac3a 100644 --- a/src/test/dockerUtils.test.ts +++ b/src/test/dockerUtils.test.ts @@ -116,6 +116,37 @@ describe('Container inspect', () => { }); }); +describe('Container removal', function () { + this.timeout(20 * 1000); + + it('waits for a removal in progress beyond seven attempts', async () => { + const conflicts = 8; + const calls: string[][] = []; + const lines: string[] = []; + const params: PartialExecParameters = { + cmd: 'docker', env: {}, output: makeLog(createPlainLog(text => lines.push(text), () => LogLevel.Trace)), + exec: async ({ args = [] }) => { + calls.push(args); + const stdout = new PassThrough(); + const stderr = new PassThrough(); + const failed = args[0] === 'rm' && calls.filter(call => call[0] === 'rm').length <= conflicts; + return { + stdin: new PassThrough(), stdout, stderr, + exit: args[0] === 'events' ? new Promise(() => {}) : new Promise(resolve => setImmediate(() => { + stdout.end(); + stderr.end(failed ? 'Error response from daemon: removal of container x is already in progress' : ''); + resolve({ code: failed ? 1 : 0, signal: null }); + })), + terminate: async () => {}, + }; + }, + }; + await removeContainer(params, 'x'); + assert.strictEqual(calls.filter(call => call[0] === 'rm').length, conflicts + 1); + assert.strictEqual(lines.filter(line => line.includes('Waiting for the removal of container x to finish.')).length, 1); + }); +}); + describe('Docker argument prefixes', () => { const prefix = ['--context', 'context with spaces', '001', 'a=b', '"literal quotes"', '']; From a8de987992b9f6681a0e0f713af33ae3be6fbaa6 Mon Sep 17 00:00:00 2001 From: Christof Marti Date: Mon, 16 Nov 2026 21:40:24 +0100 Subject: [PATCH 2/2] Treat a container being removed as missing when one is expected With --expect-existing-container, a Compose container in the removing state passed the existence check and was then replaced by a new one. Reject it like the single-container path does. Also test that waiting for an in-progress removal stops after five minutes. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/spec-node/dockerCompose.ts | 11 +++++----- src/test/dockerUtils.test.ts | 37 ++++++++++++++++++++++++++++------ 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/spec-node/dockerCompose.ts b/src/spec-node/dockerCompose.ts index 0d87a6d06..9368bccc7 100644 --- a/src/spec-node/dockerCompose.ts +++ b/src/spec-node/dockerCompose.ts @@ -46,13 +46,14 @@ async function _openDockerComposeDevContainer(params: DockerResolverParameters, const composeConfig = await readDockerComposeConfig(buildParams, composeFiles, envFile); const projectName = await getProjectName(buildParams, workspace, composeFiles, composeConfig); const containerId = await findComposeContainer(params, projectName, config.service); - if (params.expectExistingContainer && !containerId) { - throw new ContainerError({ description: 'The expected container does not exist.' }); - } - container = containerId ? await inspectContainer(params, containerId) : undefined; + const existingContainer = containerId ? await inspectContainer(params, containerId) : undefined; // A container that is already being removed cannot be started and keeps its name until removed. https://github.com/microsoft/vscode-remote-release/issues/11967 - const isRemoving = container?.State.Status === 'removing'; + const isRemoving = existingContainer?.State.Status === 'removing'; + if (params.expectExistingContainer && (!existingContainer || isRemoving)) { + throw new ContainerError({ description: 'The expected container does not exist.' }); + } + container = existingContainer; if (container && (params.removeOnStartup === true || params.removeOnStartup === container.Id || isRemoving)) { const text = isRemoving ? 'Waiting for existing container to be removed.' : 'Removing existing container.'; const start = common.output.start(text); diff --git a/src/test/dockerUtils.test.ts b/src/test/dockerUtils.test.ts index 79440ac3a..4837f7600 100644 --- a/src/test/dockerUtils.test.ts +++ b/src/test/dockerUtils.test.ts @@ -119,17 +119,20 @@ describe('Container inspect', () => { describe('Container removal', function () { this.timeout(20 * 1000); - it('waits for a removal in progress beyond seven attempts', async () => { - const conflicts = 8; - const calls: string[][] = []; + function fakeRemoval(conflicts: number, onRemove?: (attempt: number) => void) { + let removals = 0; const lines: string[] = []; const params: PartialExecParameters = { cmd: 'docker', env: {}, output: makeLog(createPlainLog(text => lines.push(text), () => LogLevel.Trace)), exec: async ({ args = [] }) => { - calls.push(args); const stdout = new PassThrough(); const stderr = new PassThrough(); - const failed = args[0] === 'rm' && calls.filter(call => call[0] === 'rm').length <= conflicts; + const remove = args[0] === 'rm'; + if (remove) { + removals++; + onRemove?.(removals); + } + const failed = remove && removals <= conflicts; return { stdin: new PassThrough(), stdout, stderr, exit: args[0] === 'events' ? new Promise(() => {}) : new Promise(resolve => setImmediate(() => { @@ -141,10 +144,32 @@ describe('Container removal', function () { }; }, }; + return { params, lines, removals: () => removals }; + } + + it('waits for a removal in progress beyond seven attempts', async () => { + const { params, lines, removals } = fakeRemoval(8); await removeContainer(params, 'x'); - assert.strictEqual(calls.filter(call => call[0] === 'rm').length, conflicts + 1); + assert.strictEqual(removals(), 9); assert.strictEqual(lines.filter(line => line.includes('Waiting for the removal of container x to finish.')).length, 1); }); + + it('stops waiting for a removal in progress after five minutes', async () => { + const now = Date.now; + let offset = 0; + Date.now = () => now() + offset; + try { + const { params, removals } = fakeRemoval(Infinity, attempt => { + if (attempt === 2) { + offset = 5 * 60 * 1000; + } + }); + await assert.rejects(removeContainer(params, 'x'), (err: any) => String(err?.stderr).includes('already in progress')); + assert.strictEqual(removals(), 2); + } finally { + Date.now = now; + } + }); }); describe('Docker argument prefixes', () => {