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

UndefRefError with @threads for multithreading on 1.3 #32846

Closed
Jutho opened this issue Aug 9, 2019 · 9 comments
Closed

UndefRefError with @threads for multithreading on 1.3 #32846

Jutho opened this issue Aug 9, 2019 · 9 comments
Labels
multithreading Base.Threads and related functionality

Comments

@Jutho
Copy link
Contributor

Jutho commented Aug 9, 2019

I seem to get an error with some existing multithreaded code, using @threads for on 1.3. I don't think it is very deterministic, as I don't see it always, but the error does seem to originate in the code generated by the @threads macro, and not in my own code, i.e.

  UndefRefError: access to undefined reference
345  Stacktrace:
346   [1] try_yieldto(::typeof(Base.ensure_rescheduled), ::Base.RefValue{Task}) at ./task.jl:552
347   [2] wait() at ./task.jl:609
348   [3] wait(::Base.GenericCondition{Base.Threads.SpinLock}) at ./condition.jl:107
349   [4] wait(::Task) at ./task.jl:214

See a full report here:
https://travis-ci.org/Jutho/TensorKit.jl/builds/569731249
The error only occurs on 1.3 with multithreading, and all random numbers start from a common seed, so should the code that was ran should be the same on all platforms.

@JeffBezanson
Copy link
Member

Try with #32814. Otherwise the stack trace from the original error is not visible.

@Jutho
Copy link
Contributor Author

Jutho commented Aug 9, 2019

OK I'll give it a try; the problem is that it's not deterministic, the tests ran fine on 1.3 on my Mac.

@Jutho
Copy link
Contributor Author

Jutho commented Aug 9, 2019

Thanks; that gives insight into the problem (which is my fault). Clearly, my first attempt at making LRUCache.jl was not very successful. I have close to zero experience with multithreaded programming, but I think I now fixed the problem. I'll close this here.

@Jutho Jutho closed this as completed Aug 9, 2019
@kapple19
Copy link

kapple19 commented Nov 8, 2020

I'm having this issue as well, just as you describe, it doesn't seem to be deterministic. I run the exact same code multiple times and the error pops up some of the runs, seemingly randomly.

This is my first experience with parallel programming as well. (Biting off a little more than I can chew.)

How did you fix it? I'm on 1.5.

@mzy2240
Copy link

mzy2240 commented Mar 28, 2023

I'm having similar issue as well, with version 1.8.2. Not deterministic as well, it tends to show up once a week. Since it is not deterministic, I am not able to give a MWE, but the pattern where the problem shows up is something like below:

container = []
Threads.@threads for (key, value) in collect(some_dict)
    push!(container, value)
end
res = vcat(container...)  # where UndefRefError shows up sometimes

@JeremiahLR
Copy link

JeremiahLR commented Jul 2, 2023

Hello, I'm having the exact same problem in 1.9.1. Almost the same as mzy2240, @threads in a for loop and a push! (this time to a dataframe). But without the vcat. Error comes from the push! I think. Also not deterministic and only sometimes happens.

I think this issue should be reopened.

@oscardssmith oscardssmith added bug Indicates an unexpected problem or unintended behavior multithreading Base.Threads and related functionality and removed bug Indicates an unexpected problem or unintended behavior labels Jul 3, 2023
@oscardssmith
Copy link
Member

I don't think this is a bug and is just that push! (and most operations on data-structures) in julia aren't thread-safe. you need a lock when resizing an array because otherwise multiple threads can try to resize the array at the same time.

@mzy2240
Copy link

mzy2240 commented Jul 3, 2023

@oscardssmith thanks for the explanation! Do you happen to have an example to show how @threads could work with lock? I tried but did not find any relevant example online.

@JeremiahLR
Copy link

Yes, an example of how to avoid/solve the issue would be most welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
multithreading Base.Threads and related functionality
Projects
None yet
Development

No branches or pull requests

6 participants