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 circular reference in timer (fixes #496) #510

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 7 additions & 5 deletions chronos/asyncproc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1010,12 +1010,14 @@ else:
retFuture.fail(newException(AsyncProcessError,
osErrorMsg(res.error())))

timer = nil

proc cancellation(udata: pointer) {.gcsafe.} =
if not(retFuture.finished()):
if not(isNil(timer)):
clearTimer(timer)
# Ignore any errors because of cancellation.
discard removeProcess2(processHandle)
if not(isNil(timer)):
clearTimer(timer)
timer = nil
# Ignore any errors because of cancellation.
discard removeProcess2(processHandle)

if timeout != InfiniteDuration:
timer = setTimer(Moment.fromNow(timeout), continuation, cast[pointer](2))
Expand Down
11 changes: 9 additions & 2 deletions chronos/internal/asyncfutures.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1338,13 +1338,15 @@ proc sleepAsync*(duration: Duration): Future[void] {.
proc completion(data: pointer) {.gcsafe.} =
if not(retFuture.finished()):
retFuture.complete()
timer = nil # Release circular reference (for gc:arc)

proc cancellation(udata: pointer) {.gcsafe.} =
if not(retFuture.finished()):
if not isNil(timer):
clearTimer(timer)
timer = nil # Release circular reference (for gc:arc)

retFuture.cancelCallback = cancellation
timer = setTimer(moment, completion, cast[pointer](retFuture))
timer = setTimer(moment, completion)
return retFuture

proc sleepAsync*(ms: int): Future[void] {.
Expand Down Expand Up @@ -1437,6 +1439,7 @@ proc withTimeout*[T](fut: Future[T], timeout: Duration): Future[bool] {.
if not(isNil(timer)):
clearTimer(timer)
fut.completeFuture()
timer = nil

# TODO: raises annotation shouldn't be needed, but likely similar issue as
# https://github.com/nim-lang/Nim/issues/17369
Expand All @@ -1447,6 +1450,7 @@ proc withTimeout*[T](fut: Future[T], timeout: Duration): Future[bool] {.
fut.cancelSoon()
else:
fut.completeFuture()
timer = nil

if fut.finished():
retFuture.complete(true)
Expand Down Expand Up @@ -1499,6 +1503,7 @@ proc waitImpl[F: SomeFuture](fut: F, retFuture: auto, timeout: Duration): auto =
if not(isNil(timer)):
clearTimer(timer)
fut.completeFuture()
timer = nil

var cancellation: proc(udata: pointer) {.gcsafe, raises: [].}
cancellation = proc(udata: pointer) {.gcsafe, raises: [].} =
Expand All @@ -1509,6 +1514,8 @@ proc waitImpl[F: SomeFuture](fut: F, retFuture: auto, timeout: Duration): auto =
else:
fut.completeFuture()

timer = nil

if fut.finished():
fut.completeFuture()
else:
Expand Down
Loading