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

@threadcall fragility #17819

Closed
maleadt opened this issue Aug 4, 2016 · 5 comments
Closed

@threadcall fragility #17819

maleadt opened this issue Aug 4, 2016 · 5 comments
Labels
domain:multithreading Base.Threads and related functionality

Comments

@maleadt
Copy link
Member

maleadt commented Aug 4, 2016

There's multiple issues with @threadcall I've spotted while debugging a test.

  1. segfaults when printing/... in called cfunction
foo(a) = (println("$a"); Cint(42))
foo_c = cfunction(foo, Cint, (Cint,))
@show       ccall(foo_c, Cint, (Cint,), 1)
@show @threadcall(foo_c, Cint, (Cint,), 1)

Probably by design, but the docs don't mention such a restriction.

  1. argument passing botched
foo(a,b) = (Cint(a+b))
foo_c = cfunction(foo, Cint, (Cint,Cint))
@show       ccall(foo_c, Cint, (Cint,Cint), 1, 2)
@show @threadcall(foo_c, Cint, (Cint,Cint), 1, 2)       # returns 2

The reason is a missing ptr adjustment when creating the args array in do_threadcall:

# cconvert, root and unsafe_convert arguments
roots = Any[]
args_size = isempty(argtypes) ? 0 : sum(sizeof, argtypes)
args_arr = Array{UInt8}(args_size)
ptr = pointer(args_arr)
for (T, x) in zip(argtypes, argvals)
    y = cconvert(T, x)
    push!(roots, y)
    unsafe_store!(convert(Ptr{T}, ptr), unsafe_convert(T, y))
    ptr += sizeof(T)    # ADDED
end
  1. example 2 often deadlocks

When debugging 2) by adding some print statements to do_threadcall, Julia easily locked up. Not sure whether or not this is expected, but I thought I'd mention it anyway.

import Base: do_threadcall

import Base: cconvert, unsafe_convert,
             acquire, threadcall_restrictor,
             thread_notifiers, c_notify_fun, release

function do_threadcall(wrapper::Function, rettype::Type, argtypes::Vector, argvals::Vector)
    # generate function pointer
    fun_ptr = cfunction(wrapper, Int, (Ptr{Void}, Ptr{Void}))

    # cconvert, root and unsafe_convert arguments
    roots = Any[]
    args_size = isempty(argtypes) ? 0 : sum(sizeof, argtypes)
    args_arr = Array{UInt8}(args_size)
    ptr = pointer(args_arr)
    for (T, x) in zip(argtypes, argvals)
        y = cconvert(T, x)
        push!(roots, y)
        unsafe_store!(convert(Ptr{T}, ptr), unsafe_convert(T, y))
        ptr += sizeof(T)    # ADDED
    end

    # create return buffer
    ret_arr = Array{UInt8}(sizeof(rettype))

    # wait for a worker thread to be available
    acquire(threadcall_restrictor)
    idx = findfirst(isnull, thread_notifiers)
    thread_notifiers[idx] = Nullable{Condition}(Condition())

    # queue up the work to be done
    println("queue work")
    ccall(:jl_queue_work, Void,
        (Ptr{Void}, Ptr{UInt8}, Ptr{UInt8}, Ptr{Void}, Cint),
        fun_ptr, args_arr, ret_arr, c_notify_fun, idx)

    # wait for a result & return it
    println("wait for it")
    wait(get(thread_notifiers[idx]))
    thread_notifiers[idx] = Nullable{Condition}()
    release(threadcall_restrictor)

    unsafe_load(convert(Ptr{rettype}, pointer(ret_arr)))
end


using Base.Test

foo(a,b) = (Cint(a+b))
foo_c = cfunction(foo, Cint, (Cint,Cint))
@show       ccall(foo_c, Cint, (Cint,Cint), 1, 2)
@show @threadcall(foo_c, Cint, (Cint,Cint), 1, 2)

Tested on latest master.

@ViralBShah
Copy link
Member

The printing is not threadsafe and there are issues open. This should perhaps be mentioned clearly in the docs for 0.5, if not already there.

I wonder if we should test for the presence of print statements in a threads macro and alert the user - or else we will have a flood of these reports.

@vtjnash
Copy link
Sponsor Member

vtjnash commented Aug 4, 2016

@threadcall is not allowed to call back into Julia code. I thought this was documented somewhere?

@tkelman
Copy link
Contributor

tkelman commented Aug 4, 2016

threadcall was never documented (not in the manual anyway)

@maleadt
Copy link
Member Author

maleadt commented Aug 4, 2016

OK. Will document the restriction and fix the bug.

@maleadt
Copy link
Member Author

maleadt commented Aug 4, 2016

A fragility following from the fact that it can't call back into Julia:

julia> @threadcall(whatever, Void, ())
signal (11): Segmentation fault

maleadt added a commit that referenced this issue Aug 4, 2016
Fixes #17819. Also document not being allowed to call back into Julia.
maleadt added a commit that referenced this issue Aug 4, 2016
Fixes #17819. Also document not being allowed to call back into Julia.
maleadt added a commit that referenced this issue Aug 5, 2016
Fixes #17819. Also document not being allowed to call back into Julia.
@kshyatt kshyatt added the domain:multithreading Base.Threads and related functionality label Aug 8, 2016
maleadt added a commit that referenced this issue Aug 13, 2016
Fixes #17819. Also document not being allowed to call back into Julia.
tkelman pushed a commit that referenced this issue Aug 20, 2016
Fixes #17819. Also document not being allowed to call back into Julia.

(cherry picked from commit aea8180)
ref #18016
mfasi pushed a commit to mfasi/julia that referenced this issue Sep 5, 2016
Fixes JuliaLang#17819. Also document not being allowed to call back into Julia.
mfasi pushed a commit to mfasi/julia that referenced this issue Sep 5, 2016
Fixes JuliaLang#17819. Also document not being allowed to call back into Julia.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:multithreading Base.Threads and related functionality
Projects
None yet
Development

No branches or pull requests

5 participants