From 5d27b12b176dd3840445cec3c41f97a0a11bbe54 Mon Sep 17 00:00:00 2001 From: gmega Date: Thu, 7 Dec 2023 17:09:45 -0300 Subject: [PATCH] move instrumentation points for async future FSM into more correct locations --- chronos/asyncfutures2.nim | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/chronos/asyncfutures2.nim b/chronos/asyncfutures2.nim index 7beb7ba..0bc4f49 100644 --- a/chronos/asyncfutures2.nim +++ b/chronos/asyncfutures2.nim @@ -315,11 +315,11 @@ proc futureContinue*(fut: FutureBase) {.raises: [], gcsafe.} = # instead with its original body captured in `fut.closure`. var next: FutureBase template iterate = - while true: - when chronosProfiling: - if not isNil(onFutureExecEvent): - onFutureExecEvent(fut, Running) + when chronosProfiling: + if not isNil(onFutureExecEvent): + onFutureExecEvent(fut, Running) + while true: # Call closure to make progress on `fut` until it reaches `yield` (inside # `await` typically) or completes / fails / is cancelled next = fut.internalClosure(fut) @@ -327,11 +327,6 @@ proc futureContinue*(fut: FutureBase) {.raises: [], gcsafe.} = if fut.internalClosure.finished(): # Reached the end of the transformed proc break - # If we got thus far, means the future is paused. - when chronosProfiling: - if not isNil(onFutureExecEvent): - onFutureExecEvent(fut, Paused) - if next == nil: raiseAssert "Async procedure (" & ($fut.location[LocationKind.Create]) & ") yielded `nil`, are you await'ing a `nil` Future?" @@ -342,6 +337,10 @@ proc futureContinue*(fut: FutureBase) {.raises: [], gcsafe.} = GC_ref(fut) next.addCallback(CallbackFunc(internalContinue), cast[pointer](fut)) + when chronosProfiling: + if not isNil(onFutureExecEvent): + onFutureExecEvent(fut, Paused) + # return here so that we don't remove the closure below return