Skip to main content

Module single_instance

Module single_instance 

Source
Expand description

Single-instance guard for the trusty-memory daemon.

Why: macOS launchd KeepAlive { SuccessfulExit: false } (i.e. OnSuccess) respawns the daemon whenever it exits with a non-zero code. When a second daemon instance fails to bind (EADDRINUSE — the first instance already owns port 7070 and/or the UDS socket), it exits non-zero, which launchd interprets as a crash and spawns yet another copy. The resulting zombie herd (69 observed in the wild) exhausts file descriptors on top of the existing fd-limit bug.

The fix: before attempting to bind, probe the discovery files. If a healthy daemon is already responding to /health, exit 0 (success). Launchd treats exit-0 as “clean shutdown” and does NOT respawn (SuccessfulExit:false = restart only on non-zero). This collapses the zombie herd immediately on the next invocation without touching launchd config.

What: exposes single_instance_check (async, for real daemon startups) and StartupAction (pure enum, for unit testing the decision logic).

Test: startup_action_* unit tests cover every branch including the stale-socket-vs-live-socket distinction.

Enums§

StartupAction
What the daemon startup should do after the single-instance check.

Functions§

single_instance_check
Perform the single-instance check at daemon startup.
startup_action_from_probe_result
Decide what to do based on the result of an HTTP health probe.