What else could it yield? Can you describe your use case in a bit more detail?
Perhaps you want to have an accept which times out? You can achieve that with sleepAsync and the or operator for Futures.
It could yield "nothing" if no new client has been registered by the OS prior to the call. I know how sleepAsync can be used, but it might not be granular enough for some people. Either way, don't BSD socket timeouts for example allow for a lower CPU utilization?
These details don't really matter in my case though, so I'm just going to use 1ms timeouts and not worry about optimizations for now.
I did manage to manage to make this assertion (in asyncdispatch.acceptAddr) fail:
assert lastError.int32 notin {EWOULDBLOCK, EAGAIN}
Which is not very helpful for users. I'll try to reproduce it.
import asyncnet, asyncdispatch
var clients {.threadvar.}: seq[AsyncSocket]
proc processClient(client: AsyncSocket) =
echo "DEBUG: process client"
# stuff ..
proc acceptClient(server: AsyncSocket) {.async.} =
let clientFut = server.accept()
await sleepAsync(100) or clientFut
if clientFut.finished:
echo "DEBUG: client accepted"
clients.add clientFut.read
proc processClients(server: AsyncSocket) =
for client in clients:
processClient(client)
proc main =
clients = @[]
var server = newAsyncSocket()
server.bindAddr(12345.Port)
server.listen()
while true:
waitFor acceptClient(server)
processClients(server)
main()
while true:
poll(timeout = 1)
(gdb) break asyncdispatch.nim:1197 Breakpoint 1 at 0x423583: asyncdispatch.nim:1197. (2 locations) (gdb) r Starting program: /home/exe/src/nim/asynctest Breakpoint 1, cb_185814 (sock=3, ClEnv=0x7ffff7f220f0) at /home/exe/src/nim/lib/pure/asyncdispatch.nim:1197 1197 if client == osInvalidSocket: (gdb) bt #0 cb_185814 (sock=3, ClEnv=0x7ffff7f220f0) at /home/exe/src/nim/lib/pure/asyncdispatch.nim:1197 #1 0x0000000000421777 in poll_182920 (timeout=500) at /home/exe/src/nim/lib/pure/asyncdispatch.nim:1032 #2 0x0000000000428cec in waitfor_199064 (fut=0x7ffff7f282b8) at /home/exe/src/nim/lib/pure/asyncdispatch.nim:1622 #3 0x000000000044d0d7 in acceptclient_199021 (server=0x7ffff7f20040) at /home/exe/src/nim/asynctest.nim:11 #4 0x000000000044d5d3 in main_199633 () at /home/exe/src/nim/asynctest.nim:27 #5 0x000000000044d7d1 in asynctestInit000 () at /home/exe/src/nim/asynctest.nim:30 #6 0x000000000044d713 in NimMainInner () at /home/exe/src/nim/lib/system.nim:2640 #7 0x000000000044d747 in NimMain () at /home/exe/src/nim/lib/system.nim:2648 #8 0x000000000044d781 in main (argc=1, args=0x7fffffffe018, env=0x7fffffffe028) at /home/exe/src/nim/lib/system.nim:2655 (gdb) c Continuing. Breakpoint 1, cb_185814 (sock=3, ClEnv=0x7ffff7f220f0) at /home/exe/src/nim/lib/pure/asyncdispatch.nim:1197 1197 if client == osInvalidSocket: (gdb) bt #0 cb_185814 (sock=3, ClEnv=0x7ffff7f220f0) at /home/exe/src/nim/lib/pure/asyncdispatch.nim:1197 #1 0x0000000000421777 in poll_182920 (timeout=500) at /home/exe/src/nim/lib/pure/asyncdispatch.nim:1032 #2 0x0000000000428cec in waitfor_199064 (fut=0x7ffff7f282b8) at /home/exe/src/nim/lib/pure/asyncdispatch.nim:1622 #3 0x000000000044d0d7 in acceptclient_199021 (server=0x7ffff7f20040) at /home/exe/src/nim/asynctest.nim:11 #4 0x000000000044d5d3 in main_199633 () at /home/exe/src/nim/asynctest.nim:27 #5 0x000000000044d7d1 in asynctestInit000 () at /home/exe/src/nim/asynctest.nim:30 #6 0x000000000044d713 in NimMainInner () at /home/exe/src/nim/lib/system.nim:2640 #7 0x000000000044d747 in NimMain () at /home/exe/src/nim/lib/system.nim:2648 #8 0x000000000044d781 in main (argc=1, args=0x7fffffffe018, env=0x7fffffffe028) at /home/exe/src/nim/lib/system.nim:2655 (gdb) c Continuing. Breakpoint 1, cb_185814 (sock=3, ClEnv=0x7ffff7f22140) at /home/exe/src/nim/lib/pure/asyncdispatch.nim:1197 1197 if client == osInvalidSocket: (gdb) bt #0 cb_185814 (sock=3, ClEnv=0x7ffff7f22140) at /home/exe/src/nim/lib/pure/asyncdispatch.nim:1197 #1 0x0000000000421777 in poll_182920 (timeout=500) at /home/exe/src/nim/lib/pure/asyncdispatch.nim:1032 #2 0x0000000000428cec in waitfor_199064 (fut=0x7ffff7f282b8) at /home/exe/src/nim/lib/pure/asyncdispatch.nim:1622 #3 0x000000000044d0d7 in acceptclient_199021 (server=0x7ffff7f20040) at /home/exe/src/nim/asynctest.nim:11 #4 0x000000000044d5d3 in main_199633 () at /home/exe/src/nim/asynctest.nim:27 #5 0x000000000044d7d1 in asynctestInit000 () at /home/exe/src/nim/asynctest.nim:30 #6 0x000000000044d713 in NimMainInner () at /home/exe/src/nim/lib/system.nim:2640 #7 0x000000000044d747 in NimMain () at /home/exe/src/nim/lib/system.nim:2648 #8 0x000000000044d781 in main (argc=1, args=0x7fffffffe018, env=0x7fffffffe028) at /home/exe/src/nim/lib/system.nim:2655 (gdb) c Continuing. Breakpoint 1, cb_185814 (sock=3, ClEnv=0x7ffff7f22140) at /home/exe/src/nim/lib/pure/asyncdispatch.nim:1197 1197 if client == osInvalidSocket: (gdb) bt #0 cb_185814 (sock=3, ClEnv=0x7ffff7f22140) at /home/exe/src/nim/lib/pure/asyncdispatch.nim:1197 #1 0x0000000000421777 in poll_182920 (timeout=500) at /home/exe/src/nim/lib/pure/asyncdispatch.nim:1032 #2 0x0000000000428cec in waitfor_199064 (fut=0x7ffff7f282b8) at /home/exe/src/nim/lib/pure/asyncdispatch.nim:1622 #3 0x000000000044d0d7 in acceptclient_199021 (server=0x7ffff7f20040) at /home/exe/src/nim/asynctest.nim:11 #4 0x000000000044d5d3 in main_199633 () at /home/exe/src/nim/asynctest.nim:27 #5 0x000000000044d7d1 in asynctestInit000 () at /home/exe/src/nim/asynctest.nim:30 #6 0x000000000044d713 in NimMainInner () at /home/exe/src/nim/lib/system.nim:2640 #7 0x000000000044d747 in NimMain () at /home/exe/src/nim/lib/system.nim:2648 #8 0x000000000044d781 in main (argc=1, args=0x7fffffffe018, env=0x7fffffffe028) at /home/exe/src/nim/lib/system.nim:2655 (gdb) c Continuing. Traceback (most recent call last) asynctest.nim(30) asynctest asynctest.nim(27) main asynctest.nim(11) acceptClient asyncdispatch.nim(1622) waitFor asyncdispatch.nim(1032) poll asyncdispatch.nim(1199) cb system.nim(3339) failedAssertImpl system.nim(3331) raiseAssert system.nim(2527) sysFatal Error: unhandled exception: not contains({EWOULDBLOCK, EAGAIN}, int32(lastError)) [AssertionError] [Inferior 1 (process 3081) exited with code 01] (gdb)
The value of client is 0, then > 0 two times and -1 (invalid) after that
EDIT: This is on 64-bit linux