Skip to content

feat: add PID lockfile guard to prevent duplicate instances#565

Closed
Lafunamor wants to merge 1 commit into
nanocoai:mainfrom
Lafunamor:feature/pid-lockfile
Closed

feat: add PID lockfile guard to prevent duplicate instances#565
Lafunamor wants to merge 1 commit into
nanocoai:mainfrom
Lafunamor:feature/pid-lockfile

Conversation

@Lafunamor

Copy link
Copy Markdown
Contributor

Summary

  • Adds acquirePidLock() called at the very start of main() that writes the current process PID to data/nanoclaw.pid
  • If the lock file already exists and the recorded process is still alive, NanoClaw logs an error and exits immediately — preventing two instances from corrupting shared state (SQLite WAL, IPC directories, credential files)
  • Stale lock files (process no longer running) are silently cleaned up and overwritten
  • The lock file is removed automatically via a process.on('exit') handler on clean shutdown

Motivation

Running npm run dev twice, or a service manager restarting before a previous instance fully exits, leads to two NanoClaw processes racing on the same SQLite database and IPC queues. The PID lock is a lightweight, dependency-free safeguard.

Files changed

  • src/index.ts — adds DATA_DIR import, acquirePidLock() function (~25 lines), and a single call in main()

Test plan

  • Start NanoClaw normally — verifies data/nanoclaw.pid is written with the correct PID
  • Start a second instance while the first is running — verifies it exits immediately with an error log
  • Kill the first instance ungracefully (kill -9), then start again — verifies the stale lock file is cleaned up and startup succeeds
  • Graceful shutdown (SIGTERM) — verifies the PID file is removed on exit

🤖 Generated with Claude Code

@Andy-NanoClaw-AI Andy-NanoClaw-AI added PR: Feature New feature or enhancement Status: Needs Review Ready for maintainer review labels Mar 5, 2026
@Lafunamor Lafunamor force-pushed the feature/pid-lockfile branch from 2d7495a to fbfb866 Compare March 5, 2026 10:31
@Lafunamor Lafunamor force-pushed the feature/pid-lockfile branch 2 times, most recently from e9020b7 to d627aba Compare March 14, 2026 15:04
@Lafunamor Lafunamor force-pushed the feature/pid-lockfile branch 4 times, most recently from 09e77d7 to 4e22fdc Compare March 20, 2026 10:22
Adds acquirePidLock() called at startup that writes the current PID to
data/nanoclaw.pid and exits if another live process already holds the
lock. Stale lock files (process no longer running) are cleaned up
automatically.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Lafunamor Lafunamor force-pushed the feature/pid-lockfile branch from 4e22fdc to ccdd8cc Compare March 22, 2026 13:54
@gavrielc

Copy link
Copy Markdown
Collaborator

Closing as part of a stale issue/PR cleanup. If this is still relevant, please reopen and we'll take another look.

@gavrielc gavrielc closed this May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Feature New feature or enhancement Status: Needs Review Ready for maintainer review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants