Skip to content

Commit 3b7f9eb

Browse files
Fix The handle is invalid. error on Windows (#227)
* Return back to latest Windows image with upgraded Go * Try changing Windows shell * Supplement error * Use windows.OpenProcess to get handle
1 parent c30cd67 commit 3b7f9eb

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

.circleci/config.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ executors:
2828
resource_class: circleci-runner/rum-large
2929
windows:
3030
machine:
31-
image: windows-server-2022-gui:2025.05.1
32-
shell: bash.exe -login
33-
resource_class: windows.medium
31+
image: windows-server-2022-gui:current
32+
shell: bash.exe --login -eo pipefail
33+
resource_class: windows.large
3434
ccc:
3535
docker:
3636
- image: circleci/command-convenience:0.1
@@ -146,7 +146,7 @@ jobs:
146146
- setup
147147
- with-go-cache:
148148
steps:
149-
- run: ./do test ./... -count 2
149+
- run: ./do test ./...
150150
- notify_failing_main
151151

152152
build:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module github.com/circleci/runner-init
22

33
go 1.24.0
44

5-
toolchain go1.24.7
5+
toolchain go1.25.3
66

77
require (
88
github.com/alecthomas/kong v1.12.1

task/cmd/cmd_windows.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ package cmd
22

33
import (
44
"context"
5+
"fmt"
56
"os"
67
"os/exec"
78
"os/signal"
8-
"sync"
9-
"sync/atomic"
109
"unsafe"
1110

1211
"github.com/circleci/ex/o11y"
@@ -30,13 +29,16 @@ func switchUser(ctx context.Context, _ *exec.Cmd, user string) {
3029
}
3130

3231
func additionalSetup(_ context.Context, cmd *exec.Cmd) {
32+
if cmd.SysProcAttr == nil {
33+
cmd.SysProcAttr = &windows.SysProcAttr{}
34+
}
3335
cmd.SysProcAttr.CreationFlags = windows.CREATE_NEW_PROCESS_GROUP
3436
}
3537

3638
func (c *Command) start() error {
3739
g, err := newProcessExitGroup()
3840
if err != nil {
39-
return err
41+
return fmt.Errorf("failed to create new process group: %w", err)
4042
}
4143

4244
c.cmd.Cancel = func() error {
@@ -47,17 +49,11 @@ func (c *Command) start() error {
4749
return err
4850
}
4951

50-
return g.AddProcess(c.cmd.Process)
51-
}
52+
if err := g.AddProcess(c.cmd.Process); err != nil {
53+
return fmt.Errorf("failed to add process to group: %w", err)
54+
}
5255

53-
// Process struct matches os.Process layout to extract handle via unsafe pointer
54-
// https://stackoverflow.com/a/56739249
55-
type process struct {
56-
Pid int
57-
_ uint8
58-
_ atomic.Uint64
59-
_ sync.RWMutex
60-
Handle uintptr
56+
return nil
6157
}
6258

6359
type processExitGroup windows.Handle
@@ -89,7 +85,11 @@ func (g processExitGroup) Dispose() error {
8985
}
9086

9187
func (g processExitGroup) AddProcess(p *os.Process) error {
92-
return windows.AssignProcessToJobObject(
93-
windows.Handle(g),
94-
windows.Handle((*process)(unsafe.Pointer(p)).Handle))
88+
handle, err := windows.OpenProcess(windows.PROCESS_SET_QUOTA|windows.PROCESS_TERMINATE, false, uint32(p.Pid))
89+
if err != nil {
90+
return err
91+
}
92+
defer windows.CloseHandle(handle)
93+
94+
return windows.AssignProcessToJobObject(windows.Handle(g), handle)
9595
}

0 commit comments

Comments
 (0)