Skip to content

feat(tui): implement process actions on Windows - #249

Open
CJstate wants to merge 1 commit into
pranshuparmar:stagingfrom
CJstate:feat/windows-process-actions
Open

CJstate wants to merge 1 commit into
pranshuparmar:stagingfrom
CJstate:feat/windows-process-actions

Conversation

@CJstate

@CJstate CJstate commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Description

Implements the process actions (Kill, Terminate, Pause, Resume, Renice) for the Windows build of the TUI, which previously registered every action as "not supported on Windows" (issue #248, @pranshuparmar's suggested mappings).

  • Kill / Terminate → TerminateProcess. Windows has no asynchronous graceful-termination signal for an arbitrary process, so the graceful variant is skipped, as suggested in the issue.
  • Pause / Resume → NtSuspendProcess / NtResumeProcess (ntdll exports; there is no documented API to suspend or resume every thread of another process at once).
  • Renice → the −20…19 nice ladder maps onto Windows priority classes (≤−10 HIGH, <0 ABOVE_NORMAL, 0 NORMAL, ≤10 BELOW_NORMAL, else IDLE). REALTIME_PRIORITY_CLASS is deliberately never used — it can starve the rest of the system, and no nice value grants that level.

Safety parity with the Unix side:

  • A terminated process object can briefly outlive the process, so every action checks the exit code first and rejects targets that have already exited — matching the Unix rule that signalling a reaped PID fails.
  • The shared TUI-layer protections (the started-at identity check before any action fires) apply unchanged, since only the leaf functions were Windows-specific.
  • The out-of-range renice guard is identical.

actionsSupported is now true on Windows, so the actions menu, footer hints and the TUI-level action tests in update_test.go become active on Windows automatically.

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

Checklist

  • I have opened this PR against the staging branch, with a Conventional Commits title
  • I have performed a self-review of my own code
  • go vet ./... passes and gofmt reports no formatting issues; the full test suite passes on Windows
  • Platform-specific changes are checked in every platform's file — this change only touches _windows files; Unix files are unchanged
  • Feature is TUI-only, so no CLI/TUI sync is needed

Testing

On Windows (go test ./..., all packages):

  • TestWindowsTerminateAndKill: term and kill end a real child process.
  • TestWindowsPauseResume: a paused ping stops writing output, and resumes after NtResumeProcess — the output flow is the suspend oracle.
  • TestWindowsSetNiceMapsToPriorityClass: each rung of the nice ladder is verified against GetPriorityClass of the target.
  • TestWindowsSetNiceRangeGuardWindows and dead-PID tests: guards and exited-target rejection.

Fixes #248

@CJstate

CJstate commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

Heads-up: the three workflow runs on this PR are sitting at "action required" (fork PRs need maintainer approval to run). Could you approve them when convenient? All tests were run locally on Windows (go test ./... across every package, plus go vet), and the diff is confined to _windows files plus one README line, so nothing outside the Windows build changes.

@pranshuparmar pranshuparmar added this to the 0.3.5 milestone Nov 10, 2026
@CJstate
CJstate force-pushed the feat/windows-process-actions branch from b15ac54 to 956c151 Compare November 14, 2026 01:51
@CJstate

CJstate commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

The Windows lint job was failing here. .golangci.yml excludes syscall.CloseHandle from errcheck, but this code calls golang.org/x/sys/windows.CloseHandle, a distinct symbol. Under GOOS=windows the three defer windows.CloseHandle(h) calls in actions_windows.go were therefore reported as unchecked errors.

Fixed them with the convention already used in internal/proc/user_windows.go (defer func() { _ = windows.CloseHandle(h) }()), plus the same fix in the test helper. Verified locally: GOOS=windows golangci-lint run ./... and GOOS=linux golangci-lint run ./... both report 0 issues, go vet ./... is clean, and go test -count=1 ./... passes. No functional behavior changed.

Also squashed the branch into a single SSH-signed, DCO-compliant commit so the history stays clean.

@pranshuparmar pranshuparmar left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

  • The README platform table still shows ❌ for Windows under “Process Actions” (README.md:1097).
  • On Windows the status could say “Paused …” or “Terminated …” instead of "Signal sent"

)

func pauseProcess(pid int) error { return suspendOrResume(pid, procNtSuspendProcess) }
func resumeProcess(pid int) error { return suspendOrResume(pid, procNtResumeProcess) }

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Suspensions stack on Windows: pause ×2 then resume ×1 leaves every thread suspended, yet the TUI reports success. Unix resumes fully on one SIGCONT. Resume should repeat until the process is running again.

}
if r1, _, ntErr := proc.Call(uintptr(h)); r1 != 0 {
// An NTSTATUS is not an errno; report it verbatim.
return fmt.Errorf("%s PID %d failed: %v", proc.Name, pid, ntErr)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

ntErr comes from GetLastError, which NtSuspendProcess/NtResumeProcess don’t set, so this can print “The operation completed successfully.” Please report the NTSTATUS instead: windows.NTStatus(r1).

c := startSleeperWindows(t)
pid := c.Process.Pid
_ = c.Process.Kill()
_ = c.Wait() // close the handle, so the PID no longer refers to a live process

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

After Wait() the handle is closed and Windows can reuse the PID, so resumeProcess/termProcess below could hit an unrelated process. Move Wait() to cleanup.

c := startSleeperWindows(t)
pid := c.Process.Pid
_ = c.Process.Kill()
_ = c.Wait()

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Same as above: drop the Wait() here so setNice can’t land on a reused PID

@CJstate
CJstate force-pushed the feat/windows-process-actions branch 2 times, most recently from 5218583 to e627878 Compare November 25, 2026 04:33
Kill, Terminate, Pause, Resume and Renice map to TerminateProcess,
NtSuspendProcess/NtResumeProcess and priority classes. Reject actions on
already-exited processes and validate nice values before any syscall.
Update the platform table and action status wording.

Signed-off-by: CJstate <142857225+CJstate@users.noreply.github.com>
@CJstate
CJstate force-pushed the feat/windows-process-actions branch from e627878 to 530900c Compare November 25, 2026 04:33
@CJstate

CJstate commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Addressed both points: rebased onto current staging with a single commit; the platform table now marks Process Actions as supported on Windows; and Windows Kill/Terminate/Pause/Resume report "Killed/Terminated/Paused/Resumed" rather than "Signal sent". The Windows tests exercise real processes via TerminateProcess and NtSuspend/NtResume.

This branch has not been deployed

No deployments
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.

2 participants