2018-05-16 08:22:34 +00:00
|
|
|
#
|
2019-02-06 14:49:11 +00:00
|
|
|
# Chronos Handles
|
|
|
|
# (c) Copyright 2018-Present
|
2018-05-16 08:22:34 +00:00
|
|
|
# Status Research & Development GmbH
|
|
|
|
#
|
|
|
|
# Licensed under either of
|
|
|
|
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
|
|
|
# MIT license (LICENSE-MIT)
|
|
|
|
|
2023-06-05 20:21:50 +00:00
|
|
|
{.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-02-21 10:48:36 +00:00
|
|
|
import "."/[asyncloop, osdefs, osutils]
|
|
|
|
import stew/results
|
|
|
|
from nativesockets import Domain, Protocol, SockType, toInt
|
2023-06-03 16:36:06 +00:00
|
|
|
export Domain, Protocol, SockType, results
|
|
|
|
|
|
|
|
when defined(windows):
|
|
|
|
export raiseSignal, raiseConsoleCtrlSignal
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2019-07-15 09:59:42 +00:00
|
|
|
const
|
2023-02-21 10:48:36 +00:00
|
|
|
asyncInvalidSocket* = AsyncFD(osdefs.INVALID_SOCKET)
|
2019-07-15 09:59:42 +00:00
|
|
|
asyncInvalidPipe* = asyncInvalidSocket
|
|
|
|
|
2023-10-30 13:27:50 +00:00
|
|
|
proc setSocketBlocking*(s: SocketHandle, blocking: bool): bool {.
|
|
|
|
deprecated: "Please use setDescriptorBlocking() instead".} =
|
2018-05-16 08:22:34 +00:00
|
|
|
## Sets blocking mode on socket.
|
2023-10-30 13:27:50 +00:00
|
|
|
setDescriptorBlocking(s, blocking).isOkOr:
|
|
|
|
return false
|
|
|
|
true
|
|
|
|
|
|
|
|
proc setSockOpt2*(socket: AsyncFD,
|
|
|
|
level, optname, optval: int): Result[void, OSErrorCode] =
|
|
|
|
var value = cint(optval)
|
|
|
|
let res = osdefs.setsockopt(SocketHandle(socket), cint(level), cint(optname),
|
|
|
|
addr(value), SockLen(sizeof(value)))
|
|
|
|
if res == -1:
|
|
|
|
return err(osLastError())
|
|
|
|
ok()
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2023-10-30 13:27:50 +00:00
|
|
|
proc setSockOpt2*(socket: AsyncFD, level, optname: int, value: pointer,
|
|
|
|
valuelen: int): Result[void, OSErrorCode] =
|
|
|
|
## `setsockopt()` for custom options (pointer and length).
|
|
|
|
## Returns ``true`` on success, ``false`` on error.
|
|
|
|
let res = osdefs.setsockopt(SocketHandle(socket), cint(level), cint(optname),
|
|
|
|
value, SockLen(valuelen))
|
|
|
|
if res == -1:
|
|
|
|
return err(osLastError())
|
|
|
|
ok()
|
|
|
|
|
|
|
|
proc setSockOpt*(socket: AsyncFD, level, optname, optval: int): bool {.
|
|
|
|
deprecated: "Please use setSockOpt2() instead".} =
|
2018-05-16 08:22:34 +00:00
|
|
|
## `setsockopt()` for integer options.
|
|
|
|
## Returns ``true`` on success, ``false`` on error.
|
2023-10-30 13:27:50 +00:00
|
|
|
setSockOpt2(socket, level, optname, optval).isOk
|
2018-05-29 09:59:39 +00:00
|
|
|
|
2019-05-07 20:11:40 +00:00
|
|
|
proc setSockOpt*(socket: AsyncFD, level, optname: int, value: pointer,
|
2023-10-30 13:27:50 +00:00
|
|
|
valuelen: int): bool {.
|
|
|
|
deprecated: "Please use setSockOpt2() instead".} =
|
2019-05-07 20:11:40 +00:00
|
|
|
## `setsockopt()` for custom options (pointer and length).
|
|
|
|
## Returns ``true`` on success, ``false`` on error.
|
2023-10-30 13:27:50 +00:00
|
|
|
setSockOpt2(socket, level, optname, value, valuelen).isOk
|
|
|
|
|
|
|
|
proc getSockOpt2*(socket: AsyncFD,
|
|
|
|
level, optname: int): Result[cint, OSErrorCode] =
|
|
|
|
var
|
|
|
|
value: cint
|
|
|
|
size = SockLen(sizeof(value))
|
|
|
|
let res = osdefs.getsockopt(SocketHandle(socket), cint(level), cint(optname),
|
|
|
|
addr(value), addr(size))
|
|
|
|
if res == -1:
|
|
|
|
return err(osLastError())
|
|
|
|
ok(value)
|
|
|
|
|
|
|
|
proc getSockOpt2*(socket: AsyncFD, level, optname: int,
|
|
|
|
T: type): Result[T, OSErrorCode] =
|
|
|
|
var
|
|
|
|
value = default(T)
|
|
|
|
size = SockLen(sizeof(value))
|
|
|
|
let res = osdefs.getsockopt(SocketHandle(socket), cint(level), cint(optname),
|
|
|
|
cast[ptr byte](addr(value)), addr(size))
|
|
|
|
if res == -1:
|
|
|
|
return err(osLastError())
|
|
|
|
ok(value)
|
2019-05-07 20:11:40 +00:00
|
|
|
|
2023-10-30 13:27:50 +00:00
|
|
|
proc getSockOpt*(socket: AsyncFD, level, optname: int, value: var int): bool {.
|
|
|
|
deprecated: "Please use getSockOpt2() instead".} =
|
2018-05-16 08:22:34 +00:00
|
|
|
## `getsockopt()` for integer options.
|
2019-05-07 20:11:40 +00:00
|
|
|
## Returns ``true`` on success, ``false`` on error.
|
2023-10-30 13:27:50 +00:00
|
|
|
value = getSockOpt2(socket, level, optname).valueOr:
|
|
|
|
return false
|
|
|
|
true
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2023-10-30 13:27:50 +00:00
|
|
|
proc getSockOpt*(socket: AsyncFD, level, optname: int, value: var pointer,
|
|
|
|
valuelen: var int): bool {.
|
|
|
|
deprecated: "Please use getSockOpt2() instead".} =
|
2019-05-07 20:11:40 +00:00
|
|
|
## `getsockopt()` for custom options (pointer and length).
|
|
|
|
## Returns ``true`` on success, ``false`` on error.
|
2023-02-21 10:48:36 +00:00
|
|
|
osdefs.getsockopt(SocketHandle(socket), cint(level), cint(optname),
|
|
|
|
value, cast[ptr SockLen](addr valuelen)) >= cint(0)
|
2019-05-07 20:11:40 +00:00
|
|
|
|
2023-10-30 13:27:50 +00:00
|
|
|
proc getSocketError*(socket: AsyncFD, err: var int): bool {.
|
|
|
|
deprecated: "Please use getSocketError() instead".} =
|
2018-05-21 21:52:57 +00:00
|
|
|
## Recover error code associated with socket handle ``socket``.
|
2023-10-30 13:27:50 +00:00
|
|
|
err = getSockOpt2(socket, cint(osdefs.SOL_SOCKET),
|
|
|
|
cint(osdefs.SO_ERROR)).valueOr:
|
|
|
|
return false
|
|
|
|
true
|
|
|
|
|
|
|
|
proc getSocketError2*(socket: AsyncFD): Result[cint, OSErrorCode] =
|
|
|
|
getSockOpt2(socket, cint(osdefs.SOL_SOCKET), cint(osdefs.SO_ERROR))
|
|
|
|
|
|
|
|
proc isAvailable*(domain: Domain): bool =
|
|
|
|
when defined(windows):
|
|
|
|
let fd = wsaSocket(toInt(domain), toInt(SockType.SOCK_STREAM),
|
|
|
|
toInt(Protocol.IPPROTO_TCP), nil, GROUP(0), 0'u32)
|
|
|
|
if fd == osdefs.INVALID_SOCKET:
|
|
|
|
return if osLastError() == osdefs.WSAEAFNOSUPPORT: false else: true
|
|
|
|
discard closeFd(fd)
|
|
|
|
true
|
|
|
|
else:
|
|
|
|
let fd = osdefs.socket(toInt(domain), toInt(SockType.SOCK_STREAM),
|
|
|
|
toInt(Protocol.IPPROTO_TCP))
|
|
|
|
if fd == -1:
|
|
|
|
return if osLastError() == osdefs.EAFNOSUPPORT: false else: true
|
|
|
|
discard closeFd(fd)
|
|
|
|
true
|
2023-02-21 10:48:36 +00:00
|
|
|
|
|
|
|
proc createAsyncSocket2*(domain: Domain, sockType: SockType,
|
2023-10-30 13:27:50 +00:00
|
|
|
protocol: Protocol,
|
|
|
|
inherit = true): Result[AsyncFD, OSErrorCode] =
|
2023-02-21 10:48:36 +00:00
|
|
|
## Creates new asynchronous socket.
|
|
|
|
when defined(windows):
|
|
|
|
let flags =
|
|
|
|
if inherit:
|
|
|
|
osdefs.WSA_FLAG_OVERLAPPED
|
|
|
|
else:
|
|
|
|
osdefs.WSA_FLAG_OVERLAPPED or osdefs.WSA_FLAG_NO_HANDLE_INHERIT
|
|
|
|
let fd = wsaSocket(toInt(domain), toInt(sockType), toInt(protocol),
|
|
|
|
nil, GROUP(0), flags)
|
|
|
|
if fd == osdefs.INVALID_SOCKET:
|
|
|
|
return err(osLastError())
|
|
|
|
|
2023-10-30 13:27:50 +00:00
|
|
|
setDescriptorBlocking(fd, false).isOkOr:
|
2023-02-21 10:48:36 +00:00
|
|
|
discard closeFd(fd)
|
2023-10-30 13:27:50 +00:00
|
|
|
return err(error)
|
|
|
|
register2(AsyncFD(fd)).isOkOr:
|
2023-02-21 10:48:36 +00:00
|
|
|
discard closeFd(fd)
|
2023-10-30 13:27:50 +00:00
|
|
|
return err(error)
|
2023-02-21 10:48:36 +00:00
|
|
|
|
|
|
|
ok(AsyncFD(fd))
|
|
|
|
else:
|
|
|
|
when declared(SOCK_NONBLOCK) and declared(SOCK_CLOEXEC):
|
|
|
|
let socketType =
|
|
|
|
if inherit:
|
|
|
|
toInt(sockType) or osdefs.SOCK_NONBLOCK
|
|
|
|
else:
|
|
|
|
toInt(sockType) or osdefs.SOCK_NONBLOCK or osdefs.SOCK_CLOEXEC
|
|
|
|
let fd = osdefs.socket(toInt(domain), socketType, toInt(protocol))
|
|
|
|
if fd == -1:
|
|
|
|
return err(osLastError())
|
2023-10-30 13:27:50 +00:00
|
|
|
register2(AsyncFD(fd)).isOkOr:
|
2023-02-21 10:48:36 +00:00
|
|
|
discard closeFd(fd)
|
2023-10-30 13:27:50 +00:00
|
|
|
return err(error)
|
2023-02-21 10:48:36 +00:00
|
|
|
ok(AsyncFD(fd))
|
|
|
|
else:
|
|
|
|
let fd = osdefs.socket(toInt(domain), toInt(sockType), toInt(protocol))
|
|
|
|
if fd == -1:
|
|
|
|
return err(osLastError())
|
2023-10-30 13:27:50 +00:00
|
|
|
setDescriptorFlags(cint(fd), true, true).isOkOr:
|
2023-02-21 10:48:36 +00:00
|
|
|
discard closeFd(fd)
|
2023-10-30 13:27:50 +00:00
|
|
|
return err(error)
|
|
|
|
register2(AsyncFD(fd)).isOkOr:
|
2023-02-21 10:48:36 +00:00
|
|
|
discard closeFd(fd)
|
2023-10-30 13:27:50 +00:00
|
|
|
return err(error)
|
2023-02-21 10:48:36 +00:00
|
|
|
ok(AsyncFD(fd))
|
|
|
|
|
|
|
|
proc wrapAsyncSocket2*(sock: cint|SocketHandle): Result[AsyncFD, OSErrorCode] =
|
|
|
|
## Wraps socket to asynchronous socket handle.
|
|
|
|
let fd =
|
|
|
|
when defined(windows):
|
|
|
|
sock
|
|
|
|
else:
|
|
|
|
when sock is cint: sock else: cint(sock)
|
|
|
|
? setDescriptorFlags(fd, true, true)
|
|
|
|
? register2(AsyncFD(fd))
|
|
|
|
ok(AsyncFD(fd))
|
2018-05-16 08:22:34 +00:00
|
|
|
|
|
|
|
proc createAsyncSocket*(domain: Domain, sockType: SockType,
|
2023-02-21 10:48:36 +00:00
|
|
|
protocol: Protocol,
|
|
|
|
inherit = true): AsyncFD =
|
2018-05-16 08:22:34 +00:00
|
|
|
## Creates new asynchronous socket.
|
|
|
|
## Returns ``asyncInvalidSocket`` on error.
|
2023-02-21 10:48:36 +00:00
|
|
|
createAsyncSocket2(domain, sockType, protocol, inherit).valueOr:
|
2018-05-16 08:22:34 +00:00
|
|
|
return asyncInvalidSocket
|
|
|
|
|
2023-02-21 10:48:36 +00:00
|
|
|
proc wrapAsyncSocket*(sock: cint|SocketHandle): AsyncFD {.
|
2023-06-05 20:21:50 +00:00
|
|
|
raises: [CatchableError].} =
|
2018-05-21 21:52:57 +00:00
|
|
|
## Wraps socket to asynchronous socket handle.
|
2018-05-16 08:22:34 +00:00
|
|
|
## Return ``asyncInvalidSocket`` on error.
|
2023-02-21 10:48:36 +00:00
|
|
|
wrapAsyncSocket2(sock).valueOr:
|
2018-05-16 08:22:34 +00:00
|
|
|
return asyncInvalidSocket
|
2019-07-15 09:59:42 +00:00
|
|
|
|
2023-02-21 10:48:36 +00:00
|
|
|
proc getMaxOpenFiles2*(): Result[int, OSErrorCode] =
|
2020-06-24 08:21:52 +00:00
|
|
|
## Returns maximum file descriptor number that can be opened by this process.
|
|
|
|
##
|
|
|
|
## Note: On Windows its impossible to obtain such number, so getMaxOpenFiles()
|
|
|
|
## will return constant value of 16384. You can get more information on this
|
|
|
|
## link https://docs.microsoft.com/en-us/archive/blogs/markrussinovich/pushing-the-limits-of-windows-handles
|
2022-01-04 17:00:17 +00:00
|
|
|
when defined(windows) or defined(nimdoc):
|
2023-02-21 10:48:36 +00:00
|
|
|
ok(16384)
|
2020-06-24 08:21:52 +00:00
|
|
|
else:
|
|
|
|
var limits: RLimit
|
2023-02-21 10:48:36 +00:00
|
|
|
if osdefs.getrlimit(osdefs.RLIMIT_NOFILE, limits) != 0:
|
|
|
|
return err(osLastError())
|
|
|
|
ok(int(limits.rlim_cur))
|
2020-06-24 08:21:52 +00:00
|
|
|
|
2023-02-21 10:48:36 +00:00
|
|
|
proc setMaxOpenFiles2*(count: int): Result[void, OSErrorCode] =
|
2020-06-24 08:21:52 +00:00
|
|
|
## Set maximum file descriptor number that can be opened by this process.
|
|
|
|
##
|
|
|
|
## Note: On Windows its impossible to set this value, so it just a nop call.
|
2022-01-04 17:00:17 +00:00
|
|
|
when defined(windows) or defined(nimdoc):
|
2023-02-21 10:48:36 +00:00
|
|
|
ok()
|
2020-06-24 08:21:52 +00:00
|
|
|
else:
|
|
|
|
var limits: RLimit
|
2023-02-21 10:48:36 +00:00
|
|
|
if getrlimit(osdefs.RLIMIT_NOFILE, limits) != 0:
|
|
|
|
return err(osLastError())
|
2020-06-24 08:21:52 +00:00
|
|
|
limits.rlim_cur = count
|
2023-02-21 10:48:36 +00:00
|
|
|
if setrlimit(osdefs.RLIMIT_NOFILE, limits) != 0:
|
|
|
|
return err(osLastError())
|
|
|
|
ok()
|
|
|
|
|
2023-06-05 20:21:50 +00:00
|
|
|
proc getMaxOpenFiles*(): int {.raises: [OSError].} =
|
2023-02-21 10:48:36 +00:00
|
|
|
## Returns maximum file descriptor number that can be opened by this process.
|
|
|
|
##
|
|
|
|
## Note: On Windows its impossible to obtain such number, so getMaxOpenFiles()
|
|
|
|
## will return constant value of 16384. You can get more information on this
|
|
|
|
## link https://docs.microsoft.com/en-us/archive/blogs/markrussinovich/pushing-the-limits-of-windows-handles
|
|
|
|
let res = getMaxOpenFiles2()
|
|
|
|
if res.isErr():
|
|
|
|
raiseOSError(res.error())
|
|
|
|
res.get()
|
|
|
|
|
2023-06-05 20:21:50 +00:00
|
|
|
proc setMaxOpenFiles*(count: int) {.raises: [OSError].} =
|
2023-02-21 10:48:36 +00:00
|
|
|
## Set maximum file descriptor number that can be opened by this process.
|
|
|
|
##
|
|
|
|
## Note: On Windows its impossible to set this value, so it just a nop call.
|
|
|
|
let res = setMaxOpenFiles2(count)
|
|
|
|
if res.isErr():
|
|
|
|
raiseOSError(res.error())
|
|
|
|
|
|
|
|
proc getInheritable*(fd: AsyncFD): Result[bool, OSErrorCode] =
|
|
|
|
## Returns ``true`` if ``fd`` is inheritable handle.
|
|
|
|
when defined(windows):
|
|
|
|
var flags = 0'u32
|
|
|
|
if getHandleInformation(HANDLE(fd), flags) == FALSE:
|
|
|
|
return err(osLastError())
|
|
|
|
ok((flags and HANDLE_FLAG_INHERIT) == HANDLE_FLAG_INHERIT)
|
|
|
|
else:
|
|
|
|
let flags = osdefs.fcntl(cint(fd), osdefs.F_GETFD)
|
|
|
|
if flags == -1:
|
|
|
|
return err(osLastError())
|
|
|
|
ok((flags and osdefs.FD_CLOEXEC) == osdefs.FD_CLOEXEC)
|
2020-06-24 08:21:52 +00:00
|
|
|
|
2019-07-15 09:59:42 +00:00
|
|
|
proc createAsyncPipe*(): tuple[read: AsyncFD, write: AsyncFD] =
|
|
|
|
## Create new asynchronouse pipe.
|
2021-05-07 20:52:24 +00:00
|
|
|
## Returns tuple of read pipe handle and write pipe handle``asyncInvalidPipe``
|
|
|
|
## on error.
|
2023-02-21 10:48:36 +00:00
|
|
|
let res = createOsPipe(AsyncDescriptorDefault, AsyncDescriptorDefault)
|
|
|
|
if res.isErr():
|
|
|
|
(read: asyncInvalidPipe, write: asyncInvalidPipe)
|
2019-07-15 09:59:42 +00:00
|
|
|
else:
|
2023-02-21 10:48:36 +00:00
|
|
|
let pipes = res.get()
|
|
|
|
(read: AsyncFD(pipes.read), write: AsyncFD(pipes.write))
|
2023-10-30 13:27:50 +00:00
|
|
|
|
|
|
|
proc getDualstack*(fd: AsyncFD): Result[bool, OSErrorCode] =
|
|
|
|
## Returns `true` if `IPV6_V6ONLY` socket option set to `false`.
|
|
|
|
var
|
|
|
|
flag = cint(0)
|
|
|
|
size = SockLen(sizeof(flag))
|
|
|
|
let res = osdefs.getsockopt(SocketHandle(fd), cint(osdefs.IPPROTO_IPV6),
|
|
|
|
cint(osdefs.IPV6_V6ONLY), addr(flag), addr(size))
|
|
|
|
if res == -1:
|
|
|
|
return err(osLastError())
|
|
|
|
ok(flag == cint(0))
|
|
|
|
|
|
|
|
proc setDualstack*(fd: AsyncFD, value: bool): Result[void, OSErrorCode] =
|
|
|
|
## Sets `IPV6_V6ONLY` socket option value to `false` if `value == true` and
|
|
|
|
## to `true` if `value == false`.
|
|
|
|
var
|
|
|
|
flag = cint(if value: 0 else: 1)
|
|
|
|
size = SockLen(sizeof(flag))
|
|
|
|
let res = osdefs.setsockopt(SocketHandle(fd), cint(osdefs.IPPROTO_IPV6),
|
|
|
|
cint(osdefs.IPV6_V6ONLY), addr(flag), size)
|
|
|
|
if res == -1:
|
|
|
|
return err(osLastError())
|
|
|
|
ok()
|