-
Notifications
You must be signed in to change notification settings - Fork 43
refactor: Move process exit handlers from the trackerless-network to the node package #3224
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
base: main
Are you sure you want to change the base?
Changes from all commits
7376dcf
161904a
4172af9
559517e
7042383
f5bee26
548dbc1
f2d88f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| #!/usr/bin/env node | ||
| import { program } from 'commander' | ||
| import pkg from '../package.json' | ||
|
|
||
| import { Logger } from '@streamr/utils' | ||
| import { createBroker } from '../src/broker' | ||
| import { readConfigAndMigrateIfNeeded } from '../src/config/migration' | ||
| import { overrideConfigToEnvVarsIfGiven } from '../src/config/config' | ||
|
|
||
| const logger = new Logger(module) | ||
|
|
||
| program | ||
| .version(pkg.version) | ||
| .name('streamr-node') | ||
|
|
@@ -17,6 +19,28 @@ program | |
| const config = readConfigAndMigrateIfNeeded(configFile) | ||
| overrideConfigToEnvVarsIfGiven(config) | ||
| const broker = await createBroker(config) | ||
|
|
||
| // Set up graceful shutdown handlers | ||
| const shutdown = async (exitCode: number) => { | ||
| await broker.stop() | ||
| process.exit(exitCode) | ||
| } | ||
|
|
||
| const exitEvents = ['SIGINT', 'SIGTERM', 'SIGUSR1', 'SIGUSR2'] | ||
juslesan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| exitEvents.forEach((event) => { | ||
| process.on(event, () => shutdown(0)) | ||
| }) | ||
|
|
||
| process.on('uncaughtException', (err) => { | ||
| logger.fatal('Encountered uncaughtException', { err }) | ||
| shutdown(1) | ||
| }) | ||
|
|
||
| process.on('unhandledRejection', (err) => { | ||
| logger.fatal('Encountered unhandledRejection', { err }) | ||
| shutdown(1) | ||
| }) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Unhandled rejection in error handlers may cause recursionThe |
||
|
|
||
| if (!program.opts().test) { | ||
| await broker.start() | ||
| } else { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Missing guard allows concurrent broker shutdown attempts
The
shutdownfunction lacks a guard against concurrent invocations. If multiple signals arrive rapidly (e.g., quick successive Ctrl+C presses), the function can be triggered multiple times beforeprocess.exit()is called. This leads to concurrentbroker.stop()calls, which can causestopServerand pluginstop()methods to be invoked concurrently. The oldNetworkStack.stop()had astoppedflag guard that prevented this, but the new broker-levelstop()has no such protection.Additional Locations (1)
packages/node/src/broker.ts#L71-L78