Skip to content

fix(daemon): re-publish the lock file if it vanishes while serving - #484

Open
fstrasz wants to merge 2 commits into
chunkhound:mainfrom
fstrasz:fix/daemon-republish-lock
Open

fstrasz wants to merge 2 commits into
chunkhound:mainfrom
fstrasz:fix/daemon-republish-lock

Conversation

@fstrasz

@fstrasz fstrasz commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A proxy finds its daemon only through the runtime lock file (daemon-locks/<hash>.json), which also carries the auth token. We observed a daemon on Windows that was alive, listening on its TCP port and serving a connected client, while both its lock file and its registry entry were gone. Its own _graceful_shutdown had not run: the client was still connected. We have not established what removed the files.

The effect was that every new chunkhound mcp for that root found no lock and started a second daemon. That daemon then failed on the database the first one still held. This went on for at least two hours, until the first daemon's last client disconnected.

Change

chunkhound/daemon/server.py: while serving, the daemon checks its own publication every _PUBLICATION_CHECK_INTERVAL (5 s).

  • If its lock is missing, unreadable, or names a dead pid, it rewrites the lock with its own pid, socket path and the same auth token. It does the same for a missing registry entry.
  • A lock that names a different live pid is left alone, with a warning. DuckDB's file lock still decides which daemon owns the database.
  • A warning is written to the daemon log whenever it re-publishes, since a vanished lock is abnormal.

Lifecycle:

  • The check task starts only after a successful publish and the startup barrier, next to _pid_poll_task. A daemon that lost the startup race never runs it.
  • _graceful_shutdown cancels and awaits the task before remove_lock and remove_registry_entry. The check itself contains no await, so it cannot re-publish after shutdown removes the files.
  • write_lock still records process_started_at for the daemon's own process, so stop_daemon's identity check is unchanged.

Test

tests/unit/test_daemon_republish_lock.py::test_serving_daemon_republishes_deleted_lock runs the real run() loop with a mocked initialize and IPC server, a 0.05 s interval, and real discovery files under tmp_path. It deletes the lock and the registry entry, then asserts both reappear with the same pid, auth token and socket path. It also asserts the lock is gone after shutdown. With the task's create_task line removed, it fails with ...daemon-locks\<hash>.json did not appear.

The existing daemon tests (tests/test_daemon_smoke.py, tests/test_daemon_startup_order.py and the tests/unit/test_daemon_*.py files) give the same result with and without the change: 92 passed, 4 skipped, 7 failed. All 7 failures are in test_daemon_smoke.py (SubprocessCrashError) on Windows, and they fail on main too.

Live check on a fork build: with a daemon serving, deleting its lock and registry entry by hand had both re-published within a second with the same pid and token, and a new proxy then attached to the running daemon.

When running these tests locally, set both CHUNKHOUND_DAEMON_RUNTIME_DIR and CHUNKHOUND_DAEMON_REGISTRY_DIR to temporary directories. With only the first set, the registry still points at the real directory that live daemons use.

🤖 Generated with Claude Code, reviewed by FStrasz

fstrasz and others added 2 commits November 7, 2026 09:37
A daemon was observed alive, listening and serving a connected client
after its discovery lock file and registry entry had disappeared. The
lock is the only way a proxy finds a daemon (it carries the auth token),
so every new proxy for that root started a second daemon, which crashed
on the DB the first one held.

While serving, the daemon now checks its publication every few seconds
and rewrites a missing lock with its own pid, socket path and the same
auth token, plus a missing registry entry, logging a warning to its log.
A lock naming a different live pid is left alone. The check is cancelled
first in _graceful_shutdown, so it can never re-publish after shutdown
removes the lock.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Brings in the 24 commits merged to main since this branch was last updated, including chunkhound#534's rust-checks fix. No conflicts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant