Skip to content

Commit

Permalink
graceful exit on SIGTERM (#2178)
Browse files Browse the repository at this point in the history
Much easier than convincing all users to change the default signal in
their service definition file to SIGINT.
  • Loading branch information
stefantalpalaru committed Dec 14, 2020
1 parent 14c5d6d commit 9daf6be
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions beacon_chain/nimbus_beacon_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import
# Standard library
std/[os, tables, strutils, strformat, sequtils, times, math,
terminal, osproc, random],
system/ansi_c,

# Nimble packages
stew/[objects, byteutils, endians2, io2], stew/shims/macros,
Expand Down Expand Up @@ -829,6 +830,12 @@ proc run*(node: BeaconNode) =
notice "Shutting down after having received SIGINT"
bnStatus = BeaconNodeStatus.Stopping
setControlCHook(controlCHandler)
# equivalent SIGTERM handler
when defined(posix):
proc SIGTERMHandler(signal: cint) {.noconv.} =
notice "Shutting down after having received SIGTERM"
bnStatus = BeaconNodeStatus.Stopping
c_signal(SIGTERM, SIGTERMHandler)

# main event loop
while bnStatus == BeaconNodeStatus.Running:
Expand Down Expand Up @@ -1212,6 +1219,12 @@ programMain:
notice "Shutting down after having received SIGINT"
quit 0
setControlCHook(exitImmediatelyOnCtrlC)
# equivalent SIGTERM handler
when defined(posix):
proc exitImmediatelyOnSIGTERM(signal: cint) {.noconv.} =
notice "Shutting down after having received SIGTERM"
quit 0
c_signal(SIGTERM, exitImmediatelyOnSIGTERM)

if config.eth2Network.isSome:
let metadata = getMetadataForNetwork(config.eth2Network.get)
Expand Down

0 comments on commit 9daf6be

Please sign in to comment.