treeform / puppy

Puppy fetches via HTTP and HTTPS
MIT License
184 stars 27 forks source link

SIGSEGV when sending a Response through a Channel with --gc:orc #30

Closed oakes closed 2 years ago

oakes commented 2 years ago

I made a simple program that launches a worker thread that is supposed to make requests with puppy and send the response down a channel:

import puppy

type
  Action = object
    request: Request
    response: ptr Channel[Response]
  Client = ref object
    requestThread: Thread[Client]
    requestReady: ptr Channel[bool]
    action: ptr Channel[Action]

proc recvAction(client: Client) {.thread.} =
  client.requestReady[].send(true)
  while true:
    let action = client.action[].recv()
    action.response[].send(fetch(action.request))

proc start*(client: var Client) =
  client.requestReady = cast[ptr Channel[bool]](
    allocShared0(sizeof(Channel[bool]))
  )
  client.action = cast[ptr Channel[Action]](
    allocShared0(sizeof(Channel[Action]))
  )
  client.requestReady[].open()
  client.action[].open()
  createThread(client.requestThread, recvAction, client)
  discard client.requestReady[].recv() # wait for thread to be ready

var c = Client()
start(c)

let res = cast[ptr Channel[Response]](
  allocShared0(sizeof(Channel[Response]))
)
res[].open()
c.action[].send(Action(request: Request(url: parseUrl("http://tired.com"), verb: "get"), response: res))
echo res[].recv()[]

If i save that to chtest.nim and run with nim c -r --threads:on --gc:orc chtest.nim I get a SIGSEGV:

/home/sekao/.choosenim/toolchains/nim-#version-1-6/lib/pure/strutils.nim(368) fetch
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

But running it via the default GC works: nim c -r --threads:on chtest.nim

This happens with nim 1.6 RC2 and with nim 1.4.8.

I don't think it's the same as #11 because it happens not just on linux, but mac OS as well. But the error message is similar so maybe it is. I'm guessing it's not specificially a problem with puppy, but i haven't been able to repro the error when sending other data types through a channel :/

oakes commented 2 years ago

I decided to report it to nim instead... https://github.com/nim-lang/Nim/issues/18950