Skip to content

Commit 86591f1

Browse files
committed
io/process: mask the "QProcess destroyed for running process" warn
1 parent f681e20 commit 86591f1

File tree

5 files changed

+88
-3
lines changed

5 files changed

+88
-3
lines changed

src/io/process.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ Process::Process(QObject* parent): QObject(parent) {
2727
);
2828
}
2929

30+
Process::~Process() {
31+
if (this->process != nullptr && this->process->processId() != 0) {
32+
// Deleting after the process finishes hides the process destroyed warning in logs
33+
QObject::connect(this->process, &QProcess::finished, [p = this->process] { delete p; });
34+
35+
this->process->setParent(nullptr);
36+
this->process->kill();
37+
}
38+
}
39+
40+
void Process::onPostReload() {
41+
this->postReload = true;
42+
this->startProcessIfReady();
43+
}
44+
3045
bool Process::isRunning() const { return this->process != nullptr; }
3146

3247
void Process::setRunning(bool running) {
@@ -165,7 +180,9 @@ void Process::setStdinEnabled(bool enabled) {
165180
}
166181

167182
void Process::startProcessIfReady() {
168-
if (this->process != nullptr || !this->targetRunning || this->mCommand.isEmpty()) return;
183+
if (this->process != nullptr || !this->postReload || !this->targetRunning
184+
|| this->mCommand.isEmpty())
185+
return;
169186
this->targetRunning = false;
170187

171188
auto& cmd = this->mCommand.first();

src/io/process.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <qvariant.h>
1212

1313
#include "../core/doc.hpp"
14+
#include "../core/reload.hpp"
1415
#include "datastream.hpp"
1516
#include "processcore.hpp"
1617

@@ -30,7 +31,9 @@
3031
/// }
3132
/// }
3233
/// ```
33-
class Process: public QObject {
34+
class Process
35+
: public QObject
36+
, public PostReloadHook {
3437
Q_OBJECT;
3538
// clang-format off
3639
/// If the process is currently running. Defaults to false.
@@ -136,6 +139,10 @@ class Process: public QObject {
136139

137140
public:
138141
explicit Process(QObject* parent = nullptr);
142+
~Process() override;
143+
Q_DISABLE_COPY_MOVE(Process);
144+
145+
void onPostReload() override;
139146

140147
// MUST be before exec(ctx) or the other will be called with a default constructed obj.
141148
QSDOC_HIDE Q_INVOKABLE void exec(QList<QString> command);
@@ -251,4 +258,5 @@ private slots:
251258
bool targetRunning = false;
252259
bool mStdinEnabled = false;
253260
bool mClearEnvironment = false;
261+
bool postReload = false;
254262
};

src/io/test/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
function (qs_test name)
22
add_executable(${name} ${ARGN})
3-
target_link_libraries(${name} PRIVATE Qt::Quick Qt::Network Qt::Test quickshell-io)
3+
target_link_libraries(${name} PRIVATE Qt::Quick Qt::Network Qt::Test quickshell-io quickshell-core quickshell-window quickshell-ui)
44
add_test(NAME ${name} WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" COMMAND $<TARGET_FILE:${name}>)
55
endfunction()
66

77
qs_test(datastream datastream.cpp ../datastream.cpp)
8+
qs_test(process process.cpp ../process.cpp ../datastream.cpp ../processcore.cpp)

src/io/test/process.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "process.hpp"
2+
3+
#include <qlist.h>
4+
#include <qsignalspy.h>
5+
#include <qtest.h>
6+
#include <qtestcase.h>
7+
8+
#include "../process.hpp"
9+
10+
void TestProcess::startAfterReload() {
11+
auto process = Process();
12+
auto startedSpy = QSignalSpy(&process, &Process::started);
13+
auto exitedSpy = QSignalSpy(&process, &Process::exited);
14+
15+
process.setCommand({"true"});
16+
process.setRunning(true);
17+
18+
QVERIFY(!process.isRunning());
19+
QCOMPARE(startedSpy.count(), 0);
20+
21+
process.onPostReload();
22+
23+
QVERIFY(process.isRunning());
24+
QVERIFY(startedSpy.wait(100));
25+
}
26+
27+
void TestProcess::testExec() {
28+
auto process = Process();
29+
auto startedSpy = QSignalSpy(&process, &Process::started);
30+
auto exitedSpy = QSignalSpy(&process, &Process::exited);
31+
32+
process.onPostReload();
33+
34+
process.setCommand({"sleep", "30"});
35+
process.setRunning(true);
36+
37+
QVERIFY(process.isRunning());
38+
QVERIFY(startedSpy.wait(100));
39+
40+
process.exec({"true"});
41+
42+
QVERIFY(exitedSpy.wait(100));
43+
QVERIFY(startedSpy.wait(100));
44+
QVERIFY(process.isRunning());
45+
}
46+
47+
QTEST_MAIN(TestProcess);

src/io/test/process.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#include <qobject.h>
4+
#include <qtmetamacros.h>
5+
6+
class TestProcess: public QObject {
7+
Q_OBJECT;
8+
9+
private slots:
10+
static void startAfterReload();
11+
static void testExec();
12+
};

0 commit comments

Comments
 (0)