Clean up Windows action process trees - #246
Open
alex-the-third wants to merge 1 commit into
Open
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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/execcloses the primary thread handle beforecmd.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_ZERObefore 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:
Run()returns.Fixes #245