Skip to content

Commit

Permalink
Merge pull request #710 from WesCossick/fix-sigint-listeners
Browse files Browse the repository at this point in the history
Fix issues with adding/removing SIGINT listeners
  • Loading branch information
cenk1cenk2 committed Mar 29, 2024
2 parents cc812f0 + 0d30480 commit 5f4bd66
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/listr2/src/listr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class Listr<
public rendererClass: ListrRendererFactory
public rendererClassOptions: ListrGetRendererOptions<ListrGetRendererClassFromValue<Renderer> | ListrGetRendererClassFromValue<FallbackRenderer>>
public rendererSelection: ListrRendererSelection
public boundSignalHandler: () => void

private concurrency: Concurrency
private renderer: ListrRenderer
Expand Down Expand Up @@ -100,7 +101,8 @@ export class Listr<
// Graceful interrupt for render cleanup
/* istanbul ignore if */
if (this.options.registerSignalListeners) {
process.once('SIGINT', this.signalHandler.bind(this)).setMaxListeners(0)
this.boundSignalHandler = this.signalHandler.bind(this)
process.once('SIGINT', this.boundSignalHandler).setMaxListeners(0)
}

/* istanbul ignore if */
Expand Down Expand Up @@ -139,12 +141,12 @@ export class Listr<

this.renderer.end()

process.removeAllListeners('SIGINT')
process.removeListener('SIGINT', this.boundSignalHandler)
} catch (err: any) {
if (this.options.exitOnError !== false) {
this.renderer.end(err)

process.removeAllListeners('SIGINT')
process.removeListener('SIGINT', this.boundSignalHandler)

// Do not exit when explicitly set to `false`
throw err
Expand Down
21 changes: 21 additions & 0 deletions packages/listr2/tests/signal-handler.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,25 @@ describe('signal handlers', () => {

expectProcessOutputToMatchSnapshot(output, 'ACxcRBILlHsqiSxYArjASFdofVqLaUyE')
})

it('should not clear other SIGINT listeners', async () => {
process.addListener('SIGINT', function doNotRemove () {})
expect(process.listeners('SIGINT').map((listener) => listener.name)).toContain('doNotRemove')

await new Listr(
[
{
title: 'a task',
task: async (): Promise<void> => {}
}
],
{
concurrent: false,
renderer: 'test',
registerSignalListeners: true
}
).run()

expect(process.listeners('SIGINT').map((listener) => listener.name)).toContain('doNotRemove')
})
})

0 comments on commit 5f4bd66

Please sign in to comment.