Skip to content

Commit

Permalink
Strict Future value / error access
Browse files Browse the repository at this point in the history
Strict future access is now always enabled, making it a panic to access
value/error when the Future is still pending - `read` and `readError`
raise catchable exceptions instead which may be preferable to use.

* remove `chronosStrictFutureAccess` enabling its functionality by
default
* disallow setting a cancellation callback on a non-pending future
  • Loading branch information
arnetheduck committed Nov 17, 2023
1 parent 1306170 commit e1c26a9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 25 deletions.
6 changes: 0 additions & 6 deletions chronos/config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
## in transition periods leading up to a breaking release that starts enforcing
## them and removes the option.
##
## `chronosPreviewV4` is a preview flag to enable v4 semantics - in particular,
## it enables strict exception checking and disables parts of the deprecated
## API and other changes being prepared for the upcoming release
##
## `chronosDebug` can be defined to enable several debugging helpers that come
## with a runtime cost - it is recommeneded to not enable these in production
## code.
Expand All @@ -26,8 +22,6 @@ const
##
## `Exception` handling may be removed in future chronos versions.

chronosStrictFutureAccess* {.booldefine.}: bool = defined(chronosPreviewV4)

chronosStackTrace* {.booldefine.}: bool = defined(chronosDebug)
## Include stack traces in futures for creation and completion points

Expand Down
20 changes: 8 additions & 12 deletions chronos/futures.nim
Original file line number Diff line number Diff line change
Expand Up @@ -209,31 +209,27 @@ func value*[T: not void](future: Future[T]): lent T =
##
## See `read` for a version that raises a catchable error when future
## has not completed.
when chronosStrictFutureAccess:
if not future.completed():
raiseFutureDefect("Future not completed while accessing value", future)
if not future.completed():
raiseFutureDefect("Future not completed while accessing value", future)

future.internalValue

func value*(future: Future[void]) =
## Return the value in a completed future - raises Defect when
## `fut.completed()` is `false`.
## Raises Defect when `fut.completed()` is `false`.
##
## See `read` for a version that raises a catchable error when future
## has not completed.
when chronosStrictFutureAccess:
if not future.completed():
raiseFutureDefect("Future not completed while accessing value", future)
if not future.completed():
raiseFutureDefect("Future not completed while accessing value", future)

func error*(future: FutureBase): ref CatchableError =
## Return the error of `future`, or `nil` if future did not fail.
##
## See `readError` for a version that raises a catchable error when the
## future has not failed.
when chronosStrictFutureAccess:
if not future.failed() and not future.cancelled():
raiseFutureDefect(
"Future not failed/cancelled while accessing error", future)
if not future.failed() and not future.cancelled():
raiseFutureDefect(
"Future not failed/cancelled while accessing error", future)

future.internalError

Expand Down
13 changes: 6 additions & 7 deletions chronos/internal/asyncfutures.nim
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,9 @@ proc tryCancel(future: FutureBase, loc: ptr SrcLoc): bool =
if not(isNil(future.internalChild)):
# If you hit this assertion, you should have used the `CancelledError`
# mechanism and/or use a regular `addCallback`
when chronosStrictFutureAccess:
doAssert future.internalCancelcb.isNil,
"futures returned from `{.async.}` functions must not use " &
"`cancelCallback`"
doAssert future.internalCancelcb.isNil,
"futures returned from `{.async.}` functions must not use " &
"`cancelCallback`"
tryCancel(future.internalChild, loc)
else:
if not(isNil(future.internalCancelcb)):
Expand Down Expand Up @@ -353,9 +352,9 @@ proc `cancelCallback=`*(future: FutureBase, cb: CallbackFunc) =
## This callback will be called immediately as ``future.cancel()`` invoked and
## must be set before future is finished.

when chronosStrictFutureAccess:
doAssert not future.finished(),
"cancellation callback must be set before finishing the future"
doAssert not future.finished(),
"cancellation callback must be set before finishing the future"

future.internalCancelcb = cb

{.push stackTrace: off.}
Expand Down

0 comments on commit e1c26a9

Please sign in to comment.