Skip to content

fix(tui): let gaia daemon start take the start lock on a cold start - #4261

Merged
kovtcharov-amd merged 1 commit into
amd:mainfrom
kovtcharov:claude/tui-daemon-start-lock
Sep 25, 2026
Merged

kovtcharov-amd merged 1 commit into
amd:mainfrom
kovtcharov:claude/tui-daemon-start-lock

Conversation

@kovtcharov

Copy link
Copy Markdown
Contributor

With no daemon running, the TUI could never start one. It held the start lock while it ran gaia daemon start, and that command waits on the same lock, so /agents and daemon-agent preflight failed after 30 s with an error that blamed a stuck port or import. Now the TUI holds the lock only long enough to re-check for a running daemon and refuse to replace a wedged one. It releases the lock before spawning. The launcher still serialises the real start under that lock, so two TUIs starting at once still get one daemon.

Test plan

  • go vet ./... && go test -race ./... in tui/
  • New unit test: a fake launcher that exits 1 if it can't take the start lock. It fails on main and passes with this change: go test ./internal/daemon -run StartOrAttach
  • New e2e test: two concurrent callers against the real gaia daemon start with GAIA_DAEMON_HOME=<tmp>. Both get the same daemon pid. On main it times out after 30 s. GAIA_E2E_PYTHON=<venv python> go test ./test -run ColdStart (it skips when no Python can import the daemon)
  • Python daemon tests: pytest tests/unit -k daemon (551 passed)

Fixes #4200

With no daemon running, the TUI could never start one: it held the start lock while running `gaia daemon start`, which waits on that same lock, so every cold start timed out after 30 s and blamed a stuck port or import.

The TUI now holds the lock only to re-check the registry and refuse a wedged daemon, then releases it before spawning. The launcher serialises the actual start under the same lock, so concurrent callers still get one daemon.

Fixes amd#4200
@github-actions github-actions Bot added documentation Documentation changes tui Go terminal UI (gaia-tui) labels Sep 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve

This fixes a real cold-start deadlock: the TUI was holding the daemon start lock while running the command that waits on that same lock, so with no daemon already running the TUI could never start one and failed after 30 seconds with a misleading "stuck binding a port" message. The new flow holds the lock only long enough to re-check for a running daemon, then hands it off to the launcher. Single-instance behaviour is preserved because the launcher still serialises the actual start.

No blocking issues. I confirmed the fix and its test independently (below). One thing worth the author's attention: the new end-to-end test can only run on a machine that already has GAIA's Python environment, so in CI it will always skip — the unit test is what actually guards the regression there. Either give that test a lane with the Python env, or say in the CI workflow that it is a local-only check, so nobody later assumes CI covers it.

Real-world evidence

No evidence-bundle.md was produced for this PR, so the automated evidence stage did not run here. I ran the checks myself in this review checkout:

$ cd tui && go vet ./internal/daemon ./test   # clean
$ go test -race -count=1 ./internal/daemon
ok  	github.com/amd/gaia/tui/internal/daemon	2.607s

The new unit test is a genuine regression guard — swapping in the pre-fix client.go reproduces the deadlock exactly:

--- FAIL: TestStartOrAttachReleasesTheStartLockBeforeSpawning (0.00s)
    a launcher that takes the start lock itself must be able to start the daemon:
    `…daemon.test -test.run=^TestHelperProcess$` exited with exit status 1 before a daemon registered.
    Launcher output:
        helper: start lock is held by the caller (<nil>).

The new e2e test skips in an environment without GAIA's Python deps, which is what CI will hit:

--- SKIP: TestColdStartThroughTheRealGaiaDaemonStart (0.16s)
    /bin/python3 cannot import the GAIA daemon from …/src: No module named 'dotenv'

The TUI cold start itself (launching the TUI with no daemon and opening /agents) is not shown on the PR and could not be exercised here — that part of the verdict rests on the e2e test the author reports running locally plus static review.

🔍 Technical details

Design check. The premise holds end to end: gaia daemon start → _handle_daemon_start → client.start_or_attach → with StartLock(...) (src/gaia/daemon/client.py:94). On POSIX both sides use flock on ~/.gaia/host/instance.lock (tui/internal/daemon/lock_unix.go vs src/gaia/daemon/lock.py:47), so the two locks genuinely interoperate, and acquireLock opens its own fd per call so two goroutines in one process still exclude each other. Single-instance now rests on the Python lock around the spawn, which tui/test/daemon_cold_start_e2e_test.go:245 asserts by comparing the pids two concurrent callers land on. acquireLock is called from exactly one site (client.go:226), so there is no other path that could still hold the lock across a launcher run.

🟢 The new e2e test will never run in CI (tui/test/daemon_cold_start_e2e_test.go:182). build_tui.yml runs go test ./... for tui/test on Linux/macOS but installs only Go, so the import gaia.cli, gaia.daemon.server probe fails and the test skips — verified above. Worth either adding a pip install -e . step (or GAIA_E2E_PYTHON) to that lane, or naming the gap in build_tui.yml the way its Windows exclusion comment already does, so the test does not quietly rot.

🟢 Concurrent callers can burn 2× StartTimeout (client.go:226 + client.go:326). checkUnderLock waits up to StartTimeout for the lock and spawnAndWait then takes a fresh StartTimeout context, so a losing caller can spend 60s before reporting failure while the comments and plan doc say "30s cap". Pre-existing arithmetic, but this change makes it reachable for the first time (it used to deadlock instead). Not worth restructuring; just be aware the documented cap is per-phase, not total.

🟢 The e2e assertion hardcodes two callers (daemon_cold_start_e2e_test.go:245). insts[0].PID != insts[1].PID silently stops covering the extra callers if const callers is ever raised.

	for i := 1; i < callers; i++ {
		if insts[i].PID != insts[0].PID {
			t.Errorf("concurrent callers started two daemons: pid %d and pid %d", insts[0].PID, insts[i].PID)
		}
	}

Strengths

  • The fake launcher that exits 1 when it cannot take the lock is the right shape for this bug — it encodes the contract (only the launcher holds the lock) rather than the symptom, and I confirmed it fails on the pre-fix code.
  • All three places that described the old invariant were updated together: the Options.StartCommand contract, the fileLock doc comment, and docs/plans/email-agent-tui-port.md:73. The fileLock comment now correctly names the Python launcher as the co-owner of the lock.
  • Extracting checkUnderLock keeps the wedged-daemon refusal (client.go:250) and the version-skew short-circuit intact rather than rewriting the decision logic to fix a locking bug.
  • gaia daemon stop exits 0 when nothing is running (cli.py:7140), so the e2e cleanup cannot produce a spurious failure on top of a real one.

@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Sep 25, 2026
Merged via the queue into amd:main with commit bcabb33 Sep 25, 2026
43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Documentation changes tui Go terminal UI (gaia-tui)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(tui): TUI cannot cold-start the daemon — it holds the lock gaia daemon start waits on

2 participants