status-im / nim-chronos

Chronos - An efficient library for asynchronous programming
https://status-im.github.io/nim-chronos/
Apache License 2.0
362 stars 51 forks source link

return statement in try/finally block breaks executing multiple await statements in finally block #415

Closed cheatfate closed 1 year ago

cheatfate commented 1 year ago

Reproducible source

import chronos

type
  ClientResponse = object
    status*: int
    data*: string

proc subFoo1(): Future[int] {.async.} =
  await sleepAsync(100.milliseconds)
  return 200

proc subFoo2(): Future[string] {.async.} =
  await sleepAsync(100.milliseconds)
  return "SOMEDATA"

proc testFoo(): Future[ClientResponse] {.async.} =
  try:
    let status = await subFoo1()
    doAssert(status == 200)
    let data = await subFoo2()
    return ClientResponse(status: status, data: data)
  finally:
    echo "stage 1"
    await sleepAsync(100.milliseconds)
    echo "stage 2"
    await sleepAsync(200.milliseconds)
    echo "stage 3"

when isMainModule:
  echo waitFor testFoo()

Current output

stage 1
(status: 200, data: "SOMEDATA")

Expected output

stage 1
stage 2
stage 3
(status: 200, data: "SOMEDATA")

Workaround

If you change return ClientResponse(status: status, data: data) to result = ClientResponse(status: status, data: data) or implicitly return ClientResponse(status: status, data: data) all statements in finally will be executed.

Menduist commented 1 year ago

If you add waitFor sleepAsync(600.milliseconds), at the end you'll see that the stage 2 & 3 are also executed, but after the testFoo future has been completed So the closure iterator transformation seems correct, it's actually chronos/asyncdispatch which is completing the future too early