jester icon indicating copy to clipboard operation
jester copied to clipboard

Can't compile with threads enabled using nim-httpauth

Open bitnom opened this issue 6 years ago • 4 comments

I am using nim-httpauth with Jester. If I try to compile with --threads:on, I get the following error:

/home/user/.nimble/pkgs/jester-0.4.3/jester.nim(1277, 9) Hint: Asynchronous route: match. [User]
/home/user/Projects/project/bin/project.nim(19, 1) template/generic instantiation of `routes` from here
/home/user/.nimble/pkgs/jester-0.4.3/jester.nim(1283, 35) template/generic instantiation of `async` from here
/home/user/.nimble/lib/pure/asyncmacro.nim(272, 31) Error: 'matchIter' is not GC-safe as it accesses 'auth' which is a global using GC'ed memory

Using their example code. I'm not sure which module is most responsible. I also posted an issue here: https://github.com/FedericoCeratto/nim-httpauth/issues/22 . I'm also posting it here because I saw that there's a possibly related todo: https://github.com/dom96/jester/issues/110

I'm also wondering your input on how to maximize performance and how necessary a caching web server like nginx is. I see that there are some supplementary modules like beast and whip but they don't look like drop-in replacements or augments for Jester. I do need all the features of Jester and of course best performance possible but I digress.

bitnom avatar Nov 19 '19 02:11 bitnom

Jester uses httpbeast (which is just a fast http server). I recommend nginx in front of it for security/reliability (as httpbeast hasn't been strengthened against all http exploits exhaustively)

If you want to use threads then you need to use a threadvar for globals and initialise them in each thread via a helper accessor:

var myGlobal: AuthState {.threavar.}

proc getMyGlobal(): AuthState =
  if myGlobal.isNil:
    myGlobal = newAuthState() # Initialise for each thread
  return myGlobal

dom96 avatar Nov 19 '19 15:11 dom96

I tried this

import asyncdispatch
import htmlgen
import httpauth
import jester
import json
import parseutils
import nwt


var backend* {.threadvar.}: HTTPAuthBackend
var auth* {.threadvar.}: HTTPAuth
var templates* {.threadvar.}: Nwt

proc init_backend(): HTTPAuthBackend =
    if not defined(backend):
        backend = newSQLBackend("mysql://test@localhost/test")
    return backend

proc init_auth(): HTTPAuth =
    if not defined(auth):
        auth = newHTTPAuth("localhost", backend)
    return auth

proc init_templates(): Nwt =
    if not defined(templates):
        templates = newNwt("templates/*.html")
    return templates


routes:
    get "/":
        backend = init_backend()
        auth = init_auth()
        templates = init_templates()
        var dom:string = templates.renderTemplate("index.html")
        resp dom

runForever()

but now getting

/home/user/.nimble/pkgs/jester-0.4.3/jester.nim(1277, 9) Hint: Asynchronous route: match. [User]
/home/user/Projects/project/bin/project.nim(38, 1) template/generic instantiation of `routes` from here
/home/user/.nimble/pkgs/jester-0.4.3/jester.nim(1283, 35) template/generic instantiation of `async` from here
/home/user/.nimble/pkgs/httpauth-#head/httpauth.nim(198, 6) Warning: 'get_current_user_level' is not GC-safe as it calls 'current_user' [GcUnsafe2]
/home/user/Projects/project/bin/project.nim(38, 1) template/generic instantiation of `routes` from here
/home/user/.nimble/pkgs/jester-0.4.3/jester.nim(1283, 35) template/generic instantiation of `async` from here
/home/user/.nimble/pkgs/httpauth-#head/httpauth.nim(452, 6) Warning: 'create_user' is not GC-safe as it calls 'get_current_user_level' [GcUnsafe2]
/home/user/Projects/project/bin/project.nim(38, 1) template/generic instantiation of `routes` from here
/home/user/.nimble/pkgs/jester-0.4.3/jester.nim(1283, 35) template/generic instantiation of `async` from here
/home/user/.nimble/lib/pure/asyncmacro.nim(272, 31) Error: 'matchIter' is not GC-safe as it calls 'create_user'

bitnom avatar Nov 20 '19 04:11 bitnom

Seems like a httpauth issue.

dom96 avatar Nov 20 '19 22:11 dom96

crap. will just make my own damn auth.

bitnom avatar Nov 30 '19 21:11 bitnom