QProcess cannot create pipe - Too many open files - but I don't understand why
-
I use this function to check if an host is online:
MyClass.h
class MyClass : public QObject { Q_OBJECT public: explicit MyClass(QObject *parent = nullptr); private: QProcess _processPing; public slots: void ping(); private slots: void processPing_finished(int exitCode, QProcess::ExitStatus exitStatus); };
MyClass.cpp
const int RECONNECTION_INTERVAL = 10000; MyClass::MyClass(QObject *parent) : QObject(parent) connect(&_processPing, &QProcess::finished, this, &MyClass::processPing_finished); } void MyClass::ping() { QStringList args; args << "-c" << "1" << "-W" << "1" << _url.host(); _processPing.start("ping", args); } void MyClass::processPing_finished(int exitCode, QProcess::ExitStatus exitStatus) { Q_UNUSED(exitStatus) _pingResult = exitCode == 0; if (!_pingResult) QTimer::singleShot(RECONNECTION_INTERVAL, this, &MyClass::ping); }
I have 20 instances of this class, but usually only 1-2 runs the process in a given moment. Since:
- I store the
QProcess
object as a class member - I catch the
finished()
signal
I'm pretty sure I don't leave anything left open, do I?
After some time I get this warning in the console:error : QProcessPrivate::createPipe: Cannot create pipe : Too many open files
I don't understand why it happens.
ps
does not show any process running:$ ps aux | grep ping user 21768 0.0 0.0 6476 2316 pts/1 S+ 17:35 0:00 grep --color=auto ping
I have to restart the application to be able again to run the ping process. Am I using
QProcess
in a wrong way?Ubuntu 22.04, Qt 6.4.0. I saw this similar question, but I don't think it's the same scenario.
- I store the
-
@Mark81
You could tryQProcess::close()
in yourfinished()
slot.You could try making
_processPing
aQProcess *
, allocating on heap,deleteLater()
onfinished()
.I would not be using an external program like
ping
to check whether a host is online, but that is a different matter. -
@JonB said in QProcess cannot create pipe - Too many open files - but I don't understand why:
I would not be using an external program like
ping
to check whether a host is online, but that is a different matter.I'm interested! What other ways would you suggest? Until now I found using
ping
is the most reliable one. -
@Mark81
There have been discussions about whether pinging a server is a useful thing to do. Mostly it seems better to do communications when you have something you actually want to communicate, and handle it not there or reconnecting at that time, not pinging to find out whether it is there or to keep connection open.There is also a possible alternative of send keep-alives on the socket instead.
If I were going to do the "ping" approach, I would look to use Qt networking rather than an external ping program. Ping is pretty simple so you should not need an external program (with
QProcess
, checking what ping is outputting, etc.) to do what it does. For instance, http://www.ping127001.com/pingpage/ping.text is apparently the source of ping from 1983, doubtless there are other implementations in C++ if you Google.