Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
feat: Kill tor, monero-wallet-rpc and cli before allowing GUI to quit
Browse files Browse the repository at this point in the history
  • Loading branch information
binarybaron committed Jun 12, 2024
1 parent 2edc0b0 commit 407f2bb
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 21 deletions.
42 changes: 26 additions & 16 deletions src/main/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import pidtree from 'pidtree';
import util from 'util';
import { getPlatform, isTestnet } from 'store/config';
import { CliLog, isCliLog } from 'models/cliModel';
import {
getLogsAndStringsFromRawFileString,
getLogsFromRawFileString,
} from 'utils/parseUtils';
import { getLogsAndStringsFromRawFileString } from 'utils/parseUtils';
import { store } from 'main/store/mainStore';
import { swapProcessExited } from 'store/features/swapSlice';
import { RpcProcessStateType } from 'models/rpcModel';
Expand All @@ -36,6 +33,10 @@ const BITCOIN_BALANCE_FORCE_REFRESH_INTERVAL = 1000 * 60;
const queue = new PQueue({ concurrency: 1 });
let cli: ChildProcessWithoutNullStreams | null = null;

export function isCliRunning() {
return cli !== null;
}

async function attemptKillMoneroWalletRpcProcess() {
if (process.env.SKIP_MONERO_WALLET_RPC_KILL === 'true') {
logger.debug('Skipping monero-wallet-rpc kill');
Expand Down Expand Up @@ -83,20 +84,29 @@ async function getSpawnArgs(
export async function stopCli() {
const rootPid = cli?.pid;
if (rootPid) {
const childrenPids = await pidtree(rootPid);
childrenPids.forEach((childPid) => {
try {
process.kill(childPid);
} catch (err) {
logger.error(
{ pid: childPid, err },
`Failed to kill children cli process`,
);
}
});
try {
const childrenPids = await pidtree(rootPid);
childrenPids.forEach((childPid) => {
try {
process.kill(childPid);
logger.info({ pid: childPid }, `Force killed child cli process`);
} catch (err) {
logger.error(
{ pid: childPid, err },
`Failed to kill children cli process`,
);
}
});
} catch (err) {
logger.error(
{ pid: rootPid, err },
`Failed to get children cli processes`,
);
}

try {
process.kill(rootPid);
logger.info({ rootPid, childrenPids }, `Force killed cli`);
logger.info({ rootPid }, `Force killed cli`);
} catch (err) {
logger.error({ pid: rootPid, err }, `Failed to kill root cli process`);
}
Expand Down
35 changes: 30 additions & 5 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import {
isDevelopment,
} from 'store/config';
import { resolveHtmlPath } from './util';
import { startRPC, stopCli } from './cli/cli';
import { isCliRunning, startRPC, stopCli } from './cli/cli';
import getSavedLogsOfSwapId, { getAssetPath, fixAppDataPath } from './cli/dirs';
import initSocket from './socket';
import logger from '../utils/logger';
import { spawnTor, stopTor } from './tor';
import { isTorRunning, spawnTor, stopTor } from './tor';
import {
buyXmr,
cancelRefundSwap,
Expand Down Expand Up @@ -105,9 +105,34 @@ async function createWindow() {

fixAppDataPath();

app.on('will-quit', async () => {
await stopCli();
stopTor();
app.on('before-quit', async (e) => {
if (isCliRunning() || isTorRunning()) {
logger.debug(
'Preventing Electron from quitting, stopping CLI, Tor and their child processes first',
);
e.preventDefault();
} else {
logger.info('Letting Electron quit, there are no running child processes');
return;
}

if (isCliRunning()) {
try {
await stopCli();
} catch (err) {
logger.warn('Failed to stop CLI', err);
}
}

if (isTorRunning()) {
try {
stopTor();
} catch (err) {
logger.warn('Failed to stop Tor', err);
}
}

app.quit();
});

app.on('window-all-closed', () => {
Expand Down
4 changes: 4 additions & 0 deletions src/main/tor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import { getTorBinary, makeFileExecutable } from './cli/dirs';

let torProc: ChildProcessWithoutNullStreams | null = null;

export function isTorRunning(): boolean {
return torProc != null;
}

export function stopTor() {
torProc?.kill();
torProc = null;
Expand Down

0 comments on commit 407f2bb

Please sign in to comment.