4

When building this code in nim:

import jester, asyncdispatch    
let stuff = "thing"    
routes:
  get "/":
    resp stuff
runForever()

it results in:

mytest.nim(3, 1) template/generic instantiation from here lib/core/macros.nim(369, 70) template/generic instantiation from here lib/pure/asyncmacro.nim(355, 31) Warning: 'matchIter' is not GC-safe as it accesses 'stuff' which is a global using GC'ed memory [GcUnsafe2]

I suppose it refers to the variable stuff and I suppose it's hard to diagnose because the jester routes are somekind of DSL.

If the message means what it means to mean, then why is it only a warning ? Or is it a false positive ? Or even more, is that concept of using variables in routes just plain impossible ?

v.oddou
  • 6,476
  • 3
  • 32
  • 63

1 Answers1

5

The procedures which are generated by Jester have been marked with {.gcsafe.}, this makes the compiler check whether the procedure accesses any global variables.

Accessing global variables shouldn't be a problem for your application, as long as Jester (and your app) is single threaded, but once your program is using multiple threads (to serve requests in parallel for example) you will need to fix this problem.

One way to fix it is to use a {.threadvar.}: https://nim-lang.org/docs/manual.html#threads-threadvar-pragma

Hope this helps!

dom96
  • 1,012
  • 1
  • 9
  • 22
  • ah nice explanation. ok so this WE I've tried moving to a proc call, and if the proc accesses a global then it doesn't complain. Seems like one indirection is enough to break the static analysis ? – v.oddou Mar 12 '18 at 01:42
  • @v.oddou what Nim version are you on? I tried to do this: http://ix.io/Wup/Nim and as you can see that throws the same kind of warnings. This was on the devel-branch though so you might've found a bug in an older version. – PMunch Mar 13 '18 at 10:42
  • @PMunch very interesting. that's exactly what I did. I wonder if it is a verbosity setting. I was on 0.18.? as provided by the choosenim `init.sh` script – v.oddou Mar 13 '18 at 10:45
  • @v.oddou, just did a 'choosenim update "#devel"' and it still gives me the warning. Try to update yours and see if the problem persists. – PMunch Mar 13 '18 at 13:52
  • I checked again, and it appears that it the variable is of type bool it doesnt warn – v.oddou Mar 13 '18 at 14:38