Skip to content

Clean up Windows action process trees - #246

Open
alex-the-third wants to merge 1 commit into
buildbarn:mainfrom
alex-the-third:fix/windows-process-cleanup
Open

Clean up Windows action process trees#246
alex-the-third wants to merge 1 commit into
buildbarn:mainfrom
alex-the-third:fix/windows-process-cleanup

Conversation

@alex-the-third

Copy link
Copy Markdown

On Windows, actions may exit while leaving subprocesses running. Those subprocesses can retain their working directory or open files inside the action root, causing the executor to fail when removing it after the action completes.

This implements the approach proposed in #245 and follows Bazel's native Windows launcher.

Implementation

Each action process is placed in its own non-breakaway Windows Job Object configured with JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE.

The root process is created suspended, assigned to the job, and only then resumed. This prevents it from spawning descendants before job assignment. Because Go's os/exec closes the primary thread handle before cmd.Start() returns, the implementation locates the root process's threads by PID and resumes them through the Windows API.

When the root process exits, any remaining processes in the job are terminated. The runner then waits for JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO before returning, ensuring that descendants no longer hold files or directories inside the action root.

Command cancellation terminates the entire job instead of only the root process. Startup cancellation is serialized with process resumption so cancellation cannot terminate the suspended process between the cancellation check and resume.

Non-Windows behavior remains unchanged.

Testing

Added Windows tests covering:

  • A parent that exits while leaving a child holding a file open inside the action root.
  • Cancellation of a running parent and descendant.
  • The race between the startup cancellation check and resuming the suspended process.
  • Immediate removal of the action root after Run() returns.

Fixes #245

Create action processes suspended and assign them to non-breakaway job objects before allowing them to execute. Terminate and wait for surviving descendants when the root process exits so build directory cleanup does not race lingering processes.

Serialize startup cancellation with process resumption and cover descendant cleanup and the cancellation race with Windows tests.
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.

Windows: remote executor fails to delete build root when actions leave subprocesses running

1 participant