Skip to content

Commit aa9b3d5

Browse files
committed
fix: race condition in screen state monitoring
1 parent 2760650 commit aa9b3d5

2 files changed

Lines changed: 25 additions & 31 deletions

File tree

package/contents/ui/RunCommand.qml

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ P5Support.DataSource {
99
property var callbacks: ({})
1010

1111
function exec(cmd, callback) {
12-
if (callback) {
12+
// ensure each command is unique to avoid race condition where the
13+
// same command runs twice but the first run fails deleting the callback
14+
// that was going to be used by the second one
15+
cmd = cmd + ";t=$(date +%N)";
16+
// console.log("running", cmd, "callback", callback);
17+
if (callback && typeof callback === "function") {
1318
callbacks[cmd] = callback;
1419
}
1520
dataSource.connectSource(cmd);
@@ -22,14 +27,17 @@ P5Support.DataSource {
2227
signal exited(string cmd, int exitCode, int exitStatus, string stdout, string stderr)
2328

2429
onExited: (cmd, exitCode, exitStatus, stdout, stderr) => {
25-
if (exitCode === 0 && cmd in callbacks) {
26-
callbacks[cmd]({
27-
cmd,
28-
exitCode,
29-
exitStatus,
30-
stdout,
31-
stderr
32-
});
30+
if (cmd in callbacks) {
31+
if (typeof callbacks[cmd] === "function") {
32+
callbacks[cmd]({
33+
cmd,
34+
exitCode,
35+
exitStatus,
36+
stdout,
37+
stderr
38+
});
39+
}
40+
delete callbacks[cmd];
3341
}
3442
}
3543

package/contents/ui/ScreenModel.qml

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818

1919
import QtQuick
20-
import org.kde.plasma.plasma5support as P5Support
2120

2221
Item {
2322
id: root
@@ -30,25 +29,6 @@ Item {
3029

3130
RunCommand {
3231
id: runCommand
33-
onExited: (cmd, exitCode, exitStatus, stdout, stderr) => {
34-
if (cmd === root.screenStateCmd)
35-
root.screenStateCmdRunning = false;
36-
if (exitCode !== 0)
37-
return;
38-
if (cmd === root.screenStateCmd) {
39-
if (stdout.length > 0) {
40-
stdout = stdout.trim().toLowerCase();
41-
root.screenIsOff = stdout === "0" || stdout.includes("off");
42-
}
43-
}
44-
}
45-
}
46-
47-
function dumpProps(obj) {
48-
console.error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
49-
for (var k of Object.keys(obj)) {
50-
print(k + "=" + obj[k] + "\n");
51-
}
5232
}
5333

5434
DBusSignalMonitor {
@@ -67,11 +47,17 @@ Item {
6747
id: screenTimer
6848
running: root.checkScreenState
6949
repeat: true
70-
interval: 200
50+
interval: 1000
7151
onTriggered: {
7252
if (root.checkScreenState && !root.screenStateCmdRunning) {
7353
root.screenStateCmdRunning = true;
74-
runCommand.exec(root.screenStateCmd);
54+
runCommand.exec(root.screenStateCmd, output => {
55+
root.screenStateCmdRunning = false;
56+
if (output.exitCode === 0 && output.stdout.length > 0) {
57+
const out = output.stdout.trim().toLowerCase();
58+
root.screenIsOff = out === "0" || out === "off";
59+
}
60+
});
7561
}
7662
}
7763
}

0 commit comments

Comments
 (0)