Skip to content

✨ feat: add --poweroff signal-init for a non-systemd init - #16

Merged
yeazelm merged 2 commits into
mainfrom
apple_vm/stereosd
Jul 10, 2026
Merged

yeazelm merged 2 commits into
mainfrom
apple_vm/stereosd

Conversation

@yeazelm

@yeazelm yeazelm commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Summary

The shutdown coordinator hardcoded systemctl poweroff, which assumes systemd
owns the services and will SIGTERM them on the way down. Under a minimal init
(capstan as PID 1) there is no systemd, so shutdown failed and the VM never
powered off.

Adds a --poweroff strategy (Config.PoweroffMode):

  • systemctl (default, unchanged): systemctl poweroff.
  • signal-init: SIGTERM PID 1 and let init stop the supervised services and
    reboot(2).

The power-off step is now an injectable function on the coordinator; the actual
kill(1, SIGTERM) sits behind a package var so the new test exercises
signal-init without signalling its own init.

Context: lets stereosd run under capstan (a minimal Rust PID 1 for microVM
sandboxes) as well as the systemd-based stereOS image.

Test plan

  • gofmt / go vet ./... clean
  • New pkg/shutdown_strategy_test.go: defaults to systemctl; signal-init
    signals PID 1 and never runs systemctl
  • make test — change is isolated to the shutdown path; the new specs pass.
    Note: make test runs -race and trips a pre-existing data race in the
    IPCServer Serve/Close lifecycle test (pkg/ipc.go), reproducible on
    main and unrelated to this change.

Part of PCC-777.

@yeazelm
yeazelm requested a review from a team June 27, 2026 03:45
@linear-code

linear-code Bot commented Jun 27, 2026

Copy link
Copy Markdown

PCC-777

@greptile-apps

greptile-apps Bot commented Jun 27, 2026

Copy link
Copy Markdown

Greptile Summary

This PR adds a --poweroff CLI flag and Config.PoweroffMode field to support two shutdown strategies: systemctl (existing default) and signal-init (SIGTERM PID 1, for capstan-based microVM sandboxes). The implementation is clean — a poweroffFunc type replaces the hard-coded systemctl poweroff call, and a package-level killPID1 var makes the signal path testable without touching the real init.

  • pkg/shutdown.go: Introduces PoweroffSystemctl/PoweroffSignalInit constants, poweroffFor dispatcher, and signalInitPoweroff; ShutdownCoordinator now holds an injected poweroffFunc rather than calling systemctl directly.
  • cmd/run/run.go: Exposes the strategy via --poweroff (default "systemctl"); the flag value is accepted without validation, so unknown strings silently fall back to systemctl.
  • pkg/shutdown_strategy_test.go: New Ginkgo specs verify that the default uses systemctl and that signal-init calls killPID1 without invoking systemctl.

Confidence Score: 4/5

Safe to merge; the core shutdown abstraction is correct and backward-compatible, with two minor rough edges worth addressing before the feature is widely used.

The --poweroff flag accepts any string without validation, so a misconfigured capstan deployment would silently fall back to systemctl and never power off the VM. The test uses a mutable package-level variable to intercept killPID1, which works today but is fragile. Neither issue affects the systemctl default path.

cmd/run/run.go (flag validation) and pkg/shutdown_strategy_test.go (package-level var mutation pattern)

Important Files Changed

Filename Overview
cmd/run/run.go Adds --poweroff CLI flag wired to Config.PoweroffMode; flag value is not validated and silently falls back to systemctl on typo
pkg/shutdown.go Replaces hard-coded systemctl poweroff with an injectable poweroffFunc; adds poweroffFor dispatcher and killPID1 package var for testability; logic is clean
pkg/shutdown_strategy_test.go New Ginkgo specs covering both strategies; mutates package-level killPID1 which is safe for sequential specs but fragile under parallel execution
pkg/stereosd.go Adds PoweroffMode to Config, defaults to PoweroffSystemctl, and threads it through to NewShutdownCoordinator; straightforward and correct
pkg/stereosd_test.go Existing ShutdownCoordinator test updated to pass PoweroffSystemctl to match new constructor signature; no functional change

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Host sends shutdown command] --> B[ShutdownCoordinator.Execute]
    B --> C[UnmountAll shared directories]
    C --> D[sync filesystems]
    D --> E{poweroffFunc selected at startup}
    E -->|PoweroffSystemctl default| F[Commander.Run systemctl poweroff]
    E -->|PoweroffSignalInit --poweroff signal-init| G[killPID1 syscall.Kill 1 SIGTERM]
    F --> H[systemd SIGTERMs all services incl. agentd]
    G --> I[capstan PID 1 stops supervised services and calls reboot RB_POWER_OFF]
    H --> J[VM powers off]
    I --> J
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
    A[Host sends shutdown command] --> B[ShutdownCoordinator.Execute]
    B --> C[UnmountAll shared directories]
    C --> D[sync filesystems]
    D --> E{poweroffFunc selected at startup}
    E -->|PoweroffSystemctl default| F[Commander.Run systemctl poweroff]
    E -->|PoweroffSignalInit --poweroff signal-init| G[killPID1 syscall.Kill 1 SIGTERM]
    F --> H[systemd SIGTERMs all services incl. agentd]
    G --> I[capstan PID 1 stops supervised services and calls reboot RB_POWER_OFF]
    H --> J[VM powers off]
    I --> J
Loading
Prompt To Fix All With AI
Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 2
cmd/run/run.go:48-49
**Unvalidated `--poweroff` flag silently falls back to systemctl**

`poweroffFor` treats any unrecognised string as `PoweroffSystemctl`. A user running stereosd under capstan who types `--poweroff signal_init` (underscore) or any other typo will silently get the systemd path, the shutdown will fail at the `systemctl` call, and the VM will never power off — without any indication of misconfiguration. Consider validating the flag value in `runDaemon` (or in `poweroffFor`) and returning an early error for unknown modes.

### Issue 2 of 2
pkg/shutdown_strategy_test.go:21-24
**Package-level variable mutation is unsafe under parallel specs**

`killPID1` is a package-level `var`, so any concurrent spec that also exercises `signalInitPoweroff` (e.g., after a future refactor adds a second signal-init test) would race on the write. The approach works today because Ginkgo specs within one process are sequential, but it is easy to break. The conventional Go pattern is to make `killPID1` a field on `ShutdownCoordinator` (already injectable via the constructor) rather than a naked package var, which also removes the need for `DeferCleanup` restoring global state.

Reviews (1): Last reviewed commit: "✨ feat: add --poweroff signal-init for a..." | Re-trigger Greptile

Comment thread cmd/run/run.go Resolved
Comment thread pkg/shutdown_strategy_test.go Resolved

@jasonwc jasonwc left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM, pending Greptile response.

@yeazelm
yeazelm force-pushed the apple_vm/stereosd branch from b734c15 to 7be4d4d Compare July 10, 2026 16:44
yeazelm added 2 commits July 10, 2026 09:45
The shutdown coordinator hardcoded `systemctl poweroff`, which assumes systemd owns the services and will SIGTERM them on its way down. Under a minimal init (capstan as PID 1) there is no systemd, so that call fails and nothing powers off.

Add a `--poweroff` strategy, selected by `Config.PoweroffMode`. The default `systemctl` mode is unchanged for the systemd-based stereOS image. The `signal-init` mode sends SIGTERM to PID 1 and lets init stop supervised services and call reboot(2) itself.

The power-off step is selected explicitly, and unknown mode strings are rejected before daemon startup. The PID 1 signal function is stored on each shutdown coordinator so tests can inject a fake without mutating package-level state.

Part of PCC-777.
The daggerverse ghcontrib module now requires dagger v0.20.8, so the PR checks (title conformance, Linear magic word) error out under the pinned v0.20.6 with "module requires dagger v0.20.8, but you have v0.20.6".
@yeazelm
yeazelm force-pushed the apple_vm/stereosd branch from 7be4d4d to f8dee14 Compare July 10, 2026 16:49
@yeazelm
yeazelm merged commit 02d835b into main Jul 10, 2026
2 checks passed
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