Skip to content

Commit

Permalink
prevent dirty exit
Browse files Browse the repository at this point in the history
  • Loading branch information
nedsalk committed Jul 10, 2024
1 parent 3e1b704 commit fe648f9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
14 changes: 13 additions & 1 deletion packages/account/src/test-utils/launchNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,21 @@ describe('launchNode', () => {
expect(mkdirSyncSpy).toHaveBeenCalledTimes(1);
const tempDirPath = mkdirSyncSpy.mock.calls[0][0];

childProcessMod.execSync(`kill -- -${pid}`);
childProcessMod.execSync(`kill ${pid}`);
// wait until cleanup finishes (done via events)
await sleep(1500);
expect(fsMod.existsSync(tempDirPath)).toBeFalsy();
});

test('calling cleanup on externally killed node does not throw', async () => {
const mkdirSyncSpy = vi.spyOn(fsMod, 'mkdirSync');

const { pid, cleanup } = await launchNode({ loggingEnabled: false });
expect(mkdirSyncSpy).toHaveBeenCalledTimes(1);

childProcessMod.execSync(`kill -- -${pid}`);
// wait until cleanup finishes (done via events)
await sleep(1500);
cleanup();
});
});
13 changes: 11 additions & 2 deletions packages/account/src/test-utils/launchNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,19 @@ export const launchNode = async ({
}
childState.isDead = true;

removeSideffects();
if (child.pid !== undefined) {
process.kill(-child.pid);
try {
process.kill(-child.pid);
} catch (e) {
const error = e as Error & { code: string };
if (error.code === 'ESRCH') {
//
} else {
throw e;
}
}
} else {
removeSideffects();
// eslint-disable-next-line no-console
console.error('No PID available for the child process, unable to kill launched node');
}
Expand Down

0 comments on commit fe648f9

Please sign in to comment.