-
Notifications
You must be signed in to change notification settings - Fork 37
Made SpawnProcess() behavior safe post fork() #237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Sjors
wants to merge
4
commits into
bitcoin-core:master
Choose a base branch
from
Sjors:2026/01/fork-safu
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+163
−15
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| // Copyright (c) The Bitcoin Core developers | ||
| // Distributed under the MIT software license, see the accompanying | ||
| // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
|
||
| #include <mp/util.h> | ||
|
|
||
| #include <kj/test.h> | ||
|
|
||
| #include <chrono> | ||
| #include <compare> | ||
| #include <condition_variable> | ||
| #include <csignal> | ||
| #include <cstdlib> | ||
| #include <mutex> | ||
| #include <string> | ||
| #include <sys/wait.h> | ||
| #include <thread> | ||
| #include <unistd.h> | ||
| #include <vector> | ||
|
|
||
| namespace { | ||
|
|
||
| // Poll for child process exit using waitpid(..., WNOHANG) until the child exits | ||
| // or timeout expires. Returns true if the child exited and status_out was set. | ||
| // Returns false on timeout or error. | ||
| static bool WaitPidWithTimeout(int pid, std::chrono::milliseconds timeout, int& status_out) | ||
| { | ||
| const auto deadline = std::chrono::steady_clock::now() + timeout; | ||
| while (std::chrono::steady_clock::now() < deadline) { | ||
| const int r = ::waitpid(pid, &status_out, WNOHANG); | ||
| if (r == pid) return true; | ||
| if (r == 0) { | ||
| std::this_thread::sleep_for(std::chrono::milliseconds{1}); | ||
| continue; | ||
| } | ||
| // waitpid error | ||
| return false; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| KJ_TEST("SpawnProcess does not run callback in child") | ||
| { | ||
| // This test is designed to fail deterministically if fd_to_args is invoked | ||
| // in the post-fork child: a mutex held by another parent thread at fork | ||
| // time appears locked forever in the child. | ||
| std::mutex target_mutex; | ||
| std::mutex control_mutex; | ||
| std::condition_variable control_cv; | ||
| bool locked{false}; | ||
| bool release{false}; | ||
|
|
||
| // Holds target_mutex until the releaser thread updates release | ||
| std::thread locker([&] { | ||
| std::unique_lock<std::mutex> target_lock(target_mutex); | ||
| { | ||
| std::lock_guard<std::mutex> g(control_mutex); | ||
| locked = true; | ||
| } | ||
| control_cv.notify_one(); | ||
|
|
||
| std::unique_lock<std::mutex> control_lock(control_mutex); | ||
| control_cv.wait(control_lock, [&] { return release; }); | ||
| }); | ||
|
|
||
| // Wait for target_mutex to be held by the locker thread. | ||
| { | ||
| std::unique_lock<std::mutex> l(control_mutex); | ||
| control_cv.wait(l, [&] { return locked; }); | ||
| } | ||
|
|
||
| // Release the lock shortly after SpawnProcess starts. | ||
| std::thread releaser([&] { | ||
| // In the unlikely event a CI machine overshoots this delay, a | ||
| // regression could be missed. This is preferable to spurious | ||
| // test failures. | ||
| std::this_thread::sleep_for(std::chrono::milliseconds{50}); | ||
| { | ||
| std::lock_guard<std::mutex> g(control_mutex); | ||
| release = true; | ||
| } | ||
| control_cv.notify_one(); | ||
| }); | ||
|
|
||
| int pid{-1}; | ||
| const int fd{mp::SpawnProcess(pid, [&](int child_fd) -> std::vector<std::string> { | ||
| // If this callback runs in the post-fork child, target_mutex appears | ||
| // locked forever (the owning thread does not exist), so this deadlocks. | ||
| std::lock_guard<std::mutex> g(target_mutex); | ||
| return {"true", std::to_string(child_fd)}; | ||
| })}; | ||
| ::close(fd); | ||
|
|
||
| int status{0}; | ||
| // Give the child up to 1 second to exit. If it does not, terminate it and | ||
| // reap it to avoid leaving a zombie behind. | ||
| const bool exited{WaitPidWithTimeout(pid, std::chrono::milliseconds{1000}, status)}; | ||
| if (!exited) { | ||
| ::kill(pid, SIGKILL); | ||
| ::waitpid(pid, &status, /*options=*/0); | ||
| } | ||
|
|
||
| releaser.join(); | ||
| locker.join(); | ||
|
|
||
| KJ_EXPECT(exited, "Timeout waiting for child process to exit"); | ||
| KJ_EXPECT(WIFEXITED(status) && WEXITSTATUS(status) == 0); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In commit "Precompute argv before fork in SpawnProcess" (9d3c2b6)
Keeping perror is probaby the best choice for now even though strictly speaking it might not be safe and could hang. Would suggest adding a comment like "// NOTE: perror() is not async-signal-safe; calling it here in a post-fork child may still deadlock in multithreaded parents."
Note for future todo: It would be nice to improve this later by adding an error-reporting pipe, writing errno to it so parent process can provide a better diagnostic to code calling it. This idea came up previously in #213 (comment) / 286fe46
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a note and TODO