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

Handle SIGTERM in client #2921

Merged
merged 4 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions packages/client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ docker build . -f Dockerfile.fromSource --tag ethereumjs:latest

You may now do appropriate directory/file mounts for `data` dir and `jwtsecret` file and provide their path appropriately in the `client` run command.

Also, in your `docker run` command, be sure to include the `--init` flag so that the container will properly handle kernel signals (like SIGINT and SIGTERM). Not doing so can lead to unexpected behaviour [(as documented here)](https://github.com/nodejs/docker-node/blob/main/docs/BestPractices.md#handling-kernel-signals)

## General Usage

You can get the client up and running by going to the shell and run:
Expand Down
43 changes: 28 additions & 15 deletions packages/client/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,30 @@ function generateAccount(): Account {
return [address, privKey]
}

/**
* Shuts down an actively running client gracefully
* @param config Client config object
* @param clientStartPromise promise that returns a client and server object
*/
const stopClient = async (config: Config, clientStartPromise: any) => {
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 ...')
}

process.exit()
}

/**
* Main entry point to start a client
*/
Expand Down Expand Up @@ -821,22 +845,11 @@ async function run() {
})

process.on('SIGINT', async () => {
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 stopClient(config, clientStartPromise)
})

process.exit()
process.on('SIGTERM', async () => {
await stopClient(config, clientStartPromise)
})
}

Expand Down
16 changes: 8 additions & 8 deletions packages/client/test/cli/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('[CLI]', () => {
const client = Client.http({ port: 8545 })
const res = await client.request('web3_clientVersion', [], 2.0)
assert.ok(res.result.includes('EthereumJS'), 'read from HTTP RPC')
child.kill()
child.kill(9)
resolve(undefined)
}
if (message.toLowerCase().includes('error')) {
Expand All @@ -36,7 +36,7 @@ describe('[CLI]', () => {
}
})
})
}, 10000)
}, 60000)

it('should start WS RPC and return valid responses', async () => {
const file = require.resolve('../../dist/bin/cli.js')
Expand All @@ -52,11 +52,11 @@ describe('[CLI]', () => {
;(client as any).ws.on('open', async function () {
const res = await client.request('web3_clientVersion', [], 2.0)
assert.ok(res.result.includes('EthereumJS'), 'read from WS RPC')
child.kill()
child.kill(9)
resolve(undefined)
})
if (message.toLowerCase().includes('error')) {
child.kill()
child.kill(9)
assert.fail(`client encountered error: ${message}`)
}
}
Expand All @@ -73,7 +73,7 @@ describe('[CLI]', () => {
}
})
})
}, 10000)
}, 60000)

it('HTTP/WS RPCs should not start when cli args omitted', async () => {
const file = require.resolve('../../dist/bin/cli.js')
Expand All @@ -82,11 +82,11 @@ describe('[CLI]', () => {
child.stdout.on('data', async (data) => {
const message: string = data.toString()
if (message.includes('address=http://')) {
child.kill()
child.kill(9)
assert.fail('http endpoint should not be enabled')
}
if (message.includes('address=ws://')) {
child.kill()
child.kill(9)
assert.fail('ws endpoint should not be enabled')
}
if (message.includes('Miner: Assembling block')) {
Expand All @@ -109,5 +109,5 @@ describe('[CLI]', () => {
}
})
})
}, 20000)
}, 60000)
})
Loading