So I've got some code like this:

import asyncDispatch
import asynchttpserver

# Create the server object and state data
  server = newAsyncHttpServer()
let port = 8000

# ...

proc handleRequest(req: Request) {.async.}=
  ## Handle an incomming connection
  var killServer = false
  # ...
  if killServer:
  # Write a response
  var headers = newHttpHeaders()
  headers.add("Content-Type", "text/html")
  await req.respond(Http200, mkPage(pageViews, killServer), headers)

# Handle requests
echo("Listening for connections on http://localhost:" & $port & "/")
waitFor server.serve(Port(port), handleRequest)

If I try to call server.close() I keep on getting this error:

Server.nim(74, 15) Error: type mismatch: got (AsyncHttpServer, Port, proc (req: Request): Future[system.void]{.locks: <unknown>.})
but expected one of:
proc serve(server: AsyncHttpServer; port: Port;
          callback: proc (request: Request): Future[void]; address = ""): Future[void]

I've tried creating a wrapper async function around the close call, that didn't work. I tried using an event callback, that didn't work either. Both other produced the same compilation errors. What's going on here?

2017-09-05 04:04:26
Try to add gcsafe pragma to your proc 2017-09-05 04:14:34
It now compiles successfully, but it doesn't seem to be killing the server when I trip that killServer block. It will run over the line and continue on with execution.
2017-09-06 00:10:29

try returning after server.close

Edit: also this is this bug

Edit2: do you set killServer = true in your code?

2017-09-06 16:20:09

Yeah, I have the killServer = true in a block somewhere.

Adding return after server.close() does work, but I need to mark server with {.threadsafe.}. It also causes an issue with the HTTP Request (on the browser side) doesn't get finished and just hangs. Interestingly enough this doesn't actually stop the application then and there. It's if I try to send a request after killServer = true that it will end the program with this output:

Traceback (most recent call last)
Server.nim(76)           Server
asyncdispatch.nim(1010)  waitFor
asyncdispatch.nim(1013)  poll
Error: unhandled exception: No handles or timers registered in dispatcher. [ValueError]

2017-09-09 03:25:31