@@ -81,16 +81,11 @@ proc startService(nimbus: var Nimbus, service: var NimbusService) =
8181# # Gracefully exits all services
8282proc  monitorServices (nimbus: Nimbus ) = 
8383  for  service in  nimbus.serviceList:
84-     if  service.serviceHandler.running ():
85-       joinThread (service.serviceHandler)
86-       info  " Exited service "  , service =  service.name
84+     joinThread (service.serviceHandler)
85+     info  " Exited service "  , service =  service.name
8786
8887  notice  " Exited all services" 
8988
90- #  ------------------------------------------------------------------------------
91- #  Public
92- #  ------------------------------------------------------------------------------
93- 
9489#  aux function to prepare arguments and options for eth1 and eth2
9590func  addArg (
9691    paramTable: var  NimbusConfigTable , cmdKind: CmdLineKind , key: string , arg: string 
@@ -110,6 +105,22 @@ func addArg(
110105
111106  paramTable[newKey] =  newArg
112107
108+ proc  controlCHandler () {.noconv .} = 
109+   when  defined (windows):
110+     #  workaround for https://github.com/nim-lang/Nim/issues/4057
111+     try :
112+       setupForeignThreadGc ()
113+     except  NimbusServiceError  as  exc:
114+       raiseAssert  exc.msg #  shouldn't happen
115+ 
116+   notice  " \t Ctrl+C pressed. Shutting down services ..." 
117+   shutdownExecution ()
118+   shutdownConsensus ()
119+ 
120+ #  ------------------------------------------------------------------------------
121+ #  Public
122+ #  ------------------------------------------------------------------------------
123+ 
113124#  Setup services
114125proc  setup * (nimbus: var  Nimbus ) = 
115126  let 
@@ -158,6 +169,10 @@ proc run*(nimbus: var Nimbus) =
158169    fatal  " error starting service:"  , msg =  e.msg
159170    quit  QuitFailure 
160171
172+   #  handling Ctrl+C signal
173+   #  note: do not move. Both execution and consensus clients create these handlers.
174+   setControlCHook (controlCHandler)
175+ 
161176  # # wait for shutdown
162177  nimbus.monitorServices ()
163178
@@ -167,22 +182,7 @@ when isMainModule:
167182
168183  setupFileLimits ()
169184
170-   var  nimbus: Nimbus  =  Nimbus .new
171- 
172-   # # Graceful shutdown by handling of Ctrl+C signal
173-   proc  controlCHandler () {.noconv .} = 
174-     when  defined (windows):
175-       #  workaround for https://github.com/nim-lang/Nim/issues/4057
176-       try :
177-         setupForeignThreadGc ()
178-       except  NimbusServiceError  as  exc:
179-         raiseAssert  exc.msg #  shouldn't happen
180- 
181-     notice  " \t Ctrl+C pressed. Shutting down services ..." 
182-     quit  0 
183- 
184-   setControlCHook (controlCHandler)
185- 
185+   var  nimbus =  Nimbus  ()
186186  nimbus.setup ()
187187  nimbus.run ()
188188
0 commit comments