Actually, if it helps anyone, even this fails in the same way: 
    
    
    import asynchttpserver, asyncdispatch, asyncnet, os, strutils, posix
    
    var fdLim: RLimit
    discard getrlimit(RLIMIT_NOFILE, fdLim)
    fdLim.rlim_cur = 5 # fdLim.rlim_max
    echo "max-max fd = ", fdLim.rlim_max, " soft = ", fdLim.rlim_cur
    discard setrlimit(RLIMIT_NOFILE, fdLim)
    
    proc fileServer(req: Request, staticDir="") {.async.} =
      var url_path = if req.url.path.len > 1 and req.url.path[0] == '/': 
req.url.pat
      var path = staticDir / url_path
      if dirExists(path): path = path / "index.html"
      await req.client.send(
        "HTTP/1.1 200\c\Lcontent-type: text/html\c\Lcontent-length: $1\c\L\c\L" 
% [
          "18" ])
      await req.client.send("that's all, folks\n")
    
    var server = newAsyncHttpServer()
    proc cb(req: Request) {.async.} =
      try:
        await req.fileServer("static")
      except:
        echo "Happen a Error!"
        await req.respond(Http200, "Happen a Error!")
    
    waitFor server.serve(Port(8080), cb)
    
    
    Run

and then 
    
    
    terminal1$ ./bug4
    terminal2$ curl http://localhost:8080/
    terminal1 output:
      max-max fd = 4096 soft = 5
    <waiting; then after curl immediate program exit with:>
      bug4.nim(26)     bug4
      /usr/lib/nim/lib/pure/asyncdispatch.nim(1934) waitFor
      /usr/lib/nim/lib/pure/asyncdispatch.nim(1626) poll
      /usr/lib/nim/lib/pure/asyncdispatch.nim(1367) runOnce
      /usr/lib/nim/lib/pure/asyncdispatch.nim(208) processPendingCallbacks
      /usr/lib/nim/lib/pure/asyncmacro.nim(22) serveNimAsyncContinue
      /usr/lib/nim/lib/pure/asyncmacro.nim(139) serveIter
      /usr/lib/nim/lib/pure/asyncfutures.nim(372) read
      [[reraised from:
      bug4.nim(26)     bug4
      /usr/lib/nim/lib/pure/asyncdispatch.nim(1936) waitFor
      /usr/lib/nim/lib/pure/asyncfutures.nim(372) read
      ]]
      Error: unhandled exception: Too many open files
      Async traceback:
        bug4.nim(26)                          bug4
        /usr/lib/nim/lib/pure/asyncdispatch.nim(1934) waitFor
        /usr/lib/nim/lib/pure/asyncdispatch.nim(1626) poll
        /usr/lib/nim/lib/pure/asyncdispatch.nim(1367) runOnce
        /usr/lib/nim/lib/pure/asyncdispatch.nim(208)  processPendingCallbacks
        /usr/lib/nim/lib/pure/asyncmacro.nim(22)      serveNimAsyncContinue
        /usr/lib/nim/lib/pure/asyncmacro.nim(139)     serveIter
        /usr/lib/nim/lib/pure/asyncfutures.nim(372)   read
      Exception message: Too many open files
      Exception type: [OSError]
    
    
    Run

and it works (for 1 parallel connection) if you raise the fd limit to 6.

This makes me wonder if _other_ OS errors than `MFILE` also get mis-handled...

Reply via email to