Skip to content

Add PID to .ninja_lock, warn if another instance is running in builddir - #2770

Open
jhasse wants to merge 2 commits into
ninja-build:masterfrom
jhasse:pid-in-lock
Open

Add PID to .ninja_lock, warn if another instance is running in builddir#2770
jhasse wants to merge 2 commits into
ninja-build:masterfrom
jhasse:pid-in-lock

Conversation

@jhasse

@jhasse jhasse commented May 8, 2026

Copy link
Copy Markdown
Collaborator

The warning might seem like a regression, but technically the use case of running two ninjas in the same build directory was never really supported - it works accidentally and we might break it at any time.

Although not the intention of this, this should fix #2762, because now the lock file isn't empty anymore, which was a problem on some network filesystems where the mtime wasn't updated in that case.

TODO

  • Accessing the file is kind of racy
  • PIDs might be reused?
  • Should .ninja_lock be deleted when Ninja quits?
The warning might seem like a regression, but technically the use case
of running two ninjas in the same build directory was never really
supported - it works accidentally and we might break it at any time.

Although not the intention of this, this should fix ninja-build#2762, because now
the lock file isn't empty anymore, which was a problem on some network
filesystems where the mtime wasn't updated in that case.
Comment thread src/build.cc
IsProcessRunning(static_cast<int>(pid))) {
Warning("another ninja process (pid %ld) seems to be running in this "
"build directory; if it is not, delete %s",
pid, lock_file_path_.c_str());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be a Fatal() error instead. Two concurrent Ninjas are likely to break everything, from the .ninja_deps and .ninja_log files, and the content of output files when both launch the same command that want to write to the same output using a tool that doesn't support that well (e.g. without atomically renaming the output file).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately ninja calling ninja again on the same build dir is used in the wild. And it "works" right now, a fatal error would be a hard regression.

When thinking about it even the warning is too much. We need to correctly fix this (the second ninja process communicates to the first process which targets it wants to run).

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

Labels

None yet

2 participants