Repository navigation
fix(daemon): start one daemon when clients race on a cold start - #336
Open
hatemadi15 wants to merge 1 commit into
Open
hatemadi15 wants to merge 1 commit into
hatemadi15 wants to merge 1 commit into
Conversation
hatemadi15
marked this pull request as ready for review
November 9, 2026 19:42
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When two clients find no daemon at the same moment, both start one. A fresh
ccc mcpsession does this on its firstsearch, which races the background index. So do twoccccommands run together, two clients that both notice aglobal_settings.ymlchange, andccc daemon restartunder the Docker entrypoint's supervisor loop. Nothing serializes the start, and on POSIX each new daemon unlinks the socket path and binds its own. The daemon that bound first keeps running where no client can reach it: it is reparented to init, and if it already served a request it holds the project's LMDB and SQLite files open.ccc daemon stopstops the reachable daemon, then reports "Daemon is not running." while the other is still alive. The unreachable daemon exits only at its idle timeout: 180 minutes by default, never under the supervisor.Change
daemon.lockin its runtime dir (flock;msvcrt.lockingon Windows) before anything else, and releases it right after closing its listener. A daemon that finds the lock held exits at once with status 75 (DAEMON_ALREADY_RUNNING_EXIT_CODE), before it reads settings or touches the PID file, last-exit marker, log or socket._UnixListenerstill removes a stale socket, but only a lock holder gets that far now._wait_for_daemon()treats status 75 as "another daemon got there first" and keeps waiting for that daemon's socket, so both clients end up on one daemon. After the handshake, a client whose own process is not the daemon that answered waits for that process to exit; otherwise the MCP server would keep it as a zombie ([ccc] <defunct>) until its next spawn.COCOINDEX_CODE_DAEMON_SUPERVISED=1, the Docker image),ccc daemon restartnow waits for the supervisor's respawn instead of starting a daemon itself, as auto-start already does. Starting its own raced the entrypoint loop.#243 takes the other approach: a lock file around the client's check → spawn → wait. This PR puts the lock in the daemon, because the displacement happens there and not every daemon is started by a client that would take a client-side lock. The Docker entrypoint's loop and a plain
ccc run-daemonstart one directly, andccc daemon restartcallsstart_daemon()outside #243's lock. noVaSon reported theccc run-daemoncase in #243's thread and suggested this fix: a lock the daemon holds for its lifetime. The two PRs change the same start-up code inclient.py, so they conflict. #243 also raises the listener backlog from 1 to 64; this PR leaves the backlog alone. Racing clients still each spawn a process; the loser exits about 0.3 s after it starts, without reading settings or serving anything.On Windows,
PipeListenercreates its first pipe instance withFILE_FLAG_FIRST_PIPE_INSTANCE, so a second daemon could never displace the first: it failed atListener()withPermissionError, but only after overwritingdaemon.pid, deleting the last-exit marker and truncatingdaemon.log. It now exits before touching any of them.Validation
uv run pytest tests/: 353 passed, 8 deselected (docker_e2e).uv run prek run --all-files: all hooks pass.uv run mypy .: 2 errors inscripts/find_best_models.py, identical onmain; CI's hook (mypy src/ tests/) is clean.test_concurrent_cold_starts_leave_one_daemon(two clients start daemons at once; one exits with status 75, the other serves both),test_crashed_daemon_leftovers_do_not_block_the_next_start,test_daemon_restart_leaves_the_start_to_the_supervisor. Against main'sdaemon.py,client.pyandcli.pyall three fail (expected one daemon left running and one exited at once, got [None, None]; nodaemon.lock;ccc daemon restartstarted a daemon under a supervisor). The two e2e tests passed 15/15 repeated runs, and 5/5 with three CPU-bound processes competing.HOME/COCOINDEX_CODE_DIR, counting that dir'sccc run-daemonprocesses withps:mainccc daemon status, coldccc daemon stopleaves it running and then says "Daemon is not running."ccc run-daemonexited 75;ccc daemon stopleaves 0ccc daemon status, cold (the case #243 tests)ccc daemon stopleaves 5 runningccc daemon stopleaves 0ccc daemon status, then a secondccc run-daemon(the case from #243's thread)daemon.pidand the socket;ccc daemon stopstops it, the first keeps running, and a secondstopsays "Daemon is not running."daemon.pidand the socket are unchanged;ccc daemon stopleaves 0ccc mcp+searchright afterinitializeccc index0.3 s apartccc daemon restartunderdocker/entrypoint.shccc daemon statusccc daemon restart,stop,stopagain ("Daemon is not running.") andrestartwith nothing running behave as before; a SIGKILLed daemon leavesdaemon.sock,daemon.lockanddaemon.pidbehind and the nextccc daemon statusstarts a new daemon; withidle_timeout_minutes: 1the daemon idle-exited after 122 s (reason: idle_timeout) and the next command started a new one without a warning.Notes
msvcrt.lockingraisingPermissionErroron a held byte and releasing it on process exit, so CI's windows-latest job is its first real run.CLAUDE.md's Process Model section gains one sentence about the lock.