Skip to content

feat: prevent multiple servers on the same index directory#26

Merged
shengyfu merged 2 commits intomainfrom
server-lock
Apr 8, 2026
Merged

feat: prevent multiple servers on the same index directory#26
shengyfu merged 2 commits intomainfrom
server-lock

Conversation

@shengyfu
Copy link
Copy Markdown
Member

@shengyfu shengyfu commented Apr 8, 2026

Summary

Prevents multiple tgrep serve instances from running on the same index directory, which could cause index corruption and serve.json overwrites.

Changes

  • serve.rs: Added try_acquire_server_lock() using fs2 crate for cross-platform exclusive file locking on serve.lock
  • Cargo.toml: Added fs2 dependency
  • Integration test: Verifies second server exits with clear error message

Behavior

When a second server attempts to start on the same index directory:

Error: another tgrep server is already running for index directory .tgrep (pid 12345, port 49152). Stop the existing server before starting a new one.

The lock is held for the server lifetime and automatically released on exit (normal, crash, or kill).

shengyfu and others added 2 commits April 7, 2026 23:23
Add an exclusive file lock (serve.lock) acquired at server startup using
the fs2 crate. If another tgrep server already holds the lock, the second
server exits immediately with a clear error message including the existing
server's PID and port.

The lock is held for the lifetime of the server process and automatically
released on exit (normal shutdown, crash, or kill).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@shengyfu shengyfu merged commit f97edbc into main Apr 8, 2026
9 checks passed
@shengyfu shengyfu deleted the server-lock branch April 8, 2026 06:33
@shengyfu shengyfu restored the server-lock branch April 8, 2026 14:14
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