Skip to content

fix(lemonade): stop the model servers a dead lemond leaves behind - #5352

Merged
kovtcharov-amd merged 4 commits into
amd:mainfrom
kovtcharov:fix/reap-orphaned-lemond-backends
Oct 19, 2026
Merged

kovtcharov-amd merged 4 commits into
amd:mainfrom
kovtcharov:fix/reap-orphaned-lemond-backends

Conversation

@kovtcharov

@kovtcharov kovtcharov commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

When GAIA's own Lemonade server crashed or was killed by itself, its model servers kept running. Each one holds gigabytes of memory, and the next start loaded the same models again beside them, doubling GPU and RAM use. On one machine a Gemma and an embedding server ran for about 30 hours after their server died. Now GAIA stops them when it starts its server, and when it finds the old one gone. It stops a process only when it runs from GAIA's own install and the process that started it has exited. Another Lemonade's processes, a live server's, and a tool such as llama-bench that you run from a terminal are never touched.

Reproduced by tests/unit/test_lemonade_orphaned_backends.py: on main, two of its real-process tests fail with the symptom ("the dead lemond's llama-server is still running"), and the others fail because the reaper doesn't exist yet. All pass here. Each runs a real process tree on Windows: a copy of the interpreter stands in for llama-server.exe under the install's cache/bin, and the test kills its stand-in lemond without the tree.

🔍 Technical details
  • EmbeddedLemonade._reap_orphaned_backends() runs from _start_locked before spawning, and from status() only when the recorded lemond is gone. A healthy status() never scans, and a test pins that.
  • A process qualifies only when its executable resolves under <GAIA_HOME>/lemonade/cache/bin and its anchor is gone, or its pid now names a younger process. The anchor is the parent on Windows, which never reparents. On POSIX it is the session leader: lemond starts its own session, and an orphan reparented to init or a subreaper stays in it. An anchor the OS hides counts as alive.
  • The scan filters on the file names in cache/bin before reading any process's path or parent, which takes ~0.1 s instead of ~5–8 s on a machine with ~400 processes. It then waits up to 5 s for the killed servers to exit, so the next model load sees their memory free.
  • Each stop is logged with its pid. If GAIA may not stop one, the warning names the pid and how to end it by hand.
  • A read-only dry run on the reporting machine kept the live lemond's four backends and singled out the two orphans of the dead one (a whisper-server and a koko). They were left running for the user to decide.
  • Reviewed independently twice. The first review found a 5 s scan, leaking tests, a timing sleep, a leftover fake pid, a missing wait for exit, and uncovered branches. The second found that a user's terminal tool from cache/bin was killed. That one is fixed by the anchor rule above, with a real-process test, plus POSIX reparenting and pid-reuse cases. A gone server's record is now cleared only if no new start recorded itself meanwhile, and a start scans once. Removing each of those guards fails its test. The real-process tests are Windows-only; Linux and macOS run the portable TestOrphanedBackends cases.
  • Independent of fix(lemonade): stop calling a busy shared server stalled, and retry a stolen port #5314. Both add methods after status(), so they conflict on text: keep both.

Test plan

  • python -m pytest tests/unit/test_lemonade_orphaned_backends.py tests/unit/test_lemonade_embedded.py: 91 passed, no stray processes left (Windows)
  • The real-process tests on origin/main: the status and start cases fail with the symptom
  • Related suites (test_lemonade_embedded*, test_daemon_lemonade*, test_init_command, test_cli_lemonade_embedded, uninstall, restart and child-env tests): 660 passed, isolated GAIA_HOME
  • python util/lint.py --black --isort --flake8 --pylint --test-lanes: all pass
  • CI: Unit Tests (Linux, Windows, macOS), Code Quality

@github-actions github-actions Bot added documentation Documentation changes llm LLM backend changes tests Test changes performance Performance-critical changes labels Oct 18, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve with suggestions

When GAIA's private Lemonade server crashes or is killed, its model servers keep running and hold gigabytes of memory. This PR stops those leftovers the next time GAIA starts the server, or when a status check finds the server gone. The guard is narrow: a process is stopped only if it runs from GAIA's own install and the server that started it is no longer running. A live server's model servers and anything from a Lemonade the user runs themselves are left alone. The PR includes tests that start real processes and fail on main. I found no blocking issues.

