Skip to content

Commit 75a5c82

Browse files
committed
Merge bitcoin#33063: util: Revert "common: Close non-std fds before exec in RunCommandJSON"
faa1c3e Revert "Merge bitcoin#32343: common: Close non-std fds before exec in RunCommandJSON" (MarcoFalke) Pull request description: After a fork() in a multithreaded program, the child can safely call only async-signal-safe functions (see [signal-safety(7)](https://www.man7.org/linux/man-pages/man7/signal-safety.7.html)) until such time as it calls execv. The standard library (`std` namespace) is not async-signal-safe. Also, `throw`, isn't. There was an alternative implementation using `readdir` (bitcoin#32529), but that isn't async-signal-safe either, and that implementation was still using `throw`. So temporarily revert this feature. A follow-up in the future can add it back, using only async-signal-safe functions, or by using a different approach. Fixes bitcoin#32524 Fixes bitcoin#33015 Fixes bitcoin#32855 For reference, a failure can manifest in the GCC debug mode: * While `fork`ing, a debug mode mutex is held (by any other thread). * The `fork`ed child tries to use the stdard libary before `execv` and deadlocks. This may look like the following: ``` (gdb) thread apply all bt Thread 1 (Thread 0xf58f4b40 (LWP 774911) "b-httpworker.2"): #0 0xf7f4f589 in __kernel_vsyscall () bitcoin#1 0xf79e467e in ?? () from /lib32/libc.so.6 #2 0xf79eb582 in pthread_mutex_lock () from /lib32/libc.so.6 #3 0xf7d93bf2 in ?? () from /lib32/libstdc++.so.6 #4 0xf7d93f36 in __gnu_debug::_Safe_iterator_base::_M_attach(__gnu_debug::_Safe_sequence_base*, bool) () from /lib32/libstdc++.so.6 #5 0x5668810a in __gnu_debug::_Safe_iterator_base::_Safe_iterator_base (this=0xf58f13ac, __seq=0xf58f13f8, __constant=false) at /bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/debug/safe_base.h:91 #6 0x56ddfb50 in __gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<int*, std::__cxx1998::vector<int, std::allocator<int> > >, std::__debug::vector<int, std::allocator<int> >, std::forward_iterator_tag>::_Safe_iterator (this=0xf58f13a8, __i=3, __seq=0xf58f13f8) at /bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/debug/safe_iterator.h:162 #7 0x56ddfacb in __gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<int*, std::__cxx1998::vector<int, std::allocator<int> > >, std::__debug::vector<int, std::allocator<int> >, std::bidirectional_iterator_tag>::_Safe_iterator (this=0xf58f13a8, __i=3, __seq=0xf58f13f8) at /bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/debug/safe_iterator.h:539 #8 0x56ddfa5b in __gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<int*, std::__cxx1998::vector<int, std::allocator<int> > >, std::__debug::vector<int, std::allocator<int> >, std::random_access_iterator_tag>::_Safe_iterator (this=0xf58f13a8, __i=3, __seq=0xf58f13f8) at /bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/debug/safe_iterator.h:687 #9 0x56ddd3f6 in std::__debug::vector<int, std::allocator<int> >::begin (this=0xf58f13f8) at /bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/debug/vector:300 #10 0x57d83701 in subprocess::detail::Child::execute_child (this=0xf58f156c) at ./util/subprocess.h:1372 #11 0x57d80a7c in subprocess::Popen::execute_process (this=0xf58f1cd8) at ./util/subprocess.h:1231 #12 0x57d6d2b4 in subprocess::Popen::Popen<subprocess::input, subprocess::output, subprocess::error, subprocess::close_fds> (this=0xf58f1cd8, cmd_args="fake.py enumerate", args=..., args=..., args=..., args=...) at ./util/subprocess.h:964 #13 0x57d6b597 in RunCommandParseJSON (str_command="fake.py enumerate", str_std_in="") at ./common/run_command.cpp:27 #14 0x57a90547 in ExternalSigner::Enumerate (command="fake.py", signers=std::__debug::vector of length 0, capacity 0, chain="regtest") at ./external_signer.cpp:28 #15 0x56defdab in enumeratesigners()::$_0::operator()(RPCHelpMan const&, JSONRPCRequest const&) const (this=0xf58f2ba0, self=..., request=...) at ./rpc/external_signer.cpp:51 ... (truncated, only one thread exists) ``` ACKs for top commit: fanquake: ACK faa1c3e darosior: ACK faa1c3e Tree-SHA512: 602da5f2eba08d7fe01ba19baf411e287ae27fe2d4b82f41734e05b7b1d938ce94cc0041e86ba677284fa92838e96ebee687023ff28047e2b036fd9a53567e0a
2 parents 3b188b8 + faa1c3e commit 75a5c82

File tree

2 files changed

+1
-59
lines changed

2 files changed

+1
-59
lines changed

src/common/run_command.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ UniValue RunCommandParseJSON(const std::string& str_command, const std::string&
2424

2525
if (str_command.empty()) return UniValue::VNULL;
2626

27-
auto c = sp::Popen(str_command, sp::input{sp::PIPE}, sp::output{sp::PIPE}, sp::error{sp::PIPE}, sp::close_fds{true});
27+
auto c = sp::Popen(str_command, sp::input{sp::PIPE}, sp::output{sp::PIPE}, sp::error{sp::PIPE});
2828
if (!str_std_in.empty()) {
2929
c.send(str_std_in);
3030
}

src/util/subprocess.h

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ Documentation for C++ subprocessing library.
3636
#ifndef BITCOIN_UTIL_SUBPROCESS_H
3737
#define BITCOIN_UTIL_SUBPROCESS_H
3838

39-
#include <util/fs.h>
40-
#include <util/strencodings.h>
4139
#include <util/syserror.h>
4240

4341
#include <algorithm>
@@ -538,20 +536,6 @@ namespace util
538536
* -------------------------------
539537
*/
540538

541-
/*!
542-
* Option to close all file descriptors
543-
* when the child process is spawned.
544-
* The close fd list does not include
545-
* input/output/error if they are explicitly
546-
* set as part of the Popen arguments.
547-
*
548-
* Default value is false.
549-
*/
550-
struct close_fds {
551-
explicit close_fds(bool c): close_all(c) {}
552-
bool close_all = false;
553-
};
554-
555539
/*!
556540
* Base class for all arguments involving string value.
557541
*/
@@ -749,7 +733,6 @@ struct ArgumentDeducer
749733
void set_option(input&& inp);
750734
void set_option(output&& out);
751735
void set_option(error&& err);
752-
void set_option(close_fds&& cfds);
753736

754737
private:
755738
Popen* popen_ = nullptr;
@@ -1043,8 +1026,6 @@ class Popen
10431026
std::future<void> cleanup_future_;
10441027
#endif
10451028

1046-
bool close_fds_ = false;
1047-
10481029
std::string exe_name_;
10491030

10501031
// Command in string format
@@ -1293,10 +1274,6 @@ namespace detail {
12931274
if (err.rd_ch_ != -1) popen_->stream_.err_read_ = err.rd_ch_;
12941275
}
12951276

1296-
inline void ArgumentDeducer::set_option(close_fds&& cfds) {
1297-
popen_->close_fds_ = cfds.close_all;
1298-
}
1299-
13001277

13011278
inline void Child::execute_child() {
13021279
#ifndef __USING_WINDOWS__
@@ -1343,41 +1320,6 @@ namespace detail {
13431320
if (stream.err_write_ != -1 && stream.err_write_ > 2)
13441321
subprocess_close(stream.err_write_);
13451322

1346-
// Close all the inherited fd's except the error write pipe
1347-
if (parent_->close_fds_) {
1348-
// If possible, try to get the list of open file descriptors from the
1349-
// operating system. This is more efficient, but not guaranteed to be
1350-
// available.
1351-
#ifdef __linux__
1352-
// For Linux, enumerate /proc/<pid>/fd.
1353-
try {
1354-
std::vector<int> fds_to_close;
1355-
for (const auto& it : fs::directory_iterator(strprintf("/proc/%d/fd", getpid()))) {
1356-
auto fd{ToIntegral<uint64_t>(it.path().filename().native())};
1357-
if (!fd || *fd > std::numeric_limits<int>::max()) continue;
1358-
if (*fd <= 2) continue; // leave std{in,out,err} alone
1359-
if (*fd == static_cast<uint64_t>(err_wr_pipe_)) continue;
1360-
fds_to_close.push_back(*fd);
1361-
}
1362-
for (const int fd : fds_to_close) {
1363-
close(fd);
1364-
}
1365-
} catch (const fs::filesystem_error &e) {
1366-
throw OSError("/proc/<pid>/fd iteration failed", e.code().value());
1367-
}
1368-
#else
1369-
// On other operating systems, iterate over all file descriptor slots
1370-
// and try to close them all.
1371-
int max_fd = sysconf(_SC_OPEN_MAX);
1372-
if (max_fd == -1) throw OSError("sysconf failed", errno);
1373-
1374-
for (int i = 3; i < max_fd; i++) {
1375-
if (i == err_wr_pipe_) continue;
1376-
close(i);
1377-
}
1378-
#endif
1379-
}
1380-
13811323
// Replace the current image with the executable
13821324
sys_ret = execvp(parent_->exe_name_.c_str(), parent_->cargv_.data());
13831325

0 commit comments

Comments
 (0)