Skip to content
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

client: Fix unclean shutdown scenario where SIGINT may come before client fully started #2677

Merged
merged 1 commit into from
May 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions packages/client/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,22 +785,42 @@ async function run() {
config.logger.info(`Reading custom genesis state accounts=${numAccounts}`)
}

const client = await startClient(config, customGenesisState)
const servers =
args.rpc === true || args.rpcEngine === true ? startRPCServers(client, args as RPCArgs) : []
if (
client.config.chainCommon.gteHardfork(Hardfork.Paris) === true &&
(args.rpcEngine === false || args.rpcEngine === undefined)
) {
config.logger.warn(`Engine RPC endpoint not activated on a post-Merge HF setup.`)
}
// Do not wait for client to be fully started so that we can hookup SIGINT handling
// else a SIGINT before may kill the process in unclean manner
const clientStartPromise = startClient(config, customGenesisState)
.then((client) => {
const servers =
args.rpc === true || args.rpcEngine === true ? startRPCServers(client, args as RPCArgs) : []
if (
client.config.chainCommon.gteHardfork(Hardfork.Paris) === true &&
(args.rpcEngine === false || args.rpcEngine === undefined)
) {
config.logger.warn(`Engine RPC endpoint not activated on a post-Merge HF setup.`)
}
config.logger.info('Client started successfully')
return { client, servers }
})
.catch((e) => {
config.logger.error('Error starting client', e)
return null
})

process.on('SIGINT', async () => {
config.logger.info('Caught interrupt signal. Shutting down...')
for (const s of servers) {
s.http().close()
config.logger.info('Caught interrupt signal. Obtaining client handle for clean shutdown...')
config.logger.info('(This might take a little longer if client not yet fully started)')
const clientHandle = await clientStartPromise
if (clientHandle !== null) {
config.logger.info('Shutting down the client and the servers...')
const { client, servers } = clientHandle
for (const s of servers) {
s.http().close()
}
await client.stop()
config.logger.info('Exiting.')
} else {
config.logger.info('Client did not start properly, exiting ...')
}
await client.stop()
config.logger.info('Exiting.')

process.exit()
})
}
Expand Down