Skip to content

ci: give the workspace image restore a deadline, and clean up after it on Windows - #346254

Merged
Ladislau Szomoru (lszomoru) merged 1 commit into
mainfrom
lszomoru/agents/workspace-image-restore-deadline
Nov 5, 2026
Merged

Ladislau Szomoru (lszomoru) merged 1 commit into
mainfrom
lszomoru/agents/workspace-image-restore-deadline

Conversation

@lszomoru

Copy link
Copy Markdown
Member

Follow-up to #346211, ahead of the Windows test jobs adopting the workspace images (#345429). Only image.ts restore, which those jobs will run to mount an image, changes: the commands that build the images (env, seed and pack) are unchanged, and no job restores an image yet.

What changed

  • A deadline. restore --timeout-seconds (240 by default) gives up before the timeout of its step could stop it with the image half attached. The deadline aborts every request, the writes of chunks that are already downloaded, and the PowerShell that attaches the VHDX. After the deadline, the warning says that the restore took too long, and the output used is false, as after any other error.
  • Windows cleanup after any failure. Before, a failure while vhd.ps1 attached the image could leave the volume mounted over the parent folder of the workspace (so the workspace was the image's, not an empty folder), or the VHDX attached for good (ATTACH_VIRTUAL_DISK_FLAG_PERMANENT_LIFETIME) with its temporary file kept in memory. Now every failure, and a commit or workspace mismatch after the mount, runs the same cleanup: it waits for the attaching PowerShell to exit, unmounts the folder (mountvol /D), deletes the VHDX (or detaches it with Dismount-DiskImage first when it is still attached), and recreates the empty workspace, so that the job can set up as without an image.
  • Waiting for the real exit. Node emits error for an aborted child before the process ends, so the cleanup waits for its exit, except for a process that never started.
  • The README describes the deadline.

How to test

The Windows path runs in the pull request that makes the Windows test jobs restore the images: its checks show the restore logs, and a temporary lower --timeout-seconds exercises the deadline during the download and during the attach.

Validation of this change:

  • ./node_modules/.bin/tsc --noEmit --strict --module nodenext --moduleResolution nodenext --target es2024 --lib es2024,dom --types node --skipLibCheck --erasableSyntaxOnly --noUnusedLocals --noUnusedParameters .github/workflows/workspace_image/image.ts: passed
  • ./node_modules/.bin/eslint .github/workflows/workspace_image/image.ts: passed
  • cd build && node --test lib/test/detectMissingCaches.test.ts lib/test/detectChanges.test.ts lib/test/workspaceImage.test.ts lib/test/agentHostTestSelection.test.ts: 34 passed
  • image.ts restore on macOS against a local mock of the API and of an artifact: with an API that never answers and a 2 s deadline, it stopped after 2.2 s with used=false; with chunks that take 30 s and a 3 s deadline, it aborted the downloads in flight and stopped after 3.1 s with used=false; without an artifact, it stopped at once (the deadline doesn't keep the process alive); a full download unpacked the image byte for byte, including empty chunks.
  • Not run: the Windows unmount and detach (mountvol, Dismount-DiskImage), which need a Windows runner.

…t on Windows

image.ts restore, which the PR test jobs will run to mount a workspace
image, now gives up after --timeout-seconds (240 by default), before the
timeout of its step could stop it with the image half attached: the
deadline aborts the downloads, the writes of the image and the
PowerShell that attaches it. On Windows, every failure, and a commit
mismatch after the mount, then waits for that PowerShell to exit,
unmounts the volume, detaches and deletes the VHDX, and recreates the
empty workspace, so that the job can set up as without an image.
Before, a failure during the attach could leave the volume mounted over
the workspace, or the VHDX attached and its temporary file in memory.

Only the restore changes: the commands that build the images (env, seed
and pack) are unchanged, and no job restores an image yet.

Co-authored-by: Alex Dima <alexdima@microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 5, 2026 13:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Synchronous mount commands block the deadline timer, allowing stalled or overdue restores to leave images partially attached or incorrectly report success without deadline enforcement and cleanup.

1 open issue
What changed in this PR

This PR adds restore-time safeguards to the workspace-image tooling ahead of adoption by Windows test jobs. Image-building commands and workflow adoption remain unchanged.

Changes:

  • Adds a configurable restore deadline, defaulting to 240 seconds, with cancellation for downloads, chunk writing, and Windows attachment.
  • Adds shared Windows failure cleanup that waits for the attaching process to exit before unmounting and attempting to detach/delete the VHDX.
  • Documents the deadline and fallback behavior.
File Description
.github/​workflows/​workspace_image/​README.md Documents the restore timeout and cleanup behavior.
.github/​workflows/​workspace_image/​image.ts Adds deadline handling, cancellation, and Windows failure cleanup.

Review effort: Balanced

if (!(timeoutSeconds > 0)) {
throw new Error('--timeout-seconds must be a positive number');
}
const deadline = setTimeout(() => downloads.abort(new Error(`The restore took longer than ${timeoutSeconds} s`)), timeoutSeconds * 1000);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks. This pull request merged before the fix, so it is in #346256: the Linux and macOS mount commands now run asynchronously and the deadline stops them, a mount that completed before they stopped is unmounted (and the Linux loop device detached), and the restore checks the deadline once more before it reports the image.

@lszomoru
Ladislau Szomoru (lszomoru) merged commit 55da8dc into main Nov 5, 2026
60 checks passed
@lszomoru
Ladislau Szomoru (lszomoru) deleted the lszomoru/agents/workspace-image-restore-deadline branch November 5, 2026 14:07
@vs-code-engineering vs-code-engineering Bot added this to the 1.146.0 milestone Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants