carlos / platform Public

activator: a failed systemctl state read logs systemctl's own reason (#451 diagnosis)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 41db5d531fa4a6fe68053c9990c97b7d4592b01e parent c2ef3fb
2 files changed, +52 −3
  • internal/activator/control.go +23 −3
  • internal/activator/control_test.go +29 −0
diff --git a/internal/activator/control.go b/internal/activator/control.go
index 0ca06618b..3e929f59d 100644
--- a/internal/activator/control.go
+++ b/internal/activator/control.go
@@ -3,6 +3,7 @@ package activator
import (
"context"
"errors"
+ "fmt"
"os"
"os/exec"
"path/filepath"
@@ -100,7 +101,7 @@ type UnitState func(ctx context.Context, unit string) (active, invocation string
func systemctlUnitState(ctx context.Context, unit string) (string, string, error) {
out, err := exec.CommandContext(ctx, "systemctl", "show", "-p", "ActiveState,InvocationID", unit).Output()
if err != nil {
- return "", "", err
+ return "", "", unitStateError(err)
}
var active, inv string
for _, line := range strings.Split(string(out), "\n") {
@@ -134,7 +135,7 @@ type MainExit struct {
func systemctlUnitMainExit(ctx context.Context, unit string) (MainExit, error) {
out, err := exec.CommandContext(ctx, "systemctl", "show", "-p", "InvocationID,ExecMainCode,ExecMainStatus,SuccessExitStatus", unit).Output()
if err != nil {
- return MainExit{}, err
+ return MainExit{}, unitStateError(err)
}
var m MainExit
for _, line := range strings.Split(string(out), "\n") {
@@ -167,7 +168,7 @@ type ControlGroupOf func(ctx context.Context, unit string) (string, error)
func systemctlControlGroup(ctx context.Context, unit string) (string, error) {
out, err := exec.CommandContext(ctx, "systemctl", "show", "-p", "ControlGroup", "--value", unit).Output()
if err != nil {
- return "", err
+ return "", unitStateError(err)
}
return strings.TrimSpace(string(out)), nil
}
@@ -190,3 +191,22 @@ func readCgroupEvents(path string) (exists, populated bool, err error) {
}
return true, !strings.Contains(string(b), "populated 0"), nil
}
+
+// unitStateError is a failed systemctl read's error with systemctl's own
+// stderr appended, cut to 200 bytes: "exit status 1" alone could not tell
+// a lost D-Bus at shutdown from a missing unit (#451). An error that is
+// not an exit is returned unchanged.
+func unitStateError(err error) error {
+ var ee *exec.ExitError
+ if !errors.As(err, &ee) {
+ return err
+ }
+ msg := strings.TrimSpace(string(ee.Stderr))
+ if msg == "" {
+ return err
+ }
+ if len(msg) > 200 {
+ msg = msg[:200]
+ }
+ return fmt.Errorf("%w: %s", err, msg)
+}
diff --git a/internal/activator/control_test.go b/internal/activator/control_test.go
new file mode 100644
index 000000000..88dc96156
--- /dev/null
+++ b/internal/activator/control_test.go
@@ -0,0 +1,29 @@
+package activator
+
+import (
+ "errors"
+ "os/exec"
+ "strings"
+ "testing"
+)
+
+// #451: a systemctl that fails at shutdown said only "exit status 1"; its
+// own reason (a lost D-Bus, say) is what tells the cause apart.
+func TestUnitStateErrorCarriesStderr(t *testing.T) {
+ _, err := exec.Command("sh", "-c", `echo "Failed to connect to bus: No such file or directory" >&2; exit 1`).Output()
+ got := unitStateError(err)
+ var ee *exec.ExitError
+ if !strings.Contains(got.Error(), "Failed to connect to bus") || !errors.As(got, &ee) {
+ t.Fatalf("%v", got)
+ }
+
+ _, err = exec.Command("sh", "-c", `head -c 1024 /dev/zero | tr '\0' q >&2; exit 1`).Output()
+ if got := unitStateError(err).Error(); strings.Count(got, "q") != 200 {
+ t.Fatalf("stderr not cut to 200 bytes: %d", strings.Count(got, "q"))
+ }
+
+ plain := errors.New("context deadline exceeded")
+ if got := unitStateError(plain); got != plain {
+ t.Fatalf("a non-exit error changed: %v", got)
+ }
+}