Skip to content

Refuse to start a second API process on the same data directory - #614

Draft
yummybomb wants to merge 3 commits into
mainfrom
hypeship/data-dir-lock
Draft

yummybomb wants to merge 3 commits into
mainfrom
hypeship/data-dir-lock

Conversation

@yummybomb

@yummybomb yummybomb commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The API takes an exclusive, nonblocking flock on <data_dir>/.hypeman.lock before loading guest state. It exits if another API process already holds the lock. The lock file is created with mode 0600 and kept across restarts, so a user with only read access to the directory cannot take the lock.

Instance locks exist only inside one process. A second API process started against the same data directory runs the whole of startup against guests the first one owns before it fails to bind the port. The binary has no flags, so running it to look for a version is enough to start one.

The configuration is loaded once. data_dir is cleaned the same way guest storage paths are before the API takes the lock. The lock stays held until process exit, even if startup returns an error or background jobs are still writing during shutdown. Executed child programs do not inherit it.

Split out of #555. It stands alone, and #555's fork recovery relies on it: with one writer guaranteed, a fork still marked at startup has no live owner.

Upgrade note

Stop the old API before upgrading or downgrading; older binaries do not take this lock. Moving storage, changing its mount, or repointing a data_dir symlink requires stopping the guests and the API first. Existing guest metadata and sockets retain the configured path (DEVELOPMENT.md).

Testing

  • TestServerRefusesSecondWriterBeforeStartup starts the server in a subprocess against a locked data directory, reached through a symlink, and checks it exits at the lock without creating any guest or system state.
  • TestServerKeepsStorageDirectoryLockedUntilExit checks the storage directory reached through a path containing a symlink and ... A failed startup still holds that lock after run() returns; the next owner can acquire it after the process exits.
  • A subprocess locks the file and execs a fresh test binary; the new program must be able to acquire the lock. Tests also check that public permissions and a symlink at the lock-file path are rejected.
  • All five lock tests passed ten consecutive runs on Linux (Go 1.26.10). The API, config, and provider package tests and go vet passed.
  • Earlier staging comparison, using the directory-inode version of this PR, in a container with its own network and PID namespaces and one guest running:
First process Second process What the second one did
main main Initialized the network manager, reconciled devices and vGPUs, started the restart policy controller and TTL reaper, then exited on bind: address already in use (74 log lines)
this branch this branch Exited at the lock after 2 log lines, before touching the directory
this branch main Ran all of startup as before. An older binary does not take the lock

Not covered

  • The lock does not protect against an older binary started second, as the last row shows.
  • The data directory must not be writable by untrusted users. Do not remove or replace .hypeman.lock while the API is running; file existence does not indicate whether the lock is held.

Instance locks exist only inside one process, so a second API process
started against the same data directory could save over the first one's
metadata and run its startup reconcilers against guests the first one
owns. The binary has no flags, so running it to look for a version was
enough to start one.

The API now takes an exclusive flock on the data directory before it
reads or writes anything in it, and exits if another process holds the
lock. The configuration is loaded once, so the directory that is locked
is the one the rest of startup uses.

Older binaries do not take the lock, so the old process has to be
stopped before this one starts.
Keep the lock on one persistent inode without requiring private read access to the whole data directory. Check its type, owner and permissions before flock, and verify that exec releases the descriptor.

Document storage cutovers as offline operations: guest metadata and socket paths retain the configured directory name.

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