Skip to content

Commit e9f2a82

Browse files
committed
fix: Properly close adb and rclone on exit
Fixes #34 Cleanly closes `adb` and `rclone` connections when the application exits, ensuring resources are freed and preventing potential issues. This addresses a previous oversight where these connections weren't properly closed, potentially leading to resource leaks and unexpected behavior. Even though we free up the adb connection we don't close the server because other programs may be using the connection. The changes introduce a `destroy()` function in `tools.js` that handles the cleanup. This function is called when the application receives the "will-quit" event, ensuring that resources are freed before the application terminates. This change enhances the application's reliability and stability by preventing resource leaks and ensuring a clean exit.
1 parent aa85722 commit e9f2a82

2 files changed

Lines changed: 37 additions & 15 deletions

File tree

main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,11 @@ async function startApp() {
538538
app.quit();
539539
}
540540
});
541+
542+
app.on("will-quit", async () => {
543+
console.log("will-quit");
544+
await tools.destroy();
545+
});
541546
}
542547

543548
startApp();

tools.js

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const l = 32;
3030
const configLocationOld = path.join(global.homedir, "sidenoder-config.json");
3131
const configLocation = path.join(global.sidenoderHome, "config.json");
3232

33-
let agentOculus, agentSteam, agentSQ;
33+
let agentOculus, agentSteam, agentSQ, tracker;
3434

3535
init();
3636

@@ -63,6 +63,7 @@ module.exports = {
6363
trackDevices,
6464
checkDeps,
6565
checkMount,
66+
destroy,
6667
mount,
6768
killRClone,
6869
getDir,
@@ -908,7 +909,7 @@ async function trackDevices() {
908909
await getDeviceSync();
909910

910911
try {
911-
const tracker = await adb.trackDevices();
912+
tracker = await adb.trackDevices();
912913
tracker.on("add", async (device) => {
913914
console.log("Device was plugged in", device.id);
914915
// await getDeviceSync();
@@ -926,15 +927,25 @@ async function trackDevices() {
926927
});
927928

928929
tracker.on("end", () => {
929-
console.error("Tracking stopped");
930-
trackDevices();
930+
console.log("Tracking stopped");
931931
});
932932
} catch (err) {
933933
console.error("Something went wrong:", err.stack);
934934
returnError(err);
935935
}
936936
}
937937

938+
async function destroy() {
939+
try {
940+
await killRClone();
941+
} catch (_err) {
942+
console.log("rclone not started");
943+
}
944+
945+
tracker.end();
946+
tracker = null;
947+
}
948+
938949
async function appInfo(args) {
939950
const { res, pkg } = args;
940951
const app = KMETAS[pkg];
@@ -1495,7 +1506,7 @@ async function killRClone() {
14951506
platform == "win"
14961507
? `taskkill.exe /F /T /IM rclone.exe`
14971508
: `killall -9 rclone`;
1498-
console.log("try kill rclone");
1509+
console.log("killing rclone");
14991510
return new Promise((res, rej) => {
15001511
exec(killCmd, (error, stdout, stderr) => {
15011512
if (error) {
@@ -1624,17 +1635,23 @@ async function mount() {
16241635
exec(
16251636
`"${rcloneCmd}" ${mountCmd} --read-only --rc --rc-no-auth --config="${global.currentConfiguration.rcloneConf}" ${global.currentConfiguration.cfgSection}: "${global.mountFolder}"`,
16261637
(error, stdout, stderr) => {
1627-
if (error) {
1628-
console.error("rclone error:", error);
1629-
if (RCLONE_ID != myId) error = false;
1630-
console.log({ RCLONE_ID, myId });
1631-
win.webContents.send("check_mount", { success: false, error });
1632-
// checkMount();
1633-
/*if (error.message.search('transport endpoint is not connected')) {
1634-
console.log('GEVONDE');
1635-
}*/
1638+
try {
1639+
// We need to use a try/catch here because the callback may have been
1640+
// called after rclone has been closed.
1641+
if (error) {
1642+
if (!tracker) {
1643+
// Window is closing
1644+
return;
1645+
}
1646+
console.error("rclone error:", error);
1647+
if (RCLONE_ID != myId) error = false;
1648+
console.log({ RCLONE_ID, myId });
1649+
win.webContents.send("check_mount", { success: false, error });
16361650

1637-
return;
1651+
return;
1652+
}
1653+
} catch (e) {
1654+
// Do nothing
16381655
}
16391656

16401657
if (stderr) {

0 commit comments

Comments
 (0)