Skip to content

Commit 426856e

Browse files
authored
Eliminate build warnings in applications. (#6889)
* Initial commit. * Update copyright years.
1 parent dec1cd3 commit 426856e

5 files changed

+58
-53
lines changed

beacon_chain/beacon_node.nim

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import
1414

1515
# Nimble packages
1616
chronos, presto, bearssl/rand,
17+
metrics, metrics/chronos_httpserver,
1718

1819
# Local modules
1920
"."/[beacon_clock, beacon_chain_db, conf, light_client],
@@ -86,6 +87,7 @@ type
8687
elManager*: ELManager
8788
restServer*: RestServerRef
8889
keymanagerHost*: ref KeymanagerHost
90+
metricsServer*: Opt[MetricsHttpServerRef]
8991
keymanagerServer*: RestServerRef
9092
keystoreCache*: KeystoreCacheRef
9193
eventBus*: EventBus

beacon_chain/nimbus_beacon_node.nim

+8-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{.push raises: [].}
99

1010
import
11-
std/[os, random, terminal, times],
11+
std/[os, random, terminal, times, exitprocs],
1212
chronos, chronicles,
1313
metrics, metrics/chronos_httpserver,
1414
stew/[byteutils, io2],
@@ -2111,6 +2111,8 @@ proc stop(node: BeaconNode) =
21112111
except CatchableError as exc:
21122112
warn "Couldn't stop network", msg = exc.msg
21132113

2114+
waitFor node.metricsServer.stopMetricsServer()
2115+
21142116
node.attachedValidators[].slashingProtection.close()
21152117
node.attachedValidators[].close()
21162118
node.db.close()
@@ -2166,7 +2168,7 @@ var gPidFile: string
21662168
proc createPidFile(filename: string) {.raises: [IOError].} =
21672169
writeFile filename, $os.getCurrentProcessId()
21682170
gPidFile = filename
2169-
addQuitProc proc {.noconv.} = discard io2.removeFile(gPidFile)
2171+
addExitProc proc {.noconv.} = discard io2.removeFile(gPidFile)
21702172

21712173
proc initializeNetworking(node: BeaconNode) {.async.} =
21722174
node.installMessageValidators()
@@ -2378,16 +2380,8 @@ proc doRunBeaconNode(config: var BeaconNodeConf, rng: ref HmacDrbgContext) {.rai
23782380

23792381
config.createDumpDirs()
23802382

2381-
if config.metricsEnabled:
2382-
let metricsAddress = config.metricsAddress
2383-
notice "Starting metrics HTTP server",
2384-
url = "http://" & $metricsAddress & ":" & $config.metricsPort & "/metrics"
2385-
try:
2386-
startMetricsHttpServer($metricsAddress, config.metricsPort)
2387-
except CatchableError as exc:
2388-
raise exc
2389-
except Exception as exc:
2390-
raiseAssert exc.msg # TODO fix metrics
2383+
let metricsServer = (waitFor config.initMetricsServer()).valueOr:
2384+
return
23912385

23922386
# Nim GC metrics (for the main thread) will be collected in onSecond(), but
23932387
# we disable piggy-backing on other metrics here.
@@ -2435,6 +2429,8 @@ proc doRunBeaconNode(config: var BeaconNodeConf, rng: ref HmacDrbgContext) {.rai
24352429

24362430
let node = waitFor BeaconNode.init(rng, config, metadata)
24372431

2432+
node.metricsServer = metricsServer
2433+
24382434
if bnStatus == BeaconNodeStatus.Stopping:
24392435
return
24402436

beacon_chain/nimbus_binary_common.nim

+36-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# beacon_chain
2-
# Copyright (c) 2018-2024 Status Research & Development GmbH
2+
# Copyright (c) 2018-2025 Status Research & Development GmbH
33
# Licensed and distributed under either of
44
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
55
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
@@ -16,7 +16,7 @@ import
1616
# Nimble packages
1717
chronos, confutils, presto, toml_serialization, metrics,
1818
chronicles, chronicles/helpers as chroniclesHelpers, chronicles/topics_registry,
19-
stew/io2,
19+
stew/io2, metrics, metrics/chronos_httpserver,
2020

2121
# Local modules
2222
./spec/[helpers, keystore],
@@ -448,6 +448,40 @@ proc initKeymanagerServer*(
448448

449449
KeymanagerInitResult(server: keymanagerServer, token: token)
450450

451+
proc initMetricsServer*(
452+
config: AnyConf
453+
): Future[Result[Opt[MetricsHttpServerRef], string]] {.
454+
async: (raises: [CancelledError]).} =
455+
if config.metricsEnabled:
456+
let
457+
metricsAddress = config.metricsAddress
458+
metricsPort = config.metricsPort
459+
url = "http://" & $metricsAddress & ":" & $metricsPort & "/metrics"
460+
461+
info "Starting metrics HTTP server", url = url
462+
463+
let server = MetricsHttpServerRef.new($metricsAddress, metricsPort).valueOr:
464+
fatal "Could not start metrics HTTP server",
465+
url = url, reason = error
466+
return err($error)
467+
468+
try:
469+
await server.start()
470+
except MetricsError as exc:
471+
fatal "Could not start metrics HTTP server",
472+
url = url, reason = exc.msg
473+
return err(exc.msg)
474+
475+
ok(Opt.some(server))
476+
else:
477+
ok(Opt.none(MetricsHttpServerRef))
478+
479+
proc stopMetricsServer*(v: Opt[MetricsHttpServerRef]) {.
480+
async: (raises: []).} =
481+
if v.isSome():
482+
info "Shutting down metrics HTTP server"
483+
await v.get().close()
484+
451485
proc quitDoppelganger*() =
452486
# Avoid colliding with
453487
# https://www.freedesktop.org/software/systemd/man/systemd.exec.html#Process%20Exit%20Codes

beacon_chain/nimbus_signing_node.nim

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# nimbus_signing_node
2-
# Copyright (c) 2018-2024 Status Research & Development GmbH
2+
# Copyright (c) 2018-2025 Status Research & Development GmbH
33
# Licensed and distributed under either of
44
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
55
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
@@ -414,7 +414,7 @@ proc asyncInit(sn: SigningNodeRef) {.async: (raises: [SigningNodeError]).} =
414414
raise newException(SigningNodeError, "")
415415
SigningNodeServer(kind: SigningNodeKind.NonSecure, nserver: res.get())
416416

417-
proc asyncRun*(sn: SigningNodeRef) {.async: (raises: []).} =
417+
proc asyncRun*(sn: SigningNodeRef) {.async: (raises: [SigningNodeError]).} =
418418
sn.runKeystoreCachePruningLoopFut =
419419
runKeystoreCachePruningLoop(sn.keystoreCache)
420420
sn.installApiHandlers()
@@ -429,6 +429,11 @@ proc asyncRun*(sn: SigningNodeRef) {.async: (raises: []).} =
429429
warn "Main loop failed with unexpected error", err_name = $exc.name,
430430
reason = $exc.msg
431431

432+
# This is trick to fool `asyncraises` from generating warning:
433+
# No exceptions possible with this operation, `error` always returns nil.
434+
if false:
435+
raise newException(SigningNodeError, "This error should never happen")
436+
432437
debug "Stopping main processing loop"
433438
var pending: seq[Future[void]]
434439
if not(sn.runKeystoreCachePruningLoopFut.finished()):

beacon_chain/nimbus_validator_client.nim

+5-37
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# beacon_chain
2-
# Copyright (c) 2018-2024 Status Research & Development GmbH
2+
# Copyright (c) 2018-2025 Status Research & Development GmbH
33
# Licensed and distributed under either of
44
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
55
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
@@ -161,38 +161,6 @@ proc initClock(
161161
current_slot = currentSlot, current_epoch = currentEpoch
162162
res
163163

164-
proc initMetrics(
165-
vc: ValidatorClientRef
166-
): Future[bool] {.async: (raises: [CancelledError]).} =
167-
if vc.config.metricsEnabled:
168-
let
169-
metricsAddress = vc.config.metricsAddress
170-
metricsPort = vc.config.metricsPort
171-
url = "http://" & $metricsAddress & ":" & $metricsPort & "/metrics"
172-
info "Starting metrics HTTP server", url = url
173-
let server =
174-
block:
175-
let res = MetricsHttpServerRef.new($metricsAddress, metricsPort)
176-
if res.isErr():
177-
error "Could not start metrics HTTP server", url = url,
178-
error_msg = res.error()
179-
return false
180-
res.get()
181-
vc.metricsServer = Opt.some(server)
182-
try:
183-
await server.start()
184-
except MetricsError as exc:
185-
error "Could not start metrics HTTP server", url = url,
186-
error_msg = exc.msg, error_name = exc.name
187-
return false
188-
true
189-
190-
proc shutdownMetrics(vc: ValidatorClientRef) {.async: (raises: []).} =
191-
if vc.config.metricsEnabled:
192-
if vc.metricsServer.isSome():
193-
info "Shutting down metrics HTTP server"
194-
await vc.metricsServer.get().close()
195-
196164
proc shutdownSlashingProtection(vc: ValidatorClientRef) =
197165
info "Closing slashing protection", path = vc.config.validatorsDir()
198166
vc.attachedValidators[].slashingProtection.close()
@@ -351,7 +319,7 @@ proc asyncInit(vc: ValidatorClientRef): Future[ValidatorClientRef] {.
351319

352320
vc.beaconClock = await vc.initClock()
353321

354-
if not(await initMetrics(vc)):
322+
vc.metricsServer = (await vc.config.initMetricsServer()).valueOr:
355323
raise newException(ValidatorClientError,
356324
"Could not initialize metrics server")
357325

@@ -368,7 +336,7 @@ proc asyncInit(vc: ValidatorClientRef): Future[ValidatorClientRef] {.
368336
vc.attachedValidators = validatorPool
369337

370338
if not(await initValidators(vc)):
371-
await vc.shutdownMetrics()
339+
await vc.metricsServer.stopMetricsServer()
372340
raise newException(ValidatorClientError,
373341
"Could not initialize local validators")
374342

@@ -432,7 +400,7 @@ proc asyncInit(vc: ValidatorClientRef): Future[ValidatorClientRef] {.
432400
)
433401
except CancelledError:
434402
debug "Initialization process interrupted"
435-
await vc.shutdownMetrics()
403+
await vc.metricsServer.stopMetricsServer()
436404
vc.shutdownSlashingProtection()
437405
return
438406

@@ -522,7 +490,7 @@ proc asyncRun*(
522490
except CancelledError:
523491
debug "Main loop interrupted"
524492

525-
await vc.shutdownMetrics()
493+
await vc.metricsServer.stopMetricsServer()
526494
vc.shutdownSlashingProtection()
527495

528496
if doppelEventFut.completed():

0 commit comments

Comments
 (0)