Skip to content

Commit

Permalink
avoid dirty exit when node already killed
Browse files Browse the repository at this point in the history
  • Loading branch information
nedsalk committed Jul 10, 2024
1 parent 3e1b704 commit dc647cb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
12 changes: 12 additions & 0 deletions packages/account/src/test-utils/launchNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,16 @@ describe('launchNode', () => {
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 dc647cb

Please sign in to comment.