Skip to content

fix(daemon): tests stop the daemons they start, and a daemon exits when its home is gone - #5294

Merged
kovtcharov-amd merged 3 commits into
mainfrom
fix/orphaned-daemons
Oct 18, 2026
Merged

kovtcharov-amd merged 3 commits into
mainfrom
fix/orphaned-daemons

Conversation

@kovtcharov-amd

@kovtcharov-amd kovtcharov-amd commented Oct 17, 2026 •

Copy link
Copy Markdown
Collaborator

Test runs left GAIA daemons running for days (57 on one dev box), and those daemons managed the developer's real Lemonade Server. A test that built an agent asked a fresh daemon to start GAIA's Lemonade whenever it looked stopped; that daemon lived in the session's temp home, so nothing ever stopped it, but it owned the real ~/.gaia server, which may explain #5261. Now a daemon shuts itself down once its home or instance.json is gone, a test session fails if it leaves one running, tests can no longer touch the real ~/.gaia Lemonade, and gaia kill --daemons cleans up the strays already out there.

Test plan

  • CI green, including the new "Check no GAIA daemon outlived the tests" step on Linux, macOS and Windows
  • pytest tests/integration/test_daemon_lifetime.py: a real daemon exits after its instance.json or home is deleted, and after its tied process exits; a child test session that leaks a daemon fails and the daemon is killed; tests that act on the real ~/.gaia fail
  • pytest tests/unit/test_daemon_lifetime.py tests/unit/test_daemon_strays.py
  • pytest tests/unit -n auto leaves no new python -m gaia.daemon process behind
  • gaia kill --daemons lists daemons with pid, age and home, stops only the orphaned ones, and leaves the daemon registered in your own home running
🔍 Technical details
  • Leak source: LemonadeClient._follow_gaia_lemonade and LemonadeManager.start_embedded_if_stopped both call ensure_lemonade → start_or_attach, which spawns a daemon when none is registered. Outside tests/unit nothing stubbed that, and GAIA_HOME was unset, so the daemon managed the real ~/.gaia Lemonade. tests/test_sdk.py and tests/test_hardware_advisor_agent.py both hit this on a machine where gaia init installed GAIA's Lemonade and it was stopped.
  • Behaviour change on dev machines: in that state those tests now fail with the real-home message (start your Lemonade with gaia daemon start first) instead of quietly starting it through a daemon that never exits. CI runners have no ~/.gaia Lemonade, so CI is unaffected.
  • Other leakers found and fixed: tests/integration/test_daemon.py spawned real daemons without GAIA_HOME (they started and stopped the real Lemonade); tests/integration/test_context_override_reload.py spawned one on every CI run via the CLI's Lemonade setup; tests/unit/installer/test_init_default_profile.py ran gaia init --check in a subprocess, which started one against an installed GAIA.
  • GAIA_DAEMON_EXIT_WITH_PID is opt-in and set only by the root conftest. Production daemon lifetime changes only when the home or instance.json disappears, at which point no client can reach the daemon anyway.
  • gaia kill --daemons kills only the daemon process (and its venv-launcher child), never the rest of its tree, so a Lemonade Server it started is left alone. It protects the daemon whose pid this home's instance.json records, because an older daemon could register in ~/.gaia while its environment pointed at a deleted test home.
  • The Go TUI tests leaked one too (a gaia-chat-test-* home). The daemon-side exit covers that case; the Go tests are unchanged.

…en its home is gone

Test runs left GAIA daemons running forever, and those daemons owned the
developer's real Lemonade Server. Building an agent outside tests/unit asked a
fresh daemon to start GAIA's Lemonade whenever it looked stopped; that daemon
lived under the session's temp home but managed the real ~/.gaia server, and
kept polling after the temp home was deleted.

- The daemon shuts down on its own, logging why, once its home or
  instance.json is gone. GAIA_DAEMON_EXIT_WITH_PID (opt-in, set only by the
  test suite) also ties it to one process, so a killed test run cannot leak.
