问题
It seems like boost::process::system is leaking fds:
Let's say I have this simple code to flush iptables config every 3 seconds (just an example):
#include <boost/process.hpp>
#include <thread>
int main(void)
{
while(true)
{
std::this_thread::sleep_for(std::chrono::seconds(3));
boost::process::system(boost::process::search_path("iptables"), "-F");
}
return 0;
}
If I observe the count of open file descriptors by listing /proc/PID/fd |wc -l
, I can see that the count increases by one every 3 seconds. Eventually, when it reaches 1024, the program will abort, because the system
call will throw an exception with what()
stating that there are too many open files!
How can I avoid this fd leakage? I'm using boost 1.69.
EDIT:
Replacing boost::process::system
with boost::process::child
does not seem to help, the child
seems to also leak fds, no matter if it gets detached or not.
EDIT 2:
Valgrind log with --track-fds=yes
:
https://termbin.com/d6ud
回答1:
The problem seems to be a bug in the specific version (1.69) of boost, and not in the posted code itself. So upgrading boost/patching the bug solves this problem.
The bug report can be found from here: https://github.com/boostorg/process/issues/62
来源:https://stackoverflow.com/questions/57752670/boostprocess-system-leaking-file-descriptors