graceful shutdown: move the hook registration (#1940)

...closer to where it starts having an effect.
This commit is contained in:
Ștefan Talpalaru 2020-11-02 19:02:27 +01:00 committed by GitHub
parent 2f049dc6e7
commit 002a3dfa54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 9 deletions

View File

@ -760,6 +760,15 @@ proc run*(node: BeaconNode) =
node.requestManager.start() node.requestManager.start()
node.startSyncManager() node.startSyncManager()
## Ctrl+C handling
proc controlCHandler() {.noconv.} =
when defined(windows):
# workaround for https://github.com/nim-lang/Nim/issues/4057
setupForeignThreadGc()
notice "Shutting down after having received SIGINT"
bnStatus = BeaconNodeStatus.Stopping
setControlCHook(controlCHandler)
# main event loop # main event loop
while bnStatus == BeaconNodeStatus.Running: while bnStatus == BeaconNodeStatus.Running:
try: try:
@ -1103,15 +1112,6 @@ programMain:
config.createDumpDirs() config.createDumpDirs()
## Ctrl+C handling
proc controlCHandler() {.noconv.} =
when defined(windows):
# workaround for https://github.com/nim-lang/Nim/issues/4057
setupForeignThreadGc()
notice "Shutting down after having received SIGINT"
bnStatus = BeaconNodeStatus.Stopping
setControlCHook(controlCHandler)
when useInsecureFeatures: when useInsecureFeatures:
if config.metricsEnabled: if config.metricsEnabled:
let metricsAddress = config.metricsAddress let metricsAddress = config.metricsAddress
@ -1119,7 +1119,11 @@ programMain:
address = metricsAddress, port = config.metricsPort address = metricsAddress, port = config.metricsPort
metrics.startHttpServer($metricsAddress, config.metricsPort) metrics.startHttpServer($metricsAddress, config.metricsPort)
# There are no managed event loops in here, to do a graceful shutdown, but
# letting the default Ctrl+C handler exit is safe, since we only read from
# the db.
var node = waitFor BeaconNode.init(rng, config, genesisStateContents) var node = waitFor BeaconNode.init(rng, config, genesisStateContents)
if bnStatus == BeaconNodeStatus.Stopping: if bnStatus == BeaconNodeStatus.Stopping:
return return
# The memory for the initial snapshot won't be needed anymore # The memory for the initial snapshot won't be needed anymore