Skip to content

Commit

Permalink
fix: ensure that we wait for processes to shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Feb 25, 2023
1 parent c264c7b commit d24990b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/createSpawn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,33 @@ it('rejects if process produces an error', async () => {

await expect(spawn`does-not-exist`).rejects.toThrowError('Program exited with code 127.');
});

const TIMEOUT = 100;

it('terminates spawned process when it receives abort signal', async () => {
const abortController = new AbortController();

const spawn = createSpawn('foo', abortController.signal);

setTimeout(() => {
void abortController.abort();
}, 50);

await spawn`sleep 10`;
}, TIMEOUT);

it('waits for termination', async () => {
const abortController = new AbortController();

const spawn = createSpawn('foo', abortController.signal);

setTimeout(() => {
void abortController.abort();
}, 50);

const start = Date.now();

await spawn`( trap '' TERM; exec sleep 0.1 )`;

expect(Date.now() - start).toBeGreaterThan(100);
}, TIMEOUT * 2);
4 changes: 4 additions & 0 deletions src/createSpawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export const createSpawn = (taskId: string, triggerSignal: AbortSignal | null) =
return result;
}

if (triggerSignal?.aborted) {
return result;
}

log.error('task %s exited with an error', taskId);

throw new Error('Program exited with code ' + result.exitCode + '.');
Expand Down

0 comments on commit d24990b

Please sign in to comment.