Finally solved, I had to disable the `--threads:on` switch and mark procs with 
`gcsafe` and it works with `--mm:orc` and without. It's single threaded but I 
for my needs it's enough.

Just wonder why compiler that proc is GC safe, and requires explicit proc 
annotation with `.gcsafe.`.

The working code, works with both `nim r play.nim` and `nim r --mm:orc play.nim`
    
    
    import std/[httpcore, asynchttpserver, asyncnet, tables, asyncdispatch, 
locks]
    
    type Session* = ref object
      id*:    string
      count*: int
    
    var sessions: Table[string, Session]
    
    proc http_handler(req: Request): Future[void] {.async, gcsafe.} =
      let id = "1"
      if id notin sessions:
        sessions[id] = Session(id: id)
      let session = sessions[id]
      await respond(req, Http200, $session.count)
    
    proc background_processing(_: AsyncFD): bool {.gcsafe.} =
      for _, session in sessions:
        session.count += 1
    
    var server = new_async_http_server()
    async_check serve(server, Port(5000), http_handler, "localhost")
    add_timer(1000, false, background_processing)
    run_forever()
    
    
    Run

Reply via email to