Fix cancellation deprecate warnings. (#5454)

Add noCancel in exception handlers.
Refactor pending cancellations code.
This commit is contained in:
Eugene Kabanov 2023-09-24 10:28:09 +03:00 committed by GitHub
parent e3fe762ec9
commit 98e08a9e02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 60 deletions

View File

@ -86,7 +86,7 @@ proc lazyWaiter(node: BeaconNodeServerRef, request: FutureBase,
ApiFailure.Communication, requestName, strategy, node, ApiFailure.Communication, requestName, strategy, node,
$request.error.msg) $request.error.msg)
node.updateStatus(RestBeaconNodeStatus.Offline, failure) node.updateStatus(RestBeaconNodeStatus.Offline, failure)
except CancelledError as exc: except CancelledError:
await cancelAndWait(request) await cancelAndWait(request)
proc lazyWait(nodes: seq[BeaconNodeServerRef], requests: seq[FutureBase], proc lazyWait(nodes: seq[BeaconNodeServerRef], requests: seq[FutureBase],
@ -257,7 +257,7 @@ template firstSuccessParallel*(
for future in pendingRequests.items(): for future in pendingRequests.items():
if not(future.finished()): if not(future.finished()):
pendingCancel.add(future.cancelAndWait()) pendingCancel.add(future.cancelAndWait())
await allFutures(pendingCancel) await noCancel allFutures(pendingCancel)
raise exc raise exc
except CatchableError as exc: except CatchableError as exc:
# This should not be happened, because allFutures() and race() did not # This should not be happened, because allFutures() and race() did not
@ -423,7 +423,7 @@ template bestSuccess*(
if not(future.finished()): if not(future.finished()):
pendingCancel.add(future.cancelAndWait()) pendingCancel.add(future.cancelAndWait())
# Awaiting cancellations. # Awaiting cancellations.
await allFutures(pendingCancel) await noCancel allFutures(pendingCancel)
raise exc raise exc
except CatchableError as exc: except CatchableError as exc:
# This should not be happened, because allFutures() and race() # This should not be happened, because allFutures() and race()
@ -526,7 +526,7 @@ template onceToAll*(
pendingCancel.add(fut.cancelAndWait()) pendingCancel.add(fut.cancelAndWait())
if not(isNil(timerFut)) and not(timerFut.finished()): if not(isNil(timerFut)) and not(timerFut.finished()):
pendingCancel.add(timerFut.cancelAndWait()) pendingCancel.add(timerFut.cancelAndWait())
await allFutures(pendingCancel) await noCancel allFutures(pendingCancel)
raise exc raise exc
except CatchableError: except CatchableError:
# This should not be happened, because allFutures() and race() did not # This should not be happened, because allFutures() and race() did not
@ -664,7 +664,7 @@ template firstSuccessSequential*(
pending.add(bodyFut.cancelAndWait()) pending.add(bodyFut.cancelAndWait())
if not(isNil(timerFut)) and not(timerFut.finished()): if not(isNil(timerFut)) and not(timerFut.finished()):
pending.add(timerFut.cancelAndWait()) pending.add(timerFut.cancelAndWait())
await allFutures(pending) await noCancel allFutures(pending)
raise exc raise exc
except CatchableError as exc: except CatchableError as exc:
# This case should not happen. # This case should not happen.

View File

@ -205,10 +205,9 @@ proc produceAndPublishAttestations*(service: AttestationServiceRef,
try: try:
await allFutures(pendingAttestations) await allFutures(pendingAttestations)
except CancelledError as exc: except CancelledError as exc:
for fut in pendingAttestations: let pending = pendingAttestations
if not(fut.finished()): .filterIt(not(it.finished())).mapIt(it.cancelAndWait())
fut.cancel() await noCancel allFutures(pending)
await allFutures(pendingAttestations)
raise exc raise exc
for future in pendingAttestations: for future in pendingAttestations:
@ -300,10 +299,9 @@ proc produceAndPublishAggregates(service: AttestationServiceRef,
try: try:
await allFutures(pendingAggregates) await allFutures(pendingAggregates)
except CancelledError as exc: except CancelledError as exc:
for fut in pendingAggregates: let pending = pendingAggregates
if not(fut.finished()): .filterIt(not(it.finished())).mapIt(it.cancelAndWait())
fut.cancel() await noCancel allFutures(pending)
await allFutures(pendingAggregates)
raise exc raise exc
for future in pendingAggregates: for future in pendingAggregates:
@ -393,7 +391,7 @@ proc spawnAttestationTasks(service: AttestationServiceRef,
except CancelledError as exc: except CancelledError as exc:
# Cancelling all the pending tasks. # Cancelling all the pending tasks.
let pending = tasks.filterIt(not(it.finished())).mapIt(it.cancelAndWait()) let pending = tasks.filterIt(not(it.finished())).mapIt(it.cancelAndWait())
await allFutures(pending) await noCancel allFutures(pending)
raise exc raise exc
except CatchableError as exc: except CatchableError as exc:
error "Unexpected error while processing attestation duties", error "Unexpected error while processing attestation duties",

View File

@ -465,7 +465,7 @@ proc addOrReplaceProposers*(vc: ValidatorClientRef, epoch: Epoch,
debug "Cancelling running proposal duty task", debug "Cancelling running proposal duty task",
slot = task.duty.slot, slot = task.duty.slot,
validator = shortLog(task.duty.pubkey) validator = shortLog(task.duty.pubkey)
task.future.cancel() task.future.cancelSoon()
else: else:
# If task is already running for proper slot, we keep it alive. # If task is already running for proper slot, we keep it alive.
debug "Keep running previous proposal duty task", debug "Keep running previous proposal duty task",
@ -714,18 +714,18 @@ proc runBlockPollMonitor(service: BlockServiceRef,
break break
res res
if blockReceived: if blockReceived:
var pending: seq[Future[void]] let pending =
for future in pendingTasks: pendingTasks.filterIt(not(it.finished())).mapIt(it.cancelAndWait())
if not(future.finished()): pending.add(future.cancelAndWait()) # We use `noCancel` here because its cleanup and we have `break`
await allFutures(pending) # after it.
await noCancel allFutures(pending)
break break
pendingTasks.keepItIf(it != completedFuture) pendingTasks.keepItIf(it != completedFuture)
if len(pendingTasks) == 0: break if len(pendingTasks) == 0: break
except CancelledError as exc: except CancelledError as exc:
var pending: seq[Future[void]] let pending =
for future in pendingTasks: pendingTasks.filterIt(not(it.finished())).mapIt(it.cancelAndWait())
if not(future.finished()): pending.add(future.cancelAndWait()) await noCancel allFutures(pending)
await allFutures(pending)
raise exc raise exc
except CatchableError as exc: except CatchableError as exc:
warn "An unexpected error occurred while running block monitoring", warn "An unexpected error occurred while running block monitoring",
@ -755,10 +755,9 @@ proc runBlockMonitor(service: BlockServiceRef) {.async.} =
try: try:
await allFutures(pendingTasks) await allFutures(pendingTasks)
except CancelledError as exc: except CancelledError as exc:
var pending: seq[Future[void]] let pending =
for future in pendingTasks: pendingTasks.filterIt(not(it.finished())).mapIt(it.cancelAndWait())
if not(future.finished()): pending.add(future.cancelAndWait()) await noCancel allFutures(pending)
await allFutures(pending)
raise exc raise exc
except CatchableError as exc: except CatchableError as exc:
warn "An unexpected error occurred while running block monitoring", warn "An unexpected error occurred while running block monitoring",
@ -781,12 +780,12 @@ proc mainLoop(service: BlockServiceRef) {.async.} =
err_msg = exc.msg err_msg = exc.msg
# We going to cleanup all the pending proposer tasks. # We going to cleanup all the pending proposer tasks.
var res: seq[Future[void]] var res: seq[FutureBase]
for epoch, data in vc.proposers.pairs(): for epoch, data in vc.proposers.pairs():
for duty in data.duties.items(): for duty in data.duties.items():
if not(duty.future.finished()): if not(duty.future.finished()):
res.add(duty.future.cancelAndWait()) res.add(duty.future.cancelAndWait())
await allFutures(res) await noCancel allFutures(res)
proc init*(t: typedesc[BlockServiceRef], proc init*(t: typedesc[BlockServiceRef],
vc: ValidatorClientRef): Future[BlockServiceRef] {.async.} = vc: ValidatorClientRef): Future[BlockServiceRef] {.async.} =

View File

@ -1581,10 +1581,9 @@ proc fillSyncCommitteeSelectionProofs*(
try: try:
discard await race(pendingRequests) discard await race(pendingRequests)
except CancelledError as exc: except CancelledError as exc:
var pending: seq[Future[void]] let pending = pendingRequests
for future in pendingRequests: .filterIt(not(it.finished())).mapIt(it.cancelAndWait())
if not(future.finished()): pending.add(future.cancelAndWait()) await noCancel allFutures(pending)
await allFutures(pending)
raise exc raise exc
(requests, pendingRequests) = (requests, pendingRequests) =
@ -1660,10 +1659,9 @@ proc fillAttestationSelectionProofs*(
try: try:
discard await race(pendingRequests) discard await race(pendingRequests)
except CancelledError as exc: except CancelledError as exc:
var pending: seq[Future[void]] let pending = pendingRequests
for future in pendingRequests: .filterIt(not(it.finished())).mapIt(it.cancelAndWait())
if not(future.finished()): pending.add(future.cancelAndWait()) await noCancel allFutures(pending)
await allFutures(pending)
raise exc raise exc
(requests, pendingRequests) = (requests, pendingRequests) =

View File

@ -333,11 +333,9 @@ proc checkNodes*(service: FallbackServiceRef): Future[bool] {.async.} =
if fut.completed() and fut.read(): if fut.completed() and fut.read():
res = true res = true
except CancelledError as exc: except CancelledError as exc:
var pending: seq[Future[void]] let pending = pendingChecks
for future in pendingChecks: .filterIt(not(it.finished())).mapIt(it.cancelAndWait())
if not(future.finished()): await noCancel allFutures(pending)
pending.add(future.cancelAndWait())
await allFutures(pending)
raise exc raise exc
return res return res
@ -456,10 +454,9 @@ proc processTimeMonitoring(service: FallbackServiceRef) {.async.} =
pendingChecks.add(service.runTimeMonitor(node)) pendingChecks.add(service.runTimeMonitor(node))
await allFutures(pendingChecks) await allFutures(pendingChecks)
except CancelledError as exc: except CancelledError as exc:
var pending: seq[Future[void]] let pending = pendingChecks
for future in pendingChecks: .filterIt(not(it.finished())).mapIt(it.cancelAndWait())
if not(future.finished()): pending.add(future.cancelAndWait()) await noCancel allFutures(pending)
await allFutures(pending)
raise exc raise exc
except CatchableError as exc: except CatchableError as exc:
warn "An unexpected error occurred while running time monitoring", warn "An unexpected error occurred while running time monitoring",

View File

@ -113,10 +113,9 @@ proc produceAndPublishSyncCommitteeMessages(service: SyncCommitteeServiceRef,
try: try:
await allFutures(pendingSyncCommitteeMessages) await allFutures(pendingSyncCommitteeMessages)
except CancelledError as exc: except CancelledError as exc:
for fut in pendingSyncCommitteeMessages: let pending = pendingSyncCommitteeMessages
if not(fut.finished()): .filterIt(not(it.finished())).mapIt(it.cancelAndWait())
fut.cancel() await noCancel allFutures(pending)
await allFutures(pendingSyncCommitteeMessages)
raise exc raise exc
for future in pendingSyncCommitteeMessages: for future in pendingSyncCommitteeMessages:
@ -253,10 +252,9 @@ proc produceAndPublishContributions(service: SyncCommitteeServiceRef,
try: try:
discard await race(pendingFutures) discard await race(pendingFutures)
except CancelledError as exc: except CancelledError as exc:
var pending: seq[Future[void]] let pending = pendingFutures
for future in pendingFutures: .filterIt(not(it.finished())).mapIt(it.cancelAndWait())
if not(future.finished()): pending.add(future.cancelAndWait()) await noCancel allFutures(pending)
await allFutures(pending)
raise exc raise exc
var completed: seq[int] var completed: seq[int]
@ -308,12 +306,11 @@ proc produceAndPublishContributions(service: SyncCommitteeServiceRef,
var errored, succeed, failed = 0 var errored, succeed, failed = 0
try: try:
await allFutures(pendingAggregates) await allFutures(pendingAggregates)
except CancelledError as err: except CancelledError as exc:
for fut in pendingAggregates: let pending = pendingAggregates
if not(fut.finished()): .filterIt(not(it.finished())).mapIt(it.cancelAndWait())
fut.cancel() await noCancel allFutures(pending)
await allFutures(pendingAggregates) raise exc
raise err
for future in pendingAggregates: for future in pendingAggregates:
if future.completed(): if future.completed():