Skip to content

Commit e7e635c

Browse files
committed
fix: cleanup error listeners after parse
1 parent c597738 commit e7e635c

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/command.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,18 +381,28 @@ export class MassargCommand<Args extends ArgsObject = ArgsObject>
381381
parent?: MassargCommand<Args>,
382382
): Promise<void> | void {
383383
// Set up process-level error handlers
384-
const handleProcessError = (error: Error) => {
384+
const handleUncaught = (error: Error) => {
385+
cleanup()
385386
this.handleError(error)
386387
}
387-
process.on('uncaughtException', handleProcessError)
388-
process.on('unhandledRejection', (reason) => {
388+
const handleRejection = (reason: unknown) => {
389+
cleanup()
389390
const error = reason instanceof Error ? reason : new Error(String(reason))
390-
handleProcessError(error)
391-
})
391+
this.handleError(error)
392+
}
393+
const cleanup = () => {
394+
process.off('uncaughtException', handleUncaught)
395+
process.off('unhandledRejection', handleRejection)
396+
}
397+
398+
process.on('uncaughtException', handleUncaught)
399+
process.on('unhandledRejection', handleRejection)
392400

393401
try {
394402
this.getArgs(argv, args, parent, true)
403+
cleanup()
395404
} catch (e) {
405+
cleanup()
396406
this.handleError(e)
397407
}
398408
}

0 commit comments

Comments
 (0)