Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix or deadlock issue. #517

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions chronos/internal/asyncfutures.nim
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ template orImpl*[T, Y](fut1: Future[T], fut2: Future[Y]): untyped =
fut2.addCallback(cb)

retFuture.cancelCallback = cancellation
return retFuture
retFuture

proc `or`*[T, Y](fut1: Future[T], fut2: Future[Y]): Future[void] =
## Returns a future which will complete once either ``fut1`` or ``fut2``
Expand All @@ -801,7 +801,7 @@ proc `or`*[T, Y](fut1: Future[T], fut2: Future[Y]): Future[void] =
## completed, the result future will also be completed.
##
## If cancelled, ``fut1`` and ``fut2`` futures WILL NOT BE cancelled.
var retFuture = newFuture[void]("chronos.or")
var retFuture = newFuture[void]("chronos.or()")
orImpl(fut1, fut2)


Expand Down Expand Up @@ -1667,8 +1667,7 @@ proc `or`*[T, Y, E1, E2](
type
InternalRaisesFutureRaises = union(E1, E2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this might be a problem if neither of E1, E2 raise CancelledError

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How i can do this? Because looks like prepend and union are not working here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in d5fd666


let
retFuture = newFuture[void]("chronos.wait()", {FutureFlag.OwnCancelSchedule})
let retFuture = newFuture[void]("chronos.or()", {})
arnetheduck marked this conversation as resolved.
Show resolved Hide resolved
orImpl(fut1, fut2)

proc wait*(fut: InternalRaisesFuture, timeout = InfiniteDuration): auto =
Expand Down
13 changes: 13 additions & 0 deletions tests/testbugs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ suite "Asynchronous issues test suite":
await server.closeWait()
return true

proc testOrDeadlock(): Future[bool] {.async.} =
proc f(): Future[void] {.async.} =
await sleepAsync(2.seconds) or sleepAsync(1.seconds)
let fx = f()
try:
await fx.cancelAndWait().wait(2.seconds)
except AsyncTimeoutError:
return false
true

test "Issue #6":
check waitFor(issue6()) == true

Expand All @@ -152,3 +162,6 @@ suite "Asynchronous issues test suite":

test "IndexError crash test":
check waitFor(testIndexError()) == true

test "`or` deadlock [#516] test":
check waitFor(testOrDeadlock()) == true
Loading