One thing worth confirming: a model server counts as protected only if its direct parent is a running GAIA server. If a backend under the install ever starts its own helper process from the same folder, the cleanup would kill that helper while the server is still healthy. Checking every ancestor instead of just the parent would rule this out.

Real-world evidence

N/A: no evidence bundle was produced for this run. This change affects server lifecycle, not a CLI command, flag, or API response. The PR shows real-process proof instead: four Windows tests start a real process tree, kill the stand-in server, and fail on main. The author also reports a read-only dry run on the affected machine that singled out exactly the two orphans. Beyond what the PR shows, this verdict rests on static review.

🔍 Technical details

Issues found

🟢 Only the direct parent is checked (src/gaia/llm/lemonade_embedded.py:1201). _reap_orphaned_backends asks _is_running_lemond(proc.ppid()). Suppose a process under cache/bin spawns a child that is also under cache/bin, for example a router-style llama-server or a helper binary. That child's parent is the backend, not lemond, so it gets killed while lemond is alive. I couldn't confirm whether any shipped backend does this today. Walking up the ancestors closes the gap cheaply:

            if any(
                self._is_running_lemond(p.pid) for p in _ancestors_safe(proc)
            ) or self._is_running_lemond(parent):
                continue

You would also need a small _ancestors_safe helper that wraps proc.parents() and treats AccessDenied as "protected", the same rule the parent check already uses. If you know no backend forks a child from cache/bin, a one-line comment saying so is enough.

🟢 status() reaps outside the instance lock (lemonade_embedded.py:1165). A bare status() (CLI or daemon) can run at the same moment as a start() in another process. I traced it and it's safe: a fresh backend's parent is the new live lemond, and _is_running_lemond accepts any version under root/dist. Consider a short comment so nobody "fixes" this later by moving the reap above the health check.

Strengths

  • The safety rule is sound. It requires both the path check (under this install's cache/bin) and the parent check (not a live lemond of this install). An unreadable parent counts as running, so the code never kills on a guess. It also accepts lemond from any version under dist/, so an older-version server is protected too.
  • The name pre-filter from the files in cache/bin keeps the scan cheap, and a test pins that a healthy status() never scans at all.
  • The tests are strong. The Windows tests use real interpreter copies as stand-in processes with a startup handshake and clean up leaked processes. The cross-platform TestOrphanedBackends cases cover all three outcomes (kill, skip, access denied) plus the empty-install early return. The CLI doc's added paragraph matches the code's behavior.

kovtcharov and others added 3 commits October 18, 2026 23:11
When GAIA's lemond crashed or was killed without its process tree, its
llama-server, whisper-server and kokoro children kept running, each holding
gigabytes of memory, and the next start loaded the same models again beside
them. Two such servers ran for about 30 hours on a developer machine.

GAIA now stops them when it starts its server and when it finds the recorded
one gone. A process qualifies only when its executable is under this
install's cache/bin and its parent is not a running lemond from this
install's dist tree; a parent the OS hides counts as running. Each one
stopped, or one GAIA may not stop, is logged with its pid. The scan matches
names first, so it costs ~0.1 s, and waits for the killed servers to exit
so the next load sees their memory free.
The first version treated any cache/bin process whose parent was not a
running lemond as an orphan, so a llama-bench or llama-cli the user ran from
a terminal was killed on the next start. Now a process is reaped only when
its anchor is gone, or its pid was taken by a younger process: the parent on
Windows, which never reparents, and the session leader on POSIX, where an
orphan is reparented to init or a subreaper but stays in dead lemond's
session. A user's tool has a live shell as its anchor and is left alone.

status() clears the gone server's record only if no start recorded a new one
meanwhile, and a start scans once instead of twice. Denied inspections are
logged at debug level.
A status check can reap while another process starts the server. That is safe because the new server's backends have a live anchor; say so where someone might otherwise move the reap under the lock.
@kovtcharov-amd
kovtcharov-amd force-pushed the fix/reap-orphaned-lemond-backends branch from a501345 to fb33b56 Compare October 19, 2026 06:13
# Conflicts:
#	src/gaia/llm/lemonade_embedded.py
@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Oct 19, 2026
Merged via the queue into amd:main with commit de79ca8 Oct 19, 2026
39 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the fix/reap-orphaned-lemond-backends branch October 19, 2026 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Documentation changes llm LLM backend changes performance Performance-critical changes tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants