2020-07-06 06:33:13 +00:00
|
|
|
#
|
|
|
|
# Chronos Debugging Utilities
|
|
|
|
#
|
|
|
|
# (c) Copyright 2020-Present Status Research & Development GmbH
|
|
|
|
#
|
|
|
|
# Licensed under either of
|
|
|
|
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
|
|
|
# MIT license (LICENSE-MIT)
|
exception tracking (#166)
* exception tracking
This PR adds minimal exception tracking to chronos, moving the goalpost
one step further.
In particular, it becomes invalid to raise exceptions from `callSoon`
callbacks: this is critical for writing correct error handling because
there's no reasonable way that a user of chronos can possibly _reason_
about exceptions coming out of there: the event loop will be in an
indeterminite state when the loop is executing an _random_ callback.
As expected, there are several issues in the error handling of chronos:
in particular, it will end up in an inconsistent internal state whenever
the selector loop operations fail, because the internal state update
functions are not written in an exception-safe way. This PR turns this
into a Defect, which probably is not the optimal way of handling things
- expect more work to be done here.
Some API have no way of reporting back errors to callers - for example,
when something fails in the accept loop, there's not much it can do, and
no way to report it back to the user of the API - this has been fixed
with the new accept flow - the old one should be deprecated.
Finally, there is information loss in the API: in composite operations
like `poll` and `waitFor` there's no way to differentiate internal
errors from user-level errors originating from callbacks.
* store `CatchableError` in future
* annotate proc's with correct raises information
* `selectors2` to avoid non-CatchableError IOSelectorsException
* `$` should never raise
* remove unnecessary gcsafe annotations
* fix exceptions leaking out of timer waits
* fix some imports
* functions must signal raising the union of all exceptions across all
platforms to enable cross-platform code
* switch to unittest2
* add `selectors2` which supercedes the std library version and fixes
several exception handling issues in there
* fixes
* docs, platform-independent eh specifiers for some functions
* add feature flag for strict exception mode
also bump version to 3.0.0 - _most_ existing code should be compatible
with this version of exception handling but some things might need
fixing - callbacks, existing raises specifications etc.
* fix AsyncCheck for non-void T
2021-03-24 09:08:33 +00:00
|
|
|
|
2022-08-06 10:56:06 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
exception tracking (#166)
* exception tracking
This PR adds minimal exception tracking to chronos, moving the goalpost
one step further.
In particular, it becomes invalid to raise exceptions from `callSoon`
callbacks: this is critical for writing correct error handling because
there's no reasonable way that a user of chronos can possibly _reason_
about exceptions coming out of there: the event loop will be in an
indeterminite state when the loop is executing an _random_ callback.
As expected, there are several issues in the error handling of chronos:
in particular, it will end up in an inconsistent internal state whenever
the selector loop operations fail, because the internal state update
functions are not written in an exception-safe way. This PR turns this
into a Defect, which probably is not the optimal way of handling things
- expect more work to be done here.
Some API have no way of reporting back errors to callers - for example,
when something fails in the accept loop, there's not much it can do, and
no way to report it back to the user of the API - this has been fixed
with the new accept flow - the old one should be deprecated.
Finally, there is information loss in the API: in composite operations
like `poll` and `waitFor` there's no way to differentiate internal
errors from user-level errors originating from callbacks.
* store `CatchableError` in future
* annotate proc's with correct raises information
* `selectors2` to avoid non-CatchableError IOSelectorsException
* `$` should never raise
* remove unnecessary gcsafe annotations
* fix exceptions leaking out of timer waits
* fix some imports
* functions must signal raising the union of all exceptions across all
platforms to enable cross-platform code
* switch to unittest2
* add `selectors2` which supercedes the std library version and fixes
several exception handling issues in there
* fixes
* docs, platform-independent eh specifiers for some functions
* add feature flag for strict exception mode
also bump version to 3.0.0 - _most_ existing code should be compatible
with this version of exception handling but some things might need
fixing - callbacks, existing raises specifications etc.
* fix AsyncCheck for non-void T
2021-03-24 09:08:33 +00:00
|
|
|
|
2023-03-31 05:35:04 +00:00
|
|
|
import "."/[asyncloop, config]
|
2021-08-26 11:22:29 +00:00
|
|
|
export asyncloop
|
2020-07-06 06:33:13 +00:00
|
|
|
|
2023-03-31 05:35:04 +00:00
|
|
|
when chronosFutureTracking:
|
2021-10-21 14:22:11 +00:00
|
|
|
import stew/base10
|
|
|
|
|
2020-07-06 06:33:13 +00:00
|
|
|
const
|
|
|
|
AllFutureStates* = {FutureState.Pending, FutureState.Cancelled,
|
2023-05-25 15:31:35 +00:00
|
|
|
FutureState.Completed, FutureState.Failed}
|
|
|
|
WithoutCompleted* = {FutureState.Pending, FutureState.Cancelled,
|
2020-07-06 06:33:13 +00:00
|
|
|
FutureState.Failed}
|
|
|
|
OnlyPending* = {FutureState.Pending}
|
2023-05-25 15:31:35 +00:00
|
|
|
OnlyCompleted* = {FutureState.Completed}
|
2020-07-06 06:33:13 +00:00
|
|
|
|
|
|
|
proc dumpPendingFutures*(filter = AllFutureStates): string =
|
|
|
|
## Dump all `pending` Future[T] objects.
|
|
|
|
##
|
|
|
|
## This list will contain:
|
|
|
|
## 1. Future[T] objects with ``FutureState.Pending`` state (this Futures are
|
|
|
|
## not yet finished).
|
|
|
|
## 2. Future[T] objects with ``FutureState.Finished/Cancelled/Failed`` state
|
|
|
|
## which callbacks are scheduled, but not yet fully processed.
|
2023-03-31 05:35:04 +00:00
|
|
|
when chronosFutureTracking:
|
2023-01-10 16:08:54 +00:00
|
|
|
var count = 0'u
|
|
|
|
var res = ""
|
2020-07-08 16:48:01 +00:00
|
|
|
for item in pendingFutures():
|
|
|
|
if item.state in filter:
|
|
|
|
inc(count)
|
|
|
|
let loc = item.location[LocCreateIndex][]
|
|
|
|
let procedure = $loc.procedure
|
|
|
|
let filename = $loc.file
|
|
|
|
let procname = if len(procedure) == 0:
|
|
|
|
"\"unspecified\""
|
|
|
|
else:
|
|
|
|
"\"" & procedure & "\""
|
2021-10-21 14:22:11 +00:00
|
|
|
let item = "Future[" & Base10.toString(item.id) & "] with name " &
|
|
|
|
$procname & " created at " & "<" & filename & ":" &
|
|
|
|
Base10.toString(uint(loc.line)) & ">" &
|
2020-07-08 16:48:01 +00:00
|
|
|
" and state = " & $item.state & "\n"
|
|
|
|
res.add(item)
|
2021-10-21 14:22:11 +00:00
|
|
|
Base10.toString(count) & " pending Future[T] objects found:\n" & $res
|
|
|
|
else:
|
|
|
|
"0 pending Future[T] objects found\n"
|
2020-07-06 06:33:13 +00:00
|
|
|
|
2021-10-21 14:22:11 +00:00
|
|
|
proc pendingFuturesCount*(filter: set[FutureState]): uint =
|
2020-07-06 06:33:13 +00:00
|
|
|
## Returns number of `pending` Future[T] objects which satisfy the ``filter``
|
|
|
|
## condition.
|
|
|
|
##
|
|
|
|
## If ``filter`` is equal to ``AllFutureStates`` Operation's complexity is
|
|
|
|
## O(1), otherwise operation's complexity is O(n).
|
2023-03-31 05:35:04 +00:00
|
|
|
when chronosFutureTracking:
|
2020-07-08 16:48:01 +00:00
|
|
|
if filter == AllFutureStates:
|
|
|
|
pendingFuturesCount()
|
|
|
|
else:
|
2021-10-21 14:22:11 +00:00
|
|
|
var res = 0'u
|
2020-07-08 16:48:01 +00:00
|
|
|
for item in pendingFutures():
|
|
|
|
if item.state in filter:
|
|
|
|
inc(res)
|
|
|
|
res
|
2020-07-06 06:33:13 +00:00
|
|
|
else:
|
2021-10-21 14:22:11 +00:00
|
|
|
0'u
|