From 419e05b7152447fd5a9b9933b52f6107bbff83c7 Mon Sep 17 00:00:00 2001 From: Mike Ratcliffe Date: Tue, 17 Sep 2024 12:03:10 +0100 Subject: [PATCH] fix: Properly close `adb` and `rclone` on exit 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 or unexpected behavior. Even though we free up the adb connection we don't close the server because other programs may be using the connection. --- main.js | 5 +++++ tools.js | 23 +++++++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/main.js b/main.js index 5643e0b..c37cc04 100644 --- a/main.js +++ b/main.js @@ -545,6 +545,11 @@ async function startApp() { app.quit(); } }); + + app.on("will-quit", async () => { + console.log("will-quit"); + await tools.destroy(); + }); } startApp(); diff --git a/tools.js b/tools.js index 2b1dc29..50cc3db 100644 --- a/tools.js +++ b/tools.js @@ -29,7 +29,7 @@ const l = 32; const configLocationOld = path.join(global.homedir, "sidenoder-config.json"); const configLocation = path.join(global.sidenoderHome, "config.json"); -let agentOculus, agentSteam, agentSQ; +let agentOculus, agentSteam, agentSQ, tracker; init(); @@ -62,6 +62,7 @@ module.exports = { trackDevices, checkDeps, checkMount, + destroy, mount, killRClone, getDir, @@ -952,7 +953,7 @@ async function trackDevices() { await getDeviceSync(); try { - const tracker = await adb.trackDevices(); + tracker = await adb.trackDevices(); tracker.on("add", async (device) => { console.log("Device was plugged in", device.id); // await getDeviceSync(); @@ -971,7 +972,6 @@ async function trackDevices() { tracker.on("end", () => { console.error("Tracking stopped"); - trackDevices(); }); } catch (err) { console.error("Something went wrong:", err.stack); @@ -979,6 +979,17 @@ async function trackDevices() { } } +async function destroy() { + try { + await killRClone(); + } catch (err) { + console.log("rclone not started"); + } + + tracker.end(); + tracker = null; +} + async function appInfo(args) { const { res, pkg } = args; const app = KMETAS[pkg]; @@ -1577,7 +1588,7 @@ async function killRClone() { platform === "win" ? `taskkill.exe /F /T /IM rclone.exe` : `killall -9 rclone`; - console.log("try kill rclone"); + console.log("killing rclone"); return new Promise((res, rej) => { exec(killCmd, (error, stdout, stderr) => { if (error) { @@ -1714,6 +1725,10 @@ async function mount() { `"${rcloneCmd}" ${mountCmd} --read-only --rc --rc-no-auth --config="${global.currentConfiguration.rcloneConf}" ${global.currentConfiguration.cfgSection}: "${global.mountFolder}"`, (error, stdout, stderr) => { if (error) { + if (!tracker) { + // Window is closing + return; + } console.error("rclone error:", error); if (RCLONE_ID !== myId) { error = false;