Skip to content

Commit

Permalink
client: Fix unclean shutdown scenario where SIGINT may come before cl…
Browse files Browse the repository at this point in the history
…ient fully started (#2677)
  • Loading branch information
g11tech authored May 4, 2023
1 parent 9b85ff1 commit e4075b5
Showing 1 changed file with 34 additions and 14 deletions.
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

0 comments on commit e4075b5

Please sign in to comment.