-
Notifications
You must be signed in to change notification settings - Fork 12
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
Detect and throw an error whenever we get a closure with a Core.Box
#133
Comments
Probably a bit late to the party, and apologies if this is too much of a "just complaining" post, but the following examples seem not super contrived, and do not cause race conditions. let
A = zeros(10)
for _ in 1:10
res = tmap(eachindex(A)) do i
A[i] = i
A[i]
end
A = zeros(10)
end
end # error
let
A = zeros(10)
for _ in 1:10
A = zeros(10)
res = tmap(eachindex(A)) do i
A[i] = i
A[i]
end
end
end # error
let
for _ in 1:10
A = zeros(10)
res = tmap(eachindex(A)) do i
A[i] = i
A[i]
end
end
end # fine Even more obvious is this one: let
A = rand(10)
for _ in 1:10
res = tmap(eachindex(A)) do i
A[i]
end
A = rand(10)
end
end # error I understand that there are performance implications for the boxed variables, but in some cases the performance penalty simply does not matter, the inner function can take a very long time and then this is somewhat negligible (correct me if I'm wrong, compilers can be voodoo to me sometimes :)). |
Hi @lkdvos, thanks for the feedback. "Just complaining" is also fine. I want this package to be useful for people, and it's good for me to hear feedback on this feature / misfeature, even if I don't end up agreeing. My opinions on this are not set in stone, and if it's too problematic I am okay with making this error opt-in rather than opt-out While I usually lean away from hyper opinionated errors, I really do think this one is such a surprising and dangerous class of errors that I really do think there's value here in throwing this error to be extra defensive. I've opened a PR #142 to try and make cases where the boxing is not incorrect a bit better. In particular, your examples can now be fixed with the Perhaps I could also add a way to exempt entire modules from these error checks so that users like you can just say "leave me alone" as a semi-global toggle. Any thoughts? |
Thanks for the nice reply! I failed to consider the The For the In any case, thanks a lot for taking the time for dealing with this. |
This is one way to at least warn users that code like the code shown in #132 is incorrect. The idea would be that when a user calls
tmap(f, itr)
(or
tmapreduce
or whatever), we want to check if any of the fields off
are aCore.Box
, which would signal that the user accidentally has caused a concurrency violation.Here's a fun example:
The problem is that the
A=1
inside the let block is making it so that inside thetmap
forres1
,A
actually is aCore.Box
and every time we assign toA
, we are mutating theCore.Box
as a race condition, and each time we referenceA
, we're making a racey read.The
Core.Box
design for closures is fine for sequential programs, but is wildly incorrect for concurrenct programs. This should actually be pretty easy to detect and throw an informative error.I don't think there is any valid use-case where you'd put a boxed closure into one of our threaded functions and not cause a data-race.
The text was updated successfully, but these errors were encountered: