Skip to content

HttpBeast random segfault on orc and threads > 1 #21422

Open
@dirtboll

Description

@dirtboll

Description

Original issue was posted here dom96/httpbeast#80.

This bug arises when adding new logging handler in httpbeast application that's using orc and threads more than 1. Below is the minimal code that could reproduce the issue (taken from httpbeast example with only addition being addHandler).

# 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())

run(onRequest, httpbeast.initSettings(numThreads=2))

I've tried replicating this by implementing httpbeast Settings object flow, which is being passed between 2 threads that I thought was the root cause, but it didn't give segfault. So far, I'm only able to replicate the bug when adding new ConsoleLogger in httpbeast applications, which also affects jester as well.

Nim Version

Latest nightlies for linux_x64 downloaded here https://github.com/nim-lang/nightlies/releases/tag/latest-version-2-0

Nim Compiler Version 1.9.1 [Linux: amd64]
Compiled at 2023-02-19
Copyright (c) 2006-2022 by Andreas Rumpf

git hash: 414f36980f17ee80a48b2f38e035b7e94f2112bd
active boot switches: -d:release

Also tested on Nim release 1.6.8

Current Output

Starting 2 threads
Listening on port 8080
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Segmentation fault (core dumped)
Error: execution of an external program failed: '/home/ubuntu/hello'

Expected Output

Starting 2 threads
Listening on port 8080

Possible Solution

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/0f7b07f00e4c90eceef90e27da6e997a111407cc/src/httpbeast.nim#L336-L339

Additional Information

  • httpbeast version 0.4.1
  • Tested on GCP VM with Ubuntu 22.04.1 LTS

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions