diff --git a/packages/account/src/test-utils/launchNode.test.ts b/packages/account/src/test-utils/launchNode.test.ts index c18ac7dd5c..8353078094 100644 --- a/packages/account/src/test-utils/launchNode.test.ts +++ b/packages/account/src/test-utils/launchNode.test.ts @@ -212,4 +212,21 @@ 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 logSpy = vi.spyOn(console, 'log'); + + 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(); + + expect(logSpy).toHaveBeenCalledWith( + `fuel-core node under pid ${pid} does not exist. The node might have been killed before cleanup was called. Exiting cleanly.` + ); + }); }); diff --git a/packages/account/src/test-utils/launchNode.ts b/packages/account/src/test-utils/launchNode.ts index f39de0da7c..0554acca8f 100644 --- a/packages/account/src/test-utils/launchNode.ts +++ b/packages/account/src/test-utils/launchNode.ts @@ -234,10 +234,22 @@ 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') { + // eslint-disable-next-line no-console + console.log( + `fuel-core node under pid ${child.pid} does not exist. The node might have been killed before cleanup was called. Exiting cleanly.` + ); + } 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'); }