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
|
|
|
#
|
|
|
|
#
|
|
|
|
# Nim's Runtime Library
|
|
|
|
# (c) Copyright 2016 Eugene Kabanov
|
|
|
|
#
|
|
|
|
# See the file "copying.txt", included in this
|
|
|
|
# distribution, for details about the copyright.
|
|
|
|
#
|
|
|
|
|
|
|
|
# This module implements Posix poll().
|
2023-03-24 15:52:55 +00:00
|
|
|
import std/tables
|
|
|
|
import stew/base10
|
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-06-05 20:21:50 +00:00
|
|
|
{.push raises: [].}
|
2023-03-24 15:52:55 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
SelectorImpl[T] = object
|
|
|
|
fds: Table[int32, SelectorKey[T]]
|
2023-08-01 09:56:08 +00:00
|
|
|
pollfds: seq[TPollfd]
|
2023-03-24 15:52:55 +00:00
|
|
|
Selector*[T] = ref SelectorImpl[T]
|
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
|
|
|
|
|
|
|
type
|
|
|
|
SelectEventImpl = object
|
|
|
|
rfd: cint
|
|
|
|
wfd: cint
|
|
|
|
SelectEvent* = ptr SelectEventImpl
|
|
|
|
|
2023-03-24 15:52:55 +00:00
|
|
|
proc toString(key: int32): string =
|
|
|
|
Base10.toString(uint32(key))
|
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-24 15:52:55 +00:00
|
|
|
template addKey[T](s: Selector[T], key: int32, skey: SelectorKey[T]) =
|
|
|
|
if s.fds.hasKeyOrPut(key, skey):
|
|
|
|
raiseAssert "Descriptor [" & key.toString() &
|
|
|
|
"] is already registered in the selector!"
|
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-24 15:52:55 +00:00
|
|
|
template getKey[T](s: Selector[T], key: int32): SelectorKey[T] =
|
|
|
|
let
|
|
|
|
defaultKey = SelectorKey[T](ident: InvalidIdent)
|
|
|
|
pkey = s.fds.getOrDefault(key, defaultKey)
|
|
|
|
doAssert(pkey.ident != InvalidIdent,
|
|
|
|
"Descriptor [" & key.toString() &
|
|
|
|
"] is not registered in the selector!")
|
|
|
|
pkey
|
|
|
|
|
|
|
|
template checkKey[T](s: Selector[T], key: int32): bool =
|
|
|
|
s.fds.contains(key)
|
|
|
|
|
|
|
|
proc freeKey[T](s: Selector[T], key: int32) =
|
|
|
|
s.fds.del(key)
|
|
|
|
|
|
|
|
proc new*(t: typedesc[Selector], T: typedesc): SelectResult[Selector[T]] =
|
|
|
|
let selector = Selector[T](
|
2023-08-01 09:56:08 +00:00
|
|
|
fds: initTable[int32, SelectorKey[T]](chronosInitialSize)
|
2023-03-24 15:52:55 +00:00
|
|
|
)
|
|
|
|
ok(selector)
|
|
|
|
|
|
|
|
proc close2*[T](s: Selector[T]): SelectResult[void] =
|
|
|
|
s.fds.clear()
|
|
|
|
s.pollfds.clear()
|
|
|
|
|
|
|
|
proc new*(t: typedesc[SelectEvent]): SelectResult[SelectEvent] =
|
|
|
|
let flags = {DescriptorFlag.NonBlock, DescriptorFlag.CloseOnExec}
|
|
|
|
let pipes = ? createOsPipe(flags, flags)
|
|
|
|
var res = cast[SelectEvent](allocShared0(sizeof(SelectEventImpl)))
|
|
|
|
res.rfd = pipes.read
|
|
|
|
res.wfd = pipes.write
|
|
|
|
ok(res)
|
|
|
|
|
|
|
|
proc trigger2*(event: SelectEvent): SelectResult[void] =
|
|
|
|
var data: uint64 = 1
|
|
|
|
let res = handleEintr(osdefs.write(event.wfd, addr data, sizeof(uint64)))
|
|
|
|
if res == -1:
|
|
|
|
err(osLastError())
|
|
|
|
elif res != sizeof(uint64):
|
2023-08-01 09:56:08 +00:00
|
|
|
err(osdefs.EINVAL)
|
2023-03-24 15:52:55 +00:00
|
|
|
else:
|
|
|
|
ok()
|
|
|
|
|
|
|
|
proc close2*(event: SelectEvent): SelectResult[void] =
|
|
|
|
let
|
|
|
|
rfd = event.rfd
|
|
|
|
wfd = event.wfd
|
|
|
|
deallocShared(cast[pointer](event))
|
|
|
|
let rres = handleEintr(osdefs.close(rfd))
|
|
|
|
if rres == -1:
|
|
|
|
discard osdefs.close(wfd)
|
|
|
|
return err(osLastError())
|
|
|
|
let wres = handleEintr(osdefs.close(wfd))
|
|
|
|
if wres == -1:
|
|
|
|
err(osLastError())
|
|
|
|
else:
|
|
|
|
ok()
|
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-24 15:52:55 +00:00
|
|
|
template toPollEvents(events: set[Event]): cshort =
|
|
|
|
var res = cshort(0)
|
|
|
|
if Event.Read in events: res = res or POLLIN
|
|
|
|
if Event.Write in events: res = res or POLLOUT
|
|
|
|
res
|
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
|
|
|
|
|
|
|
template pollAdd[T](s: Selector[T], sock: cint, events: set[Event]) =
|
2023-08-01 09:56:08 +00:00
|
|
|
s.pollfds.add(TPollfd(fd: sock, events: toPollEvents(events), revents: 0))
|
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
|
|
|
|
|
|
|
template pollUpdate[T](s: Selector[T], sock: cint, events: set[Event]) =
|
2023-03-24 15:52:55 +00:00
|
|
|
var updated = false
|
|
|
|
for mitem in s.pollfds.mitems():
|
|
|
|
if mitem.fd == sock:
|
|
|
|
mitem.events = toPollEvents(events)
|
2023-08-01 09:56:08 +00:00
|
|
|
updated = true
|
2023-03-24 15:52:55 +00:00
|
|
|
break
|
|
|
|
if not(updated):
|
|
|
|
raiseAssert "Descriptor [" & $sock & "] is not registered in the queue!"
|
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
|
|
|
|
|
|
|
template pollRemove[T](s: Selector[T], sock: cint) =
|
2023-03-24 15:52:55 +00:00
|
|
|
let index =
|
|
|
|
block:
|
|
|
|
var res = -1
|
|
|
|
for key, item in s.pollfds.pairs():
|
|
|
|
if item.fd == sock:
|
|
|
|
res = key
|
|
|
|
break
|
|
|
|
res
|
|
|
|
if index < 0:
|
|
|
|
raiseAssert "Descriptor [" & $sock & "] is not registered in the queue!"
|
|
|
|
else:
|
|
|
|
s.pollfds.del(index)
|
|
|
|
|
|
|
|
proc registerHandle2*[T](s: Selector[T], fd: cint, events: set[Event],
|
|
|
|
data: T): SelectResult[void] =
|
|
|
|
let skey = SelectorKey[T](ident: fd, events: events, param: 0, data: data)
|
|
|
|
|
|
|
|
s.addKey(fd, skey)
|
|
|
|
if events != {}:
|
|
|
|
s.pollAdd(fd, events)
|
|
|
|
ok()
|
|
|
|
|
|
|
|
proc updateHandle2*[T](s: Selector[T], fd: cint,
|
|
|
|
events: set[Event]): SelectResult[void] =
|
|
|
|
const EventsMask = {Event.Timer, Event.Signal, Event.Process, Event.Vnode,
|
|
|
|
Event.User, Event.Oneshot, Event.Error}
|
|
|
|
s.fds.withValue(int32(fd), pkey):
|
|
|
|
doAssert(pkey[].events * EventsMask == {},
|
|
|
|
"Descriptor [" & fd.toString() & "] could not be updated!")
|
|
|
|
if pkey[].events != events:
|
|
|
|
if pkey[].events == {}:
|
|
|
|
s.pollAdd(fd, events)
|
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
|
|
|
else:
|
2023-03-24 15:52:55 +00:00
|
|
|
if events != {}:
|
|
|
|
s.pollUpdate(fd, events)
|
|
|
|
else:
|
|
|
|
s.pollRemove(fd)
|
|
|
|
pkey.events = events
|
|
|
|
do:
|
|
|
|
raiseAssert "Descriptor [" & fd.toString() &
|
|
|
|
"] is not registered in the selector!"
|
|
|
|
ok()
|
|
|
|
|
|
|
|
proc registerEvent2*[T](s: Selector[T], ev: SelectEvent,
|
|
|
|
data: T): SelectResult[cint] =
|
|
|
|
doAssert(not(isNil(ev)))
|
|
|
|
let
|
|
|
|
key = SelectorKey[T](ident: ev.rfd, events: {Event.User},
|
|
|
|
param: 0, data: data)
|
|
|
|
|
|
|
|
s.addKey(ev.rfd, key)
|
|
|
|
s.pollAdd(ev.rfd, {Event.Read}.toPollEvents())
|
|
|
|
ok(ev.rfd)
|
|
|
|
|
|
|
|
proc unregister2*[T](s: Selector[T], fd: cint): SelectResult[void] =
|
|
|
|
let pkey = s.getKey(fd)
|
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
|
|
|
if pkey.events != {}:
|
2023-03-24 15:52:55 +00:00
|
|
|
if {Event.Read, Event.Write, Event.User} * pkey.events != {}:
|
|
|
|
s.pollRemove(fd)
|
|
|
|
s.freeKey(fd)
|
|
|
|
ok()
|
|
|
|
|
|
|
|
proc unregister2*[T](s: Selector[T], event: SelectEvent): SelectResult[void] =
|
|
|
|
s.unregister2(event.rfd)
|
|
|
|
|
|
|
|
proc prepareKey[T](s: Selector[T], event: var TPollfd): Opt[ReadyKey] =
|
|
|
|
let
|
|
|
|
fdi32 = int32(event.fd)
|
|
|
|
revents = event.revents
|
|
|
|
|
|
|
|
var
|
|
|
|
pkey = s.getKey(fdi32)
|
|
|
|
rkey = ReadyKey(fd: event.fd)
|
|
|
|
|
|
|
|
# Cleanup all the received events.
|
|
|
|
event.revents = 0
|
|
|
|
|
|
|
|
if (revents and POLLIN) != 0:
|
|
|
|
if Event.User in pkey.events:
|
|
|
|
var data: uint64 = 0
|
|
|
|
let res = handleEintr(osdefs.read(event.fd, addr data, sizeof(uint64)))
|
|
|
|
if res != sizeof(uint64):
|
|
|
|
let errorCode = osLastError()
|
|
|
|
if errorCode == EAGAIN:
|
|
|
|
return Opt.none(ReadyKey)
|
|
|
|
else:
|
|
|
|
rkey.events.incl({Event.User, Event.Error})
|
|
|
|
rkey.errorCode = errorCode
|
|
|
|
else:
|
|
|
|
rkey.events.incl(Event.User)
|
|
|
|
else:
|
|
|
|
rkey.events.incl(Event.Read)
|
|
|
|
|
|
|
|
if (revents and POLLOUT) != 0:
|
|
|
|
rkey.events.incl(Event.Write)
|
|
|
|
|
|
|
|
if (revents and POLLERR) != 0 or (revents and POLLHUP) != 0 or
|
|
|
|
(revents and POLLNVAL) != 0:
|
|
|
|
rkey.events.incl(Event.Error)
|
|
|
|
|
|
|
|
ok(rkey)
|
|
|
|
|
|
|
|
proc selectInto2*[T](s: Selector[T], timeout: int,
|
|
|
|
readyKeys: var openArray[ReadyKey]): SelectResult[int] =
|
|
|
|
var k = 0
|
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-24 15:52:55 +00:00
|
|
|
verifySelectParams(timeout, -1, int(high(cint)))
|
|
|
|
|
|
|
|
let
|
|
|
|
maxEventsCount = min(len(s.pollfds), len(readyKeys))
|
|
|
|
eventsCount =
|
|
|
|
if maxEventsCount > 0:
|
|
|
|
let res = handleEintr(poll(addr(s.pollfds[0]), Tnfds(maxEventsCount),
|
2023-08-01 09:56:08 +00:00
|
|
|
cint(timeout)))
|
2023-03-24 15:52:55 +00:00
|
|
|
if res < 0:
|
|
|
|
return err(osLastError())
|
|
|
|
res
|
|
|
|
else:
|
|
|
|
0
|
|
|
|
|
|
|
|
for i in 0 ..< len(s.pollfds):
|
|
|
|
if s.pollfds[i].revents != 0:
|
|
|
|
let rkey = s.prepareKey(s.pollfds[i]).valueOr: continue
|
|
|
|
readyKeys[k] = rkey
|
|
|
|
inc(k)
|
|
|
|
if k == eventsCount: break
|
|
|
|
|
|
|
|
ok(k)
|
|
|
|
|
|
|
|
proc select2*[T](s: Selector[T], timeout: int): SelectResult[seq[ReadyKey]] =
|
2023-08-01 09:56:08 +00:00
|
|
|
var res = newSeq[ReadyKey](chronosEventsCount)
|
2023-03-24 15:52:55 +00:00
|
|
|
let count = ? selectInto2(s, timeout, res)
|
|
|
|
res.setLen(count)
|
|
|
|
ok(res)
|
|
|
|
|
|
|
|
proc newSelector*[T](): Selector[T] {.
|
2023-06-05 20:21:50 +00:00
|
|
|
raises: [OSError].} =
|
2023-03-24 15:52:55 +00:00
|
|
|
let res = Selector.new(T)
|
|
|
|
if res.isErr(): raiseOSError(res.error)
|
|
|
|
res.get()
|
|
|
|
|
|
|
|
proc close*[T](s: Selector[T]) {.
|
2023-06-05 20:21:50 +00:00
|
|
|
raises: [IOSelectorsException].} =
|
2023-03-24 15:52:55 +00:00
|
|
|
let res = s.close2()
|
|
|
|
if res.isErr(): raiseIOSelectorsError(res.error())
|
|
|
|
|
|
|
|
proc newSelectEvent*(): SelectEvent {.
|
2023-06-05 20:21:50 +00:00
|
|
|
raises: [IOSelectorsException].} =
|
2023-03-24 15:52:55 +00:00
|
|
|
let res = SelectEvent.new()
|
|
|
|
if res.isErr(): raiseIOSelectorsError(res.error())
|
|
|
|
res.get()
|
|
|
|
|
|
|
|
proc trigger*(event: SelectEvent) {.
|
2023-06-05 20:21:50 +00:00
|
|
|
raises: [IOSelectorsException].} =
|
2023-03-24 15:52:55 +00:00
|
|
|
let res = event.trigger2()
|
|
|
|
if res.isErr(): raiseIOSelectorsError(res.error())
|
|
|
|
|
|
|
|
proc close*(event: SelectEvent) {.
|
2023-06-05 20:21:50 +00:00
|
|
|
raises: [IOSelectorsException].} =
|
2023-03-24 15:52:55 +00:00
|
|
|
let res = event.close2()
|
|
|
|
if res.isErr(): raiseIOSelectorsError(res.error())
|
|
|
|
|
|
|
|
proc registerHandle*[T](s: Selector[T], fd: cint | SocketHandle,
|
|
|
|
events: set[Event], data: T) {.
|
2023-06-05 20:21:50 +00:00
|
|
|
raises: [IOSelectorsException].} =
|
2023-03-24 15:52:55 +00:00
|
|
|
let res = registerHandle2(s, cint(fd), events, data)
|
|
|
|
if res.isErr(): raiseIOSelectorsError(res.error())
|
|
|
|
|
|
|
|
proc updateHandle*[T](s: Selector[T], fd: cint | SocketHandle,
|
|
|
|
events: set[Event]) {.
|
2023-06-05 20:21:50 +00:00
|
|
|
raises: [IOSelectorsException].} =
|
2023-03-24 15:52:55 +00:00
|
|
|
let res = updateHandle2(s, cint(fd), events)
|
|
|
|
if res.isErr(): raiseIOSelectorsError(res.error())
|
|
|
|
|
|
|
|
proc unregister*[T](s: Selector[T], fd: cint | SocketHandle) {.
|
2023-06-05 20:21:50 +00:00
|
|
|
raises: [IOSelectorsException].} =
|
2023-03-24 15:52:55 +00:00
|
|
|
let res = unregister2(s, cint(fd))
|
|
|
|
if res.isErr(): raiseIOSelectorsError(res.error())
|
|
|
|
|
|
|
|
proc unregister*[T](s: Selector[T], event: SelectEvent) {.
|
2023-06-05 20:21:50 +00:00
|
|
|
raises: [IOSelectorsException].} =
|
2023-03-24 15:52:55 +00:00
|
|
|
let res = unregister2(s, event)
|
|
|
|
if res.isErr(): raiseIOSelectorsError(res.error())
|
|
|
|
|
|
|
|
proc registerEvent*[T](s: Selector[T], event: SelectEvent,
|
|
|
|
data: T) {.
|
2023-06-05 20:21:50 +00:00
|
|
|
raises: [IOSelectorsException].} =
|
2023-03-24 15:52:55 +00:00
|
|
|
let res = registerEvent2(s, event, data)
|
|
|
|
if res.isErr(): raiseIOSelectorsError(res.error())
|
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
|
|
|
|
|
|
|
proc selectInto*[T](s: Selector[T], timeout: int,
|
2023-03-24 15:52:55 +00:00
|
|
|
readyKeys: var openArray[ReadyKey]): int {.
|
2023-06-05 20:21:50 +00:00
|
|
|
raises: [IOSelectorsException].} =
|
2023-03-24 15:52:55 +00:00
|
|
|
let res = selectInto2(s, timeout, readyKeys)
|
|
|
|
if res.isErr(): raiseIOSelectorsError(res.error())
|
|
|
|
res.get()
|
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
|
|
|
|
|
|
|
proc select*[T](s: Selector[T], timeout: int): seq[ReadyKey] =
|
2023-03-24 15:52:55 +00:00
|
|
|
let res = select2(s, timeout)
|
|
|
|
if res.isErr(): raiseIOSelectorsError(res.error())
|
|
|
|
res.get()
|
|
|
|
|
|
|
|
proc contains*[T](s: Selector[T], fd: SocketHandle|cint): bool {.inline.} =
|
|
|
|
s.checkKey(int32(fd))
|
|
|
|
|
|
|
|
proc setData*[T](s: Selector[T], fd: SocketHandle|cint, data: T): bool =
|
|
|
|
s.fds.withValue(int32(fd), skey):
|
|
|
|
skey[].data = data
|
|
|
|
return true
|
|
|
|
do:
|
|
|
|
return false
|
|
|
|
|
|
|
|
template withData*[T](s: Selector[T], fd: SocketHandle|cint, value,
|
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
|
|
|
body: untyped) =
|
2023-03-24 15:52:55 +00:00
|
|
|
s.fds.withValue(int32(fd), skey):
|
|
|
|
var value = addr(skey[].data)
|
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
|
|
|
body
|
|
|
|
|
2023-03-24 15:52:55 +00:00
|
|
|
template withData*[T](s: Selector[T], fd: SocketHandle|cint, value, body1,
|
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
|
|
|
body2: untyped) =
|
2023-03-24 15:52:55 +00:00
|
|
|
s.fds.withValue(int32(fd), skey):
|
|
|
|
var value = addr(skey[].data)
|
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
|
|
|
body1
|
2023-03-24 15:52:55 +00:00
|
|
|
do:
|
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
|
|
|
body2
|
|
|
|
|
2023-03-24 15:52:55 +00:00
|
|
|
proc getFd*[T](s: Selector[T]): int = -1
|