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

Detect and throw an error whenever we get a closure with a Core.Box #133

Open
MasonProtter opened this issue Jan 28, 2025 · 0 comments
Open

Comments

@MasonProtter
Copy link
Member

MasonProtter commented Jan 28, 2025

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 of f are a Core.Box, which would signal that the user accidentally has caused a concurrency violation.

Here's a fun example:

julia> let
           A = 1
           res1 = tmap(1:10) do i
               A = i
               sleep(rand())
               A
           end
           res2 = tmap(1:10) do i
               local A = i
               sleep(rand())
               A
           end
           @info "Oops!" res1' res2'
       end
┌ Info: Oops!
│   res1' =1×10 adjoint(::Vector{Int64}) with eltype Int64:8  2  10  8  4  2  6  8  10  4
│   res2' =1×10 adjoint(::Vector{Int64}) with eltype Int64:1  2  3  4  5  6  7  8  9  10

The problem is that the A=1 inside the let block is making it so that inside the tmap for res1, A actually is a Core.Box and every time we assign to A, we are mutating the Core.Box as a race condition, and each time we reference A, 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant