Repository navigation
ci: give the workspace image restore a deadline, and clean up after it on Windows - #346254
Conversation
…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>
There was a problem hiding this comment.
🟡 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); |
There was a problem hiding this comment.
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.

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,seedandpack) are unchanged, and no job restores an image yet.What changed
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 outputusedisfalse, as after any other error.vhd.ps1attached 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 withDismount-DiskImagefirst when it is still attached), and recreates the empty workspace, so that the job can set up as without an image.errorfor an aborted child before the process ends, so the cleanup waits for itsexit, except for a process that never started.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-secondsexercises 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: passedcd build && node --test lib/test/detectMissingCaches.test.ts lib/test/detectChanges.test.ts lib/test/workspaceImage.test.ts lib/test/agentHostTestSelection.test.ts: 34 passedimage.ts restoreon 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 withused=false; with chunks that take 30 s and a 3 s deadline, it aborted the downloads in flight and stopped after 3.1 s withused=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.mountvol,Dismount-DiskImage), which need a Windows runner.