Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spawn passes refs across threads #7057

Closed
yglukhov opened this issue Jan 10, 2018 · 1 comment

Comments

@yglukhov
Copy link
Member

commented Jan 10, 2018

import threadpool

type Foo = ref object
proc echoFooAddr(f: Foo) =
    echo cast[int](f)

proc test(i: Foo) =
    var p = proc() =
        echoFooAddr(i)
    p() # Print i addr on the main thread
    spawn p() # print i addr on the secondary thread
    sync()

var i: Foo
i.new()
test(i)
nim c -r --threads:on ~/test.nim

Outputs two identical numbers. In real life results in random gc crashes.

Expected behavior: closure context should be deep-copied, so that secondary thread prints different address.

@yglukhov yglukhov changed the title Spawn passes refs from main thread to background thread Spawn passes refs across threads Jan 10, 2018

@Araq Araq added the Showstopper label Jan 12, 2018

@yglukhov

This comment has been minimized.

Copy link
Member Author

commented Jan 24, 2018

Another option would be forbidding closures in spawn, whatever is easier. In case of forbidding the problem may be worked around by wrapping the closure into an object type, in such case the runtime behavior is correct.

@Araq Araq added High Priority and removed Showstopper labels Feb 27, 2018

@Araq Araq closed this in 2678fa6 Jul 6, 2019

narimiran added a commit that referenced this issue Jul 8, 2019

fixes #7057
(cherry picked from commit 2678fa6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.