-
Notifications
You must be signed in to change notification settings - Fork 15
feat: add process exit handler #1212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
586a93f
fix: add process exit handler
71efc34
refactor: add int tests and refactor for reuse
8467290
refactor: add no double close test
177355e
refactor: fix int test
6950284
refactor: fix lint
fb34061
refactor: add better flush handling
896fb75
refactor: rename options
c84ea01
refactor: add test
ad85170
Update packages/utils/src/lib/performance-observer.ts
BioPhoton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import process from 'node:process'; | ||
|
|
||
| /* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
| const SIGNALS = [ | ||
| ['SIGINT', 130], | ||
| ['SIGTERM', 143], | ||
| ['SIGQUIT', 131], | ||
| ] as const; | ||
| /* eslint-enable @typescript-eslint/no-magic-numbers */ | ||
|
|
||
| export type FatalKind = 'uncaughtException' | 'unhandledRejection'; | ||
| type ExitHandlerOptions = | ||
| | { | ||
| onClose?: () => void; | ||
| onFatal: (err: unknown, kind?: FatalKind) => void; | ||
| } | ||
| | { | ||
| onClose: () => void; | ||
| onFatal?: never; | ||
| }; | ||
|
|
||
| export function installExitHandlers(options: ExitHandlerOptions): void { | ||
| // Fatal errors | ||
| process.on('uncaughtException', err => { | ||
| options.onFatal?.(err, 'uncaughtException'); | ||
| }); | ||
|
|
||
| process.on('unhandledRejection', reason => { | ||
| options.onFatal?.(reason, 'unhandledRejection'); | ||
| }); | ||
|
|
||
| // Graceful shutdown signals | ||
| SIGNALS.forEach(([signal]) => { | ||
| process.on(signal, () => { | ||
| options.onClose?.(); | ||
| }); | ||
| }); | ||
|
|
||
| // Normal exit | ||
| process.on('exit', () => { | ||
| options.onClose?.(); | ||
| }); | ||
|
BioPhoton marked this conversation as resolved.
Outdated
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import process from 'node:process'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { installExitHandlers } from './exit-process.js'; | ||
|
|
||
| describe('exit-process tests', () => { | ||
| const onFatal = vi.fn(); | ||
| const onClose = vi.fn(); | ||
| const processOnSpy = vi.spyOn(process, 'on'); | ||
| const processExitSpy = vi.spyOn(process, 'exit').mockImplementation(vi.fn()); | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| [ | ||
| 'uncaughtException', | ||
| 'unhandledRejection', | ||
| 'SIGINT', | ||
| 'SIGTERM', | ||
| 'SIGQUIT', | ||
| 'exit', | ||
| ].forEach(event => { | ||
| process.removeAllListeners(event); | ||
| }); | ||
| }); | ||
|
|
||
| it('should install event listeners for all expected events', () => { | ||
| expect(() => installExitHandlers({ onFatal, onClose })).not.toThrow(); | ||
|
|
||
| expect(processOnSpy).toHaveBeenCalledWith( | ||
| 'uncaughtException', | ||
| expect.any(Function), | ||
| ); | ||
| expect(processOnSpy).toHaveBeenCalledWith( | ||
| 'unhandledRejection', | ||
| expect.any(Function), | ||
| ); | ||
| expect(processOnSpy).toHaveBeenCalledWith('SIGINT', expect.any(Function)); | ||
| expect(processOnSpy).toHaveBeenCalledWith('SIGTERM', expect.any(Function)); | ||
| expect(processOnSpy).toHaveBeenCalledWith('SIGQUIT', expect.any(Function)); | ||
| expect(processOnSpy).toHaveBeenCalledWith('exit', expect.any(Function)); | ||
| }); | ||
|
|
||
| it('should call onFatal with error and kind for uncaughtException', () => { | ||
| expect(() => installExitHandlers({ onFatal })).not.toThrow(); | ||
|
|
||
| const testError = new Error('Test uncaught exception'); | ||
|
|
||
| (process as any).emit('uncaughtException', testError); | ||
|
|
||
| expect(onFatal).toHaveBeenCalledWith(testError, 'uncaughtException'); | ||
| expect(onFatal).toHaveBeenCalledTimes(1); | ||
| expect(onClose).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should call onFatal with reason and kind for unhandledRejection', () => { | ||
| expect(() => installExitHandlers({ onFatal })).not.toThrow(); | ||
|
|
||
| const testReason = 'Test unhandled rejection'; | ||
|
|
||
| (process as any).emit('unhandledRejection', testReason); | ||
|
|
||
| expect(onFatal).toHaveBeenCalledWith(testReason, 'unhandledRejection'); | ||
| expect(onFatal).toHaveBeenCalledTimes(1); | ||
| expect(onClose).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should call onClose and exit with code 0 for SIGINT', () => { | ||
| expect(() => installExitHandlers({ onClose })).not.toThrow(); | ||
|
|
||
| (process as any).emit('SIGINT'); | ||
|
|
||
| expect(onClose).toHaveBeenCalledTimes(1); | ||
| expect(onClose).toHaveBeenCalledWith(); | ||
| expect(onFatal).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should call onClose and exit with code 0 for SIGTERM', () => { | ||
| expect(() => installExitHandlers({ onClose })).not.toThrow(); | ||
|
|
||
| (process as any).emit('SIGTERM'); | ||
|
|
||
| expect(onClose).toHaveBeenCalledTimes(1); | ||
| expect(onClose).toHaveBeenCalledWith(); | ||
| expect(onFatal).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should call onClose and exit with code 0 for SIGQUIT', () => { | ||
| expect(() => installExitHandlers({ onClose })).not.toThrow(); | ||
|
|
||
| (process as any).emit('SIGQUIT'); | ||
|
|
||
| expect(onClose).toHaveBeenCalledTimes(1); | ||
| expect(onClose).toHaveBeenCalledWith(); | ||
| expect(onFatal).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should call onClose for normal exit', () => { | ||
| expect(() => installExitHandlers({ onClose })).not.toThrow(); | ||
|
|
||
| (process as any).emit('exit'); | ||
|
|
||
| expect(onClose).toHaveBeenCalledTimes(1); | ||
| expect(onClose).toHaveBeenCalledWith(); | ||
| expect(onFatal).not.toHaveBeenCalled(); | ||
| expect(processExitSpy).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.