- The root conftest fails a session that leaves a daemon running, naming the
  test that started it, and fails any test that would start, stop or unload
  the real ~/.gaia Lemonade or spawn a daemon for it.
- The embedded-Lemonade start stub moves from tests/unit to every test, at
  session scope so class- and module-scoped agent fixtures are covered too.
- tests/integration/test_daemon.py points GAIA_HOME at a tmp folder and stops
  its daemon in fixture teardown, even when a test fails.
- `gaia kill --daemons` lists daemons with pid, age and home, and stops the
  orphaned ones; the daemon this home registered is never stopped.
- CI checks no daemon outlived the tests on Linux, macOS and Windows.

Closes #5293
@github-actions github-actions Bot added documentation Documentation changes devops DevOps/infrastructure changes cli CLI changes tests Test changes daemon Daemon supervisor / sidecar control plane labels Oct 17, 2026
…daemon

It ran the CLI's Lemonade setup, which starts a real daemon for the user's
GAIA home; on CI that daemon was left running after every run. The fixture
HTTP server is the model server, so the daemon step is stubbed.
On a machine with GAIA installed, `gaia init --check` started a daemon for
the real Lemonade Server, and the daemon outlived the test run.
@kovtcharov-amd
kovtcharov-amd merged commit 7a0d02e into main Oct 18, 2026
68 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the fix/orphaned-daemons branch October 18, 2026 07:29
kovtcharov-amd added a commit to kovtcharov/gaia that referenced this pull request Oct 19, 2026
…it for two hours (amd#5359)

A GAIA daemon started for a temporary home (an eval, a demo, a test
drive) kept running forever once its users were gone, as long as the
folder still existed. A cleanup on one dev machine found 15 of them
running at once, up to 43 hours old, along with the agent sidecars some
still supervised. Now a daemon for any home other than `~/.gaia` shuts
itself down after two hours with no connected client, no busy sidecar,
no model-slot lease held or queued, and no pending scheduled job, and
the next GAIA command starts a fresh one. The everyday `~/.gaia` daemon
is unaffected.

This completes amd#5294, which made a daemon exit when its home folder is
deleted.

## Test plan

- [x] `pytest tests/unit/test_daemon_lifetime.py` (45 passed) and every
daemon and broker test (705 passed)
- [x] `python util/lint.py --black --isort`
- [x] Real daemon, scratch `GAIA_HOME`,
`GAIA_DAEMON_IDLE_EXIT_MINUTES=1`, no client: logged "nothing has used
it for 1 minutes" and exited 60 s after start
- [x] Same, with a registered app client: still running at 90 s. Once
the client process ended, the daemon dropped it within 4 s and exited 60
s later
- [x] The `~/.gaia` daemon never idles out: pinned by
`test_the_default_home_never_idles_out`, and `is_default_home` by
`test_the_machine_home_is_the_default`

<details>
<summary>🔍 Technical details</summary>

- `lifetime.exit_reason` gains an idle reason that applies only when
`paths.host_dir()` isn't `~/.gaia/host`. `LifetimeWatch` takes `idle`
and `idle_limit`.
- The idle measure is `lifetime.DaemonIdle`. A held or queued
`ModelSlotBroker` lease counts as use. The broker now reports
`last_activity_s_ago` (stamped on grant, release and reclaim), so even
leases shorter than the 5 s check restart the clock. Underneath it is
`LemonadeClients.snapshot()["idle_seconds"]`: `None` while any client
lease is held, and reset by every sweep while a sidecar connection runs.
Once that passes the limit, `clock.job_counts()["pending"]` is checked
too, so the scheduler DB is read only near the limit.
- `GAIA_DAEMON_IDLE_EXIT_MINUTES`: unset means 120, `0` means off, and
anything that isn't a non-negative integer raises `DaemonStartError` at
startup.
- Docs: the daemon section of `docs/reference/cli.mdx`.
</details>

---------

Co-authored-by: Kalin Ovtcharov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI changes daemon Daemon supervisor / sidecar control plane devops DevOps/infrastructure changes documentation Documentation changes tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant