Use stable Windows process handles for job assignment

This commit is contained in:
2026-08-31 23:49:47 +00:00
parent 04283e99d4
commit f14583321d
2 changed files with 14 additions and 10 deletions

View File

@@ -273,7 +273,7 @@ error presentation at the existing adapter-facing boundary.
## Stage 3 — Use The Stable Windows Process Handle
**Status: Pending**
**Status: Completed**
### Goal

View File

@@ -39,19 +39,23 @@ func (tree *windowsProcessTree) Start(cmd *exec.Cmd) error {
return err
}
process, err := windows.OpenProcess(windows.PROCESS_SET_QUOTA|windows.PROCESS_TERMINATE, false, uint32(cmd.Process.Pid))
if err == nil {
err = windows.AssignProcessToJobObject(job, process)
_ = windows.CloseHandle(process)
var assignErr error
withHandleErr := cmd.Process.WithHandle(func(handle uintptr) {
assignErr = windows.AssignProcessToJobObject(job, windows.Handle(handle))
})
if withHandleErr == nil && assignErr == nil {
assignErr = resumeInitialThread(uint32(cmd.Process.Pid))
}
if err == nil {
err = resumeInitialThread(uint32(cmd.Process.Pid))
}
if err != nil {
if withHandleErr != nil || assignErr != nil {
killErr := cmd.Process.Kill()
waitErr := cmd.Wait()
_ = windows.CloseHandle(job)
return joinErrors(fmt.Errorf("assign process to job object: %w", err), killErr, waitErr)
return joinErrors(
fmt.Errorf("acquire process handle for job assignment: %w", withHandleErr),
fmt.Errorf("assign process to job object or resume initial thread: %w", assignErr),
killErr,
waitErr,
)
}
tree.job = job