Skip to content

Add procattr to start and stop process trees on each platform - #108

Merged
ernestrc merged 2 commits into
unstablebuild:mainfrom
ejb711:add-procattr
Sep 30, 2026
Merged

ernestrc merged 2 commits into
unstablebuild:mainfrom
ejb711:add-procattr

Conversation

@ejb711

@ejb711 ejb711 commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

Part of Windows support (#68, coordinated on #81). Third slice; graphics stay with #82.

Four call sites built syscall.SysProcAttr literals with Setpgid, Setsid and Setctty, and the
file scheme killed process groups with syscall.Kill. None of these exist on Windows, so
workspace, workspacerpc, extensionv2, idedebug and vte fail to compile there.

This collects them in a small internal/procattr package: NewGroup, NewSession, LeadsGroup,
KillGroup and Signal. The unix implementations are the old code moved unchanged, including
killProcessGroup from the file scheme. On Windows a new group is CREATE_NEW_PROCESS_GROUP, a
group is killed with taskkill /T /F (which walks the process tree, with "not found" mapped to
os.ErrProcessDone like ESRCH on unix), sessions and controlling terminals do not exist, and
Signal supports SIGKILL only.

Linux and macOS are unchanged: the existing process-group tests in file_scheme_test and
workspace_runner_test pass as before, and new tests pin that the helpers build exactly the
attributes the literals built. The Windows-tagged tests cover the taskkill arguments and the
group check; GOOS=windows go vet ./internal/procattr/ passes.

idelsp's single Setpgid literal is deliberately left alone here because #107 touches the same
file's imports; it becomes a one-line follow-up once both land.

Comment thread internal/procattr/procattr_windows.go Outdated
// KillGroup terminates proc and every process descended from it.
func KillGroup(proc *os.Process) error {
// Windows has no group-wide signal; taskkill /T walks the parent-pid tree.
err := exec.Command("taskkill", taskkillArgs(proc.Pid)...).Run()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding SysProcAttr{HideWindow: true} so every kill doesn't flash a console window.

}

// KillGroup terminates proc and every process descended from it.
func KillGroup(proc *os.Process) error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also thread a context here and pass it as exec.CommandContext. On unix it's just a syscall, but on windows we're spawning a subprocess that can block for an unbound amount of time.

// Signal sends sig to the process with the given pid. Windows supports only
// SIGKILL.
func Signal(pid int, sig syscall.Signal) error {
proc, err := os.FindProcess(pid)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a defer proc.Release() here.

Comment thread internal/procattr/procattr_unix_test.go Outdated
cmd.SysProcAttr = NewGroup()
require.NoError(t, cmd.Run())

assert.ErrorIs(t, KillGroup(cmd.Process), os.ErrProcessDone)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After cmd.Run() the process has returned, the pgid is free for reuse and SIGKILL(-pid) can land on an unrelated group. Low probability, but getting random process killed on a busy CI machine would not be fun to debug.

// `go run` exec the real program as a grandchild that SIGKILL
// cannot be forwarded to. Joining an existing Pgid is someone
// else's group and not ours to signal.
attr := cmd.SysProcAttr

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the comment above now belongs to the LeadsGroup implementations

Comment thread internal/workspace/file_scheme.go Outdated
err := syscall.Kill(int(pid), signal)
err := procattr.Signal(int(pid), signal)
if err != nil {
return fmt.Errorf("syscall kill: %w", err)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should update this error now to "Send signal: %w"

ejb711 and others added 2 commits September 30, 2026 14:51
Four call sites built syscall.SysProcAttr literals with Setpgid, Setsid and Setctty,
and the file scheme read Setpgid and Pgid and killed process groups with syscall.Kill.
None of these exist on Windows, so workspace, workspacerpc, extensionv2, idedebug and
vte fail to compile there (unstablebuild#68).

Collect them in internal/procattr: NewGroup, NewSession, LeadsGroup, KillGroup and
Signal. The unix implementations are the old code moved unchanged, including
killProcessGroup from the file scheme. On Windows a new group is
CREATE_NEW_PROCESS_GROUP, a group is killed with taskkill /T /F, which walks the
process tree, sessions and controlling terminals do not exist, and Signal supports
SIGKILL only. idelsp has the same Setpgid literal; it moves in a follow-up so this
change stays independent of the LSP socketpair split.

Linux and macOS are unchanged: the existing process-group tests pass as before, and new
tests pin that the helpers build the same attributes as the literals they replace.

Signed-off-by: Eric Brooks <[email protected]>
… site

The Unix implementation was selected with !windows, which also matched js/wasm and
plan9, where SysProcAttr has no Setpgid or Setsid and the package failed to build.

The language server spawn site still built its SysProcAttr by hand, so it was left
out of the shared contract.

On Windows, KillGroup ran taskkill found on PATH, flashed a console window from a
GUI process, and could block cancel and teardown with no bound, where the Unix
syscall never blocks. Signal leaked the handle opened by FindProcess.

NewSession now returns nil when nothing is requested, so callers no longer need
their own guard. The Unix KillGroup test signalled the group of a reaped child
whose pid could be reused; it now targets a pid above any pid_max. Tests that only
restated the implementation are removed.

Co-Authored-By: Rune Agent (claude/claude-opus-5-5) <[email protected]>
Signed-off-by: ernestrc <[email protected]>
@ernestrc
ernestrc merged commit 4895d70 into unstablebuild:main Sep 30, 2026
8 checks passed
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 76.92308% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/procattr/procattr_unix.go 70.00% 3 Missing ⚠️
internal/workspace/file_scheme.go 60.00% 2 Missing ⚠️
internal/ide/idedebug/server.go 0.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

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.

3 participants