chenhg5/cc-connect · error

stop: ( )

Error message

stop: %s (%w)

What it means

Stop() wraps a failure of `systemctl stop cc-connect`, including systemctl's stdout. It means the service could not be stopped, typically because systemctl cannot reach the systemd manager or the unit is not loaded.

Solutions

  1. Read the embedded stdout; if it says the unit is not loaded, treat the service as already stopped.
  2. Verify systemd is reachable: `systemctl is-system-running`.
  3. Run with sufficient privileges (sudo) for system units.
  4. Fall back gracefully: check `systemctl is-active cc-connect` before/after Stop.

Example fix

// before
mgr.Stop() // stop: Unit cc-connect.service not loaded.
// after
if out, err := mgr.Stop(); err != nil && strings.Contains(out, "not loaded") {
    return nil // already stopped
} else if err != nil {
    return err
}
Defensive patterns

Strategy: try-catch

Validate before calling

active, err := exec.Command("systemctl", "is-active", "cc-connect").Output()
if err == nil && strings.TrimSpace(string(active)) == "inactive" {
    return nil // already stopped
}

Type guard

func serviceLoaded() bool {
    out, err := exec.Command("systemctl", "is-active", "cc-connect").Output()
    return err == nil && strings.TrimSpace(string(out)) != "unknown"
}

Try / catch

if err := mgr.Stop(); err != nil {
    if strings.Contains(err.Error(), "not loaded") {
        return nil // idempotent stop
    }
    return err
}

Prevention

When it happens

Trigger: Calling Stop when systemd is not running, the unit was never installed/loaded, or the caller lacks privileges; also during shutdown when the system manager is going away.

Common situations: Calling Stop from within the service itself in a constrained environment; unit already removed but Stop invoked on stale state; WSL2 session where systemd isn't PID 1.

Understand the failure class

Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.

Related errors


AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06). Data as JSON: /api/errors/1042bb71bbca0506. Report an issue: GitHub.

Appendix: source

Thrown at daemon/systemd.go:116

	if _, err := runSystemctl(m.sysArgs("daemon-reload")...); err != nil {
		slog.Warn("systemd: daemon-reload failed", "error", err)
	}
	return nil
}

func (m *systemdManager) Start() error {
	out, err := runSystemctl(m.sysArgs("start", systemdServiceName)...)
	if err != nil {
		return fmt.Errorf("start: %s (%w)", out, err)
	}
	return nil
}

func (m *systemdManager) Stop() error {
	out, err := runSystemctl(m.sysArgs("stop", systemdServiceName)...)
	if err != nil {
		return fmt.Errorf("stop: %s (%w)", out, err)
	}
	return nil
}

func (m *systemdManager) Restart() error {
	out, err := runSystemctl(m.sysArgs("restart", systemdServiceName)...)
	if err != nil {
		return fmt.Errorf("restart: %s (%w)", out, err)
	}
	return nil
}

func (m *systemdManager) Status() (*Status, error) {
	st := &Status{Platform: m.Platform()}

	unitPath := m.unitPath()
	if _, err := os.Stat(unitPath); err != nil {
		return st, nil

View on GitHub (pinned to 4000b2338a)