Skip to content

Awaiting for Future[void] fails if future never completes or fails #14564

Open
@Q-Master

Description

@Q-Master

When trying to wait for the guard future I've encountered a wrong behaviour, IMHO. The idea is to wait some arbitrary future, which might be completed from some outer code not depending on guarded async code completed or not.

This code

import asyncdispatch

var guardfuture = newFuture[void]("test.guardfuture")

proc a() {.async.} =
    await sleepAsync(1000)

proc b() {.async.} =
    var af: auto = a()
    await guardfuture
    await af

proc fireGuard() =
    guardfuture.complete()

proc testGuard() {.async.} =
    await b()

proc stop() {.noconv.} =
    fireGuard()

when isMainModule:
    setControlCHook(stop)
    waitFor testGuard()
    quit(QuitSuccess)

should wait forever till ctrl+C pressed, but it fails just after proc a completes, with a message

/usr/local/Cellar/nim/1.2.0/nim/lib/pure/asyncdispatch.nim(1886) waitFor
/usr/local/Cellar/nim/1.2.0/nim/lib/pure/asyncdispatch.nim(1576) poll
/usr/local/Cellar/nim/1.2.0/nim/lib/pure/asyncdispatch.nim(1291) runOnce
Error: unhandled exception: No handles or timers registered in dispatcher. [ValueError]

but if I comment out awaiting the guardfuture in proc a, everything works without errors, but not as expected and quits just after a() completes.

If I press ctrl+c while timer is ticking, everything also works as a charm.

$ nim -v
Nim Compiler Version 1.2.0 [MacOSX: amd64]

The latest build from source has the same issue.

$ ./bin/nim -v
Nim Compiler Version 1.3.5 [MacOSX: amd64]

Metadata

Metadata

Assignees

No one assigned

    Labels

    AsyncEverything related to Nim's async

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions