Nim icon indicating copy to clipboard operation
Nim copied to clipboard

Check that `ref` types are not passed to threads

Open vanyle opened this issue 1 year ago • 2 comments

For memory reasons, types containing refs should not be passed to the argument of a thread. This PR enforces this.

Because the macro system is not available from thread as thread is imported by the system module, we use typedesc (an implementation suggested by beef331 here).

The code recursively checks that the object passed does not contains ref inside nested fields.

A test is added to make sure this actually works.

vanyle avatar Apr 08 '24 23:04 vanyle

https://github.com/status-im/nim-testutils seems to contain a bug. Inside ntu.nim:

proc spawnTest(child: var Thread[ThreadPayload]; test: TestSpec;
               core: int): bool =

a thread is created and ThreadPayload is passed to it.

We have:

type ThreadPayload = object
    core: int
    spec: TestSpec

In another file, we have:

TestSpec* = ref object
    section*: string
    args*: string
    ....

It seems to be working!

vanyle avatar Apr 09 '24 00:04 vanyle

../../lib/std/typedthreads.nim(305, 25) Error: type mismatch: got <Thread[system.void], proc (){.nimcall, gcsafe.}, void>
but expected one of:
proc (t: var Thread[system.void], tp: proc (){.nimcall, gcsafe.}){.noSideEffect, gcsafe.}

I don't understand this error in the tests. They are the same type, there is no mismatch. The compiler does not seem in understand that there are two createThread function, a generic and a non-generic one.

vanyle avatar Apr 13 '24 16:04 vanyle