don't crash on shutdown (#323)

* don't crash on shutdown

* cleanup
This commit is contained in:
Dmitriy Ryajov 2022-12-05 09:00:02 -06:00 committed by GitHub
parent 0beeefd760
commit 7907d67797
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 36 additions and 4 deletions

View File

@ -19,6 +19,7 @@ import ./codex/utils/keyutils
export codex, conf, libp2p, chronos, chronicles
when isMainModule:
import std/sequtils
import std/os
import pkg/confutils/defs
import ./codex/utils/fileutils
@ -29,6 +30,12 @@ when isMainModule:
when defined(posix):
import system/ansi_c
type
CodexStatus {.pure.} = enum
Stopped,
Stopping,
Running
let config = CodexConf.load(
version = codexFullVersion
)
@ -59,6 +66,10 @@ when isMainModule:
trace "Repo dir initialized", dir = config.dataDir / "repo"
var
state: CodexStatus
pendingFuts: seq[Future[void]]
let
keyPath =
if isAbsolute(string config.netPrivKeyFile):
@ -77,7 +88,11 @@ when isMainModule:
setupForeignThreadGc()
except Exception as exc: raiseAssert exc.msg # shouldn't happen
notice "Shutting down after having received SIGINT"
waitFor server.stop()
pendingFuts.add(server.stop())
state = CodexStatus.Stopping
notice "Stopping Codex"
try:
setControlCHook(controlCHandler)
@ -88,12 +103,29 @@ when isMainModule:
when defined(posix):
proc SIGTERMHandler(signal: cint) {.noconv.} =
notice "Shutting down after having received SIGTERM"
waitFor server.stop()
notice "Stopped Codex"
pendingFuts.add(server.stop())
state = CodexStatus.Stopping
notice "Stopping Codex"
c_signal(ansi_c.SIGTERM, SIGTERMHandler)
waitFor server.start()
pendingFuts.add(server.start())
state = CodexStatus.Running
while state == CodexStatus.Running:
# poll chronos
chronos.poll()
# wait fot futures to finish
let res = waitFor allFinished(pendingFuts)
state = CodexStatus.Stopped
if res.anyIt( it.failed ):
error "Codex didn't shutdown correctly"
quit QuitFailure
notice "Exited codex"
of StartUpCommand.initNode: