dom96 / httpbeast

A highly performant, multi-threaded HTTP 1.1 server written in Nim.
MIT License
450 stars 53 forks source link

Random segmentation fault when using orc and threads more than 1 #80

Open dirtboll opened 1 year ago

dirtboll commented 1 year ago

This minimal code should reproduce the issue.

# hello.nim
import options, asyncdispatch, logging

import httpbeast

proc onRequest(req: Request): Future[void] =
  if req.httpMethod == some(HttpGet):
    case req.path.get()
    of "/":
      req.send("Hello World")
    else:
      req.send(Http404)

if logging.getHandlers().len == 0:
  addHandler(logging.newConsoleLogger())
  setLogFilter(when defined(release): lvlInfo else: lvlDebug)

run(onRequest, httpbeast.initSettings(numThreads=2))
nim c -r --gc:orc -d:releae --threads:on hello.nim

When running and killing the binary multiple times, the program sometimes runs but most of the times it gives this error:

root@4dfa057c787d:~# ./hello
Starting 2 threads
Listening on port 8080
Traceback (most recent call last)
/root/.nimble/pkgs/httpbeast-0.4.1/httpbeast.nim(83) eventLoop
/nim/lib/system/orc.nim(46) nimIncRefCyclic
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Segmentation fault

I initially found the issue when using Jester. After some searching I found that I can solve the issue by commenting this block of code. Other workaround that I did was modifying Settings.loggers to ref seq[Logger], which also removes the random segfault. https://github.com/dom96/httpbeast/blob/7de559e6eab97763bae16b8a973373e9fe691574/src/httpbeast.nim#L336-L339

dom96 commented 1 year ago

I expect ORC to be compatible with the current default GC so it would be nice to confirm whether this is an ORC bug. Can you raise the same in the Nim repo?

dirtboll commented 1 year ago

Sure, I'll post the issue there too.

PMunch commented 1 year ago

@dirtboll, did you ever post in in the main repo? Would be nice to have the issue linked here so it's easier to track. I just faced the same issue today with the devel version of Nim which enables --gc:orc --threads:on by default.

dirtboll commented 1 year ago

I forgot, it's been 4 months... Yeah I've just tested it with the latest nightlies, still got it. Will post right away.

bung87 commented 1 year ago

have you tried after https://github.com/dom96/httpbeast/pull/89 merged ? I run test locally result seem stable green.