Skip to content

feat(cli): keep lerd daemon alive so macOS can gracefully stop Podman #1407

Description

@retr0ripper

lerd starts the Podman Machine and containers at macOS login through lerd-autostart, but the lerd start process exits as soon as startup finishes, leaving the VM running without a process that launchd can signal when macOS shuts down. The result is that the Podman Machine can be killed abruptly, and lerd-postgres-timescaledb sees an unclean PostgreSQL shutdown and spends several minutes in crash recovery and fsyncing data on the next boot.

It should run a long-lived lerd daemon instead. The daemon starts the same environment as lerd start, then stays alive waiting for SIGTERM or SIGINT. When the service manager asks it to stop, it runs the normal lerd stop flow and waits for that shutdown to finish before exiting, giving the containers and Podman Machine a chance to shut down cleanly. The daemon should reuse the existing start and stop implementation rather than spawning lerd start and lerd stop as child commands, so there is one lifecycle implementation and no recursive service behavior.

The autostart service should invoke lerd daemon instead of lerd start, with Restart=on-failure so an unexpected daemon failure is still recovered by the service manager. The command itself is an internal implementation detail, so it should be registered as hidden and not become another user-facing command in the normal lerd --help output.

The shutdown path needs to wait for lerd stop to complete before the daemon exits. Receiving SIGTERM, starting the stop operation and immediately exiting would leave the VM exposed to the same abrupt termination this change is intended to fix. The handler should also tolerate an already-stopped or partially stopped environment and avoid running shutdown concurrently if multiple signals arrive.

The existing lerd start and lerd stop commands keep their current behavior. Only the macOS autostart lifecycle changes from a fire-and-forget start process to a supervised daemon, so manually starting and stopping Lerd does not acquire a persistent background process.

The main files are internal/cli/daemon.go for the new hidden command, the command registration in cmd/lerd/main.go (or the existing command registry), and internal/systemd/units/lerd-autostart.service for the service change from lerd start to lerd daemon.

Verification should include running the daemon directly and confirming it remains alive after startup, sending it SIGTERM and checking that the normal stop sequence completes before it exits, then rebooting macOS through the actual autostart service. After the reboot, lerd-postgres-timescaledb should come up without the multi-minute PostgreSQL crash-recovery/fsync phase. Repeated reboot/shutdown cycles should produce the same clean result.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions