Repository navigation
fix(lemonade): stop calling a busy shared server stalled, and retry a stolen port - #5314
Conversation
… stolen port A GAIA Lemonade swapping models looked dead to every other client, which were told to stop it. Lemonade's /health waits on the router lock that an eviction holds until the old model's in-flight request drains, so on a shared machine it can miss a 3 s probe for minutes. A server that also answers the lock-free /live is now reported running and busy: callers reuse it and requests queue. Busy for longer than lemond's own request budget, it counts as stalled again, so a wedged server still gets an actionable error. The daemon's supervisor made the same mistake with a system install and called a slow port a stranger after 20 s. It now attaches when the port answers /live and lemond's /docs index. Starting GAIA's server on a port it chose could fail when another program bound that port first: lemond exits 1 when either loopback address is held. GAIA now retries on a new port, logging each retry, at most three times within the one start deadline. A port the caller named is never swapped.
|
Verdict: Approve with suggestions Before this PR, GAIA called its own Lemonade dead while the server was swapping models, and One gap is worth a follow-up. The time limit on "busy" only covers GAIA's own server. If a system install on the default port gets stuck while still answering liveness, GAIA now attaches to it with no limit. Before, it gave an immediate "port occupied" error. Real-world evidenceN/A here: no evidence bundle was produced for this run. The PR description reports a manual run against a real isolated lemond (2026.40.0). On main it showed "not answering… run stop"; on this branch it showed running and busy. That report is a prose summary, not captured output, so this verdict rests on the code review plus the new socket-level tests for the 🔍 Technical detailsIssues 🟢 Supervisor's busy check has no time limit ( 🟢 CLI evidence is prose, not output. The status output gains a new 🟢 Checked, no issue
Strengths
|
…monade-lifecycle-a3c88f # Conflicts: # src/gaia/llm/lemonade_embedded.py
A system Lemonade stuck with its router lock held still answers /live, so the supervisor would attach to it forever. Past lemond's request budget it now raises an error naming how to restart it, the same bound GAIA's own server already had. The port-retry test's fake lemond also stopped answering on macOS CI: its HTTPServer did a reverse DNS lookup on bind, and the test probe read the system proxy. Both fakes now bind without the lookup, and the probe goes direct.
|
Addressed all three suggestions, and fixed the red macOS check.
🔍 Technical details
|
|
🟡 The new "stuck server" timer never resets once the server recovers, so a later, ordinary model swap gets reported as a wedged server. The daemon keeps one supervisor for its whole life. The first time a system Lemonade is busy, its start time is recorded. That record is only cleared if the supervisor later sees the port open, not busy, and not healthy. When the server simply recovers and answers health, the record stays. Hours later the next model swap makes it busy again. The supervisor then measures from the first busy moment, decides the server has been busy for hours, and fails the request with "restart it". Clear the record whenever health answers, and add a test for busy → healthy → busy again. 🔍 Technical details
Simplest fix: in |
…orking The busy limit from the previous commit counted time between sightings, so a swap seen hours after an earlier one read as hours busy and `gaia init` restarted a working server, while a truly wedged one was never caught because nothing polls status() often enough. Each sighting now records the cumulative CPU and disk counters of lemond's backend processes; the server counts as stuck only when a later sighting finds exactly the same backends with exactly the same counters more than five minutes on. Any work, or a backend starting or exiting, restarts the record, and no evidence never yields a verdict. On a real lemond 2026.40.0 holding its router lock for an eviction, the llama-server's counters rose ~10 s per second and /api/v1/docs answered in 0.02 s; the supervisor's fingerprint test now serves that recorded body. The supervisor also drops its record whenever health answers, so a server that recovered starts fresh at its next swap.
…monade-lifecycle-a3c88f
|
Fixed. The supervisor now drops its record whenever health answers, on both probes and after the grace wait, so a server that recovered starts fresh at its next swap. That record also no longer measures time. It now holds the backends' CPU and disk counters, and a server counts as stuck only when they haven't moved at all for five minutes. So even a record that outlived a recovery can't flag a server that did any work since. |
…isteners An unreadable backend process was dropped from the activity snapshot, so a working server whose backend GAIA could not read looked idle and was called stuck after five minutes. It now counts as no evidence, which never yields a verdict. On macOS the supervisor could never find a system server's pid, because the machine-wide socket table needs root; it now walks the user's own processes when that table is refused. Tests now fail without each fix they cover: the record reset on a healthy probe and on clear, the supervisor attaching before the stranger grace, and the /docs format check, against a stranger that answers /live and /docs. The troubleshooting page and docstrings quote the current messages.
A loading server now reads as running and busy, so one the daemon reports as not answering is stuck: "wait and retry" no longer helps, `gaia init` does. The troubleshooting page gains an entry for the "stuck rather than loading a model" error the supervisor links to. Two more tests now fail without the code they cover: a server that turns busy during the stranger grace window, and a replaced backend that lands on the old one's exact counter.
… can't
A machine suspended past the quiet window froze the backends' counters while
the clock ran, so the first check after waking found identical counters more
than five minutes apart and called a working server stuck; `gaia init` then
restarted it. Both GAIA's own server and the supervisor now sample again a
few seconds later before that verdict, and any movement, or a sample that
can't be read, counts as busy.
The troubleshooting entry now quotes GAIA's own messages ("running but
stuck", "running but not answering") with the same remedy start() gives, and
an unrelated accordion icon changed by an earlier edit is restored.
…d#5352) 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. <details> <summary>🔍 Technical details</summary> - `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 amd#5314. Both add methods after `status()`, so they conflict on text: keep both. </details> ## Test plan - [x] `python -m pytest tests/unit/test_lemonade_orphaned_backends.py tests/unit/test_lemonade_embedded.py`: 91 passed, no stray processes left (Windows) - [x] The real-process tests on `origin/main`: the status and start cases fail with the symptom - [x] 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` - [x] `python util/lint.py --black --isort --flake8 --pylint --test-lanes`: all pass - [ ] CI: Unit Tests (Linux, Windows, macOS), Code Quality --------- Co-authored-by: Kalin Ovtcharov <[email protected]>
On a shared machine, GAIA called its own Lemonade dead whenever the server was swapping models, and told the user to stop it. Lemonade's health check waits on the lock that model eviction holds until the old model's request finishes, so it can miss GAIA's 3-second probe for minutes.
gaia initrestarted such a server, and the daemon's supervisor gave a "stranger on the port" error for a slow system install. Now a server that still answers Lemonade's lock-free liveness check reads as running and busy, so requests wait for it. One whose model backends then do no work at all for five minutes is still flagged, with the restart command; any work, or a backend starting or exiting, resets that. Separately, starting GAIA's server failed outright when another program took the chosen port before Lemonade could bind it. GAIA now picks a new port, logs the retry, and stops after three tries.Reproduced by
tests/unit/test_lemonade_shared_lifecycle.py: 5 tests fail on main, all pass here. On a real, isolated lemond (2026.40.0, CPU backend, port 18326), an/unloadwaiting on a running generation kept/healthpast 10 s for ~85 s while/liveanswered in 0.00 s. Main reported "not answering… Rungaia lemonade embedded stop". This branch reported running and busy, and reused the server.Captured output of that run
Merging with #5282: both change the
Popenthat launches lemond. Here that call moved into_spawn, so whichever lands second swapssubprocess.Popen(forpopen_outliving_job(there.🔍 Technical details
EmbeddedStatus.busy: the pid must pass_identify == OWNand/livemust return{"status":"ok"}./livecarries no key, so on its own it proves nothing about identity.busy_since.json. The server counts as stuck only when a later sighting finds the same backends with identical counters more than 300 s on, so the verdict holds however rarelystatus()is called. No evidence (processes not inspectable) never gives a verdict. Before a stuck verdict it samples again 3 s later, because a machine that slept past the window has frozen counters that move again on waking./api/v1/docsanswered in 0.02 s while/healthtimed out at 10 s. That recorded body is the fixture the supervisor test serves.load()after the router lock is released, so they never read as busy./liveand the/api/v1/docsindex ("format": "text/markdown", also lock-free) both answer. Anything else still gets the occupied-port error._port_freesays the port is held. All attempts share one deadline, and a retry needs at least 5 s left.daemon/lemonade.py: its "not answering" error now says the server is stuck and namesgaia init, instead of "may still be loading, wait and retry".init_command.pychanges only throughstatus(). If fix(lemonade): wait for GAIA's restarted model server instead of failing the turn #5264 lands first, its retryableEmbeddedLemonadeNotAnsweringfor this case now waits on a stuck server; that wait is bounded, so it's harmless, but it is worth a look when merging.status().code-revieweragent before the PR opened, and by the PR bot afterwards. Fixed from those reviews: a wedged server answering/liveread as healthy forever; a time-based limit that either restarted a working server or never caught a wedge (replaced by the counter evidence above); the/docsfingerprint; the retry deadline; the supervisor keeping a stale record; a macOS-only test hang in the fake lemond. A second review of the final design found nothing blocking. A third round's items are fixed too: unreadable backends counted as idle, the macOS listener lookup, and four tests that passed without the fix they cover (each was checked by removing that fix). Not changed: the C1 tests keep lemond's real 3 s probe (~6 s each) so they fail onmainfor the real reason; shrinking the timeout would make them pass onmain.Test plan
python -m pytest tests/unit/test_lemonade_shared_lifecycle.py: 31 passed (Windows). Removing each fix in turn (the record reset on health and on clear, the early attach, the busy check after the grace window, the pid comparison, the/docsformat check, and the daemon's stuck-not-loading remedy, and the wake-from-sleep re-sample on both paths) fails its test Onorigin/main, 5 fail with the real symptoms ("busy server reported as not running", the occupied-port error, "exited immediately")test_lemonade_embedded*,test_lemonade_supervisor,test_daemon_lemonade*,test_init_command,test_cli_lemonade_embedded, launcher/choice/restart tests, and 15 more files): 709 passed after merging main, isolatedGAIA_HOME, Windowspython util/lint.py --black --isort --flake8 --pylint --test-lanes: all pass/docsand backend-counter checks