From 9daf6be73c7f881c81276184dfbd739893b78165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C8=98tefan=20Talpalaru?= Date: Mon, 14 Dec 2020 17:45:14 +0100 Subject: [PATCH] graceful exit on SIGTERM (#2178) Much easier than convincing all users to change the default signal in their service definition file to SIGINT. --- beacon_chain/nimbus_beacon_node.nim | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/beacon_chain/nimbus_beacon_node.nim b/beacon_chain/nimbus_beacon_node.nim index 54976293f..f5caadfc4 100644 --- a/beacon_chain/nimbus_beacon_node.nim +++ b/beacon_chain/nimbus_beacon_node.nim @@ -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, @@ -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: @@ -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)