Feature flags allow consumers of chronos to target versions with and
without certain features via compile-time selection. The first feature
flag added is for raise tracking support.
* simplify `closeWait` implementations
* remove redundant cancellation callbacks
* use `noCancel` to avoid forgetting the right future flags
* add a few missing raises trackers
* enforce `OwnCancelSchedule` on manually created futures that don't raise `CancelledError`
* ensure cancellations don't reach internal futures
Because the callback types were used explicitly in some consumers of
chronos, the change of type introduces a backwards incompatibility
preventing a smooth transition to v4 for code that doesn't uses
`raises`.
This PR restores backwards compatibility at the expense of introducing a
new type with a potentially ugly name - that said, there is already
precedence for using numbered names to provide new error handling
strategy in chronos.
* Fixes.
* Make httpcommon no-raises.
* Make httpbodyrw no-raises.
* Make multipart no-raises.
* Make httpdebug no-raises.
* Make httpagent no-raises.
* Make httpclient no-raises.
* Make httpserver/shttpserver no-raises.
* fix prepend/remove when E is noraises
---------
Co-authored-by: Jacek Sieka <jacek@status.im>
This avoids copies here and there throughout the pipeline - ie
`copyString` and friends can often be avoided when moving things into
and out of futures
Annoyingly, one has to sprinkle the codebase liberally with `sink` and
`move` for the pipeline to work well - sink stuff _generally_ works
better in orc/arc
Looking at nim 1.6/refc, sink + local variable + move generates the best
code:
msg directly:
```nim
T1_ = (*colonenv_).msg1; (*colonenv_).msg1 = copyStringRC1(msg);
```
local copy without move:
```nim
T60_ = (*colonenv_).localCopy1; (*colonenv_).localCopy1 =
copyStringRC1(msg);
```
local copy with move:
```nim
asgnRef((void**) (&(*colonenv_).localCopy1), msg);
```
Annoyingly, sink is also broken for refc+literals as it tries to
changes the refcount of the literal as part of the move (which shouldn't
be happening, but here we are), so we have to use a hack to find
literals and avoid moving them.
* Fix transports addresses functions should not return so many exceptions.
* Add raising `Defect` functions to AsyncQueue.
* Add raises/asyncraises into async streams.
* Remove `Safe` primitives.
Make AsyncStreamError to be ancestor of AsyncError.
Make AsyncStreamReader/Writer loops requirement to not raise any exceptions
* Remove `par` fields.
* Remove `par` fields from TLSStream.
* Attempt to lower memory usage.
Dedicated exceptions for `read` failures reduce the risk of mixing up
"user" exceptions with those of Future itself. The risk still exists, if
the user allows a chronos exception to bubble up explicitly.
Because `await` structurally guarantees that the Future is not `pending`
at the time of `read`, it does not raise this new exception.
* introduce `FuturePendingError` and `FutureCompletedError` when
`read`:ing a future of uncertain state
* fix `waitFor` / `read` to return `lent` values
* simplify code generation for `void`-returning async procs
* document `Raising` type helper
With these fixes, `transports`/`asyncsync` correctly propagate and document their raises information - generally, most transport functions (send etc) raise `TransportError` and `CancelledError` - `closeWait` is special in that it generally doesn't fail.
This PR introduces the syntax `Future[void].Raises([types])` to create the `InternalRaisesFuture` type with the correct encoding for the types - this allows it to be used in user code while retaining the possibility to change the internal representation down the line.
* introduce raising constraints on stream callbacks - these constraints now give a warning when called with a callback that can raise exceptions (raising callbacks would crash
* fix fail and its tests, which wasn't always given a good generic match
* work around nim bugs related to macro expansion of generic types
* make sure transports raise only `TransportError`-derived exceptions (and `CancelledError`)
* introduce user guide based on `mdbook`
* set up structure for adding simple `chronos` usage examples
* move most readme content to book
* ci deploys book and api guide automatically
* remove most of existing engine docs (obsolete)
* Fix waitFor() should not exit earlier last callback will be scheduled.
* Tune tests to use less memory.
* Fix `testutils`. There is no more last poll() needed.
* Update chronos/internal/asyncfutures.nim
---------
Co-authored-by: Jacek Sieka <jacek@status.im>
This PR replaces the global strict exception mode with an option to
handle `Exception` per function while at the same time enabling strict
exception checking globally by default as has been planned for v4.
`handleException` mode raises `AsyncExceptionError` to distinguish it
from `ValueError` which may originate from user code.
* remove obsolete 1.2 config options
Per discussion in
https://github.com/status-im/nim-chronos/pull/251#issuecomment-1559233139,
`async: (parameters..)` is introduced as a way to customize the async
transformation instead of relying on separate keywords (like
asyncraises).
Two parameters are available as of now:
`raises`: controls the exception effect tracking
`raw`: disables body transformation
Parameters are added to `async` as a tuple allowing more params to be
added easily in the future:
```nim:
proc f() {.async: (name: value, ...).}`
```
* introduce `asyncraises` to core future utilities
Similar to the introduction of `raises` into a codebase, `asyncraises`
needs to be introduced gradually across all functionality before
deriving benefit.
This is a first introduction along with utilities to manage raises lists
and transform them at compile time.
Several scenarios ensue:
* for trivial cases, adding `asyncraises` is enough and the framework
deduces the rest
* some functions "add" new asyncraises (similar to what `raise` does in
"normal" code) - for example `wait` may raise all exceptions of the
future passed to it and additionally a few of its own - this requires
extending the raises list
* som functions "remove" raises (similar to what `try/except` does) such
as `nocancel` with blocks cancellations and therefore reduce the raising
set
Both of the above cases are currently handled by a macro, but depending
on the situation lead to code organisation issues around return types
and pragma limitations - in particular, to keep `asyncraises`
backwards-compatibility, some code needs to exist in two versions which
somewhat complicates the implementation.
* add `asyncraises` versions for several `asyncfutures` utilities
* when assigning exceptions to a `Future` via `fail`, check at compile
time if possible and at runtime if not that the exception matches
constraints
* fix `waitFor` comments
* move async raises to separate module, implement `or`
* Introduce chronos/internals, move some code
This PR breaks the include dependencies between `asyncfutures2` and
`asyncmacros2` by moving the dispatcher and some other code to a new
module.
This step makes it easier to implement `asyncraises` support for future
utilities like `allFutures` etc avoiding the need to play tricks with
include order etc.
Future PR:s may further articulate the difference between "internal"
stuff subject to API breakage and regular public API intended for end
users (rather than advanced integrators).
* names
* windows fix
* Exception tracking v2
* some fixes
* Nim 1.2 compat
* simpler things
* Fixes for libp2p
* Fixes for strictException
* better await exception check
* Fix for template async proc
* make async work with procTy
* FuturEx is now a ref object type
* add tests
* update test
* update readme
* Switch to asyncraises pragma
* Address tests review comments
* Rename FuturEx to RaiseTrackingFuture
* Fix typo
* Split asyncraises into async, asyncraises
* Add -d:chronosWarnMissingRaises
* Add comment to RaiseTrackingFuture
* Allow standalone asyncraises
* CheckedFuture.fail type checking
* First cleanup
* Remove useless line
* Review comments
* nimble: Remove #head from unittest2
* Remove implict raises: CancelledError
* Move checkFutureExceptions to asyncfutures2
* Small refacto
* small cleanup
* Complete in closure finally
* cleanup tests, add comment
* bump
* chronos is not compatible with nim 1.2 anymore
* re-add readme modifications
* fix special exception handlers
* also propagate excetion type in `read`
* `RaiseTrackingFuture` -> `InternalRaisesFuture`
Use internal naming scheme for RTF (this type should only be accessed
via asyncraises)
* use `internalError` for error reading
* oops
* 2.0 workarounds
* again
* remove try/finally for non-raising functions
* Revert "remove try/finally for non-raising functions"
This reverts commit 86bfeb5c972ef379a3bd34e4a16cd158a7455721.
`finally` is needed if code returns early :/
* fixes
* avoid exposing `newInternalRaisesFuture` in manual macro code
* avoid unnecessary codegen for `Future[void]`
* avoid reduntant block around async proc body
* simplify body generation for forward declarations with comment but no
body
* avoid duplicate `gcsafe` annotiations
* line info for return at end of async proc
* expand tests
* fix comments, add defer test
---------
Co-authored-by: Jacek Sieka <jacek@status.im>
* Add callTick and stream cancellation tests.
* Fix stepsAsync() test.
* Cancellation changes.
* Update and add more cancellation tests.
* Fix Posix shutdown call to handle ENOTCONN error.
* With new changes to to cancellation its now possible.
* Refactor testsoon.nim to not produce artifacts after tests are finished.
* Debugging MacOS issue.
* Adjust flaky test times.
* Fix issue.
* Add test for issue #334 which was also addressed in this PR.
Avoid `break` in problematic test.
* Add noCancelWait() call which prohibits cancellation.
Fix closeWait() calls to use noCancelWait() predicate.
Adding sleep to flaky MacOS test.
* Remove all debugging echoes.
* Fix cancelAndWait() which now could perform multiple attempts to cancel target Future (mustCancel behavior).
* Fix issues revealed by switch to different cancelAndWait().
* Address review comments.
* Fix testutils compilation warning.
* Rename callTick() to internalCallTick().
* Add some documentation comments.
* Disable flaky ratelimit test.
* Rename noCancelWait() to noCancel().
Address review comments.
* Unpack `finally/defer` blocks and introduce explicit cleaning of objects.
Add request query to debug information.
* Unroll one more loop to avoid `break`.
Add test for query debug string.
* Fix cancellation behavior.
* Address review comments.