I'm trying to use a lot of asynchronous sockets on Windows and I'm having several problems and ultimately most sockets don't work.
With less than 1000 asynchronous sockets there is not error, from 1000 to 5000 asynchronous sockets i get a OSError with error code 10055. This error is raised in the asynchronous tasks in the socket.connect method, so it's possible to catch the exception, i can wait and i can try to restart the connection.
When i have more than 5000 asynchronous sockets i get a OSError with error codes 10038 and 0 and i get it on get it on waitFor or await all function... So theses errors break all asynchronous sockets.
Do you know the problem and a workaround to use lot of asynchronous sockets on Windows ? The same code works fine on Linux.
I write the full code here: https://github.com/mauricelambert/AsyncPortScanner/blob/main/AsyncPortScanner.nim
I try to write a minimal code to reproduce this error:
import asyncnet, asyncdispatch, net, strutils, system, selectors, typetraits, os, std/times
proc test_port(ip: string, port: int): Future[void] {.async.} =
var state = false
var socket: AsyncSocket
try:
socket = newAsyncSocket(AF_INET, SOCK_STREAM)
except IOSelectorsException, OSError:
await sleepAsync(1000)
await test_port(ip, port)
return
try:
state = await withTimeout(socket.connect(ip, Port(port)), 1000)
except OSError as e:
if e.errorCode == 10055:
await sleepAsync(1000)
await test_port(ip, port)
return
raise e
finally:
try:
socket.close()
except OSError:
discard
proc scan(futures: seq[Future[void]]): Future[void] {.async.} =
await all(futures)
proc start_scan() =
var futures = newSeq[Future[void]]()
for port in 0..65535:
for ip in ["192.168.56.1", "192.168.56.2", "192.168.56.3", "192.168.56.4", "192.168.56.5"]:
futures.add(test_port(ip, port))
while true:
try:
waitFor scan(futures)
break
except OSError as e:
if e.errorCode != 10038 and e.errorCode != 0:
echo "There is an OS error... ", $e.errorCode
raise e
sleep(1000)
start_scan()