Skip to content

Commit

Permalink
Implement graceful shutdown.
Browse files Browse the repository at this point in the history
  • Loading branch information
web3-developer committed Sep 20, 2024
1 parent a79b091 commit 3d336cf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
36 changes: 31 additions & 5 deletions fluffy/fluffy.nim
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func optionToOpt[T](o: Option[T]): Opt[T] =
else:
Opt.none(T)

proc run(config: PortalConf) {.raises: [CatchableError].} =
proc run(config: PortalConf): PortalNode {.raises: [CatchableError].} =
setupLogging(config.logLevel, config.logStdout, none(OutFile))

notice "Launching Fluffy", version = fullVersionStr, cmdParams = commandLineParams()
Expand Down Expand Up @@ -252,7 +252,7 @@ proc run(config: PortalConf) {.raises: [CatchableError].} =

setupRpcServer(rpcWsServer)

runForever()
return node

when isMainModule:
{.pop.}
Expand All @@ -262,6 +262,32 @@ when isMainModule:
)
{.push raises: [].}

case config.cmd
of PortalCmd.noCommand:
run(config)
let node =
case config.cmd
of PortalCmd.noCommand:
run(config)

# Ctrl+C handling
proc controlCHandler() {.noconv.} =
when defined(windows):
# workaround for https://github.com/nim-lang/Nim/issues/4057
try:
setupForeignThreadGc()
except Exception as exc:
raiseAssert exc.msg # shouldn't happen

notice "Shutting down after having received SIGINT"
node.state = PortalNodeState.Stopping

try:
setControlCHook(controlCHandler)
except Exception as exc: # TODO Exception
warn "Cannot set ctrl-c handler", msg = exc.msg

while node.state == PortalNodeState.Running:
try:
poll()
except CatchableError as e:
warn "Exception in poll()", exc = e.name, err = e.msg

waitFor node.stop()
6 changes: 3 additions & 3 deletions fluffy/network/beacon/beacon_light_client_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type
GetBoolCallback* = proc(): bool {.gcsafe, raises: [].}
GetSlotCallback* = proc(): Slot {.gcsafe, raises: [].}

LightClientManager* = object
LightClientManager* = ref object
network: BeaconNetwork
rng: ref HmacDrbgContext
getTrustedBlockRoot: GetTrustedBlockRootCallback
Expand Down Expand Up @@ -315,12 +315,12 @@ proc loop(self: LightClientManager) {.async: (raises: [CancelledError]).} =
didLatestSyncTaskProgress = didProgress,
)

proc start*(self: var LightClientManager) =
proc start*(self: LightClientManager) =
## Start light client manager's loop.
doAssert self.loopFuture == nil
self.loopFuture = self.loop()

proc stop*(self: var LightClientManager) {.async: (raises: []).} =
proc stop*(self: LightClientManager) {.async: (raises: []).} =
## Stop light client manager's loop.
if not self.loopFuture.isNil():
await noCancel(self.loopFuture.cancelAndWait())
Expand Down
10 changes: 9 additions & 1 deletion fluffy/portal_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export
beacon_light_client, history_network, state_network, portal_protocol_config, forks

type
PortalNodeState* = enum
Starting
Running
Stopping

PortalNodeConfig* = object
accumulatorFile*: Opt[string]
disableStateRootValidation*: bool
Expand All @@ -33,6 +38,7 @@ type
storageCapacity*: uint64

PortalNode* = ref object
state*: PortalNodeState
discovery: protocol.Protocol
contentDB: ContentDB
streamManager: StreamManager
Expand Down Expand Up @@ -214,6 +220,8 @@ proc start*(n: PortalNode) =

n.statusLogLoop = statusLogLoop(n)

n.state = PortalNodeState.Running

proc stop*(n: PortalNode) {.async: (raises: []).} =
debug "Stopping Portal node"

Expand All @@ -233,8 +241,8 @@ proc stop*(n: PortalNode) {.async: (raises: []).} =
futures.add(n.statusLogLoop.cancelAndWait())

futures.add(n.discovery.closeWait())
n.contentDB.close()

await noCancel(allFutures(futures))

n.contentDB.close()
n.statusLogLoop = nil

0 comments on commit 3d336cf

Please sign in to comment.