Skip to content

Error on JavaCall.init() with Julia >= 1.12 #170

@arnaud-ma

Description

@arnaud-ma
julia> using JavaCall

julia> JavaCall.init()
ERROR: TaskFailedException

    nested task error: BoundsError: attempt to access 1-element Vector{Ptr{JavaCall.JNI.JNIEnv}} at index [2]
    Stacktrace:
     [1] throw_boundserror(A::Vector{Ptr{JavaCall.JNI.JNIEnv}}, I::Tuple{Int64})
       @ Base ./essentials.jl:15
     [2] getindex
       @ ./essentials.jl:917 [inlined]
     [3] getindex
       @ ./refpointer.jl:193 [inlined]
     [4] attach_current_thread
       @ ~/.julia/packages/JavaCall/IAoV2/src/JNI.jl:171 [inlined]
     [5] macro expansion
       @ ~/.julia/packages/JavaCall/IAoV2/src/JNI.jl:176 [inlined]
     [6] (::JavaCall.JNI.var"#attach_threads##0#attach_threads##1"{})(tid::Int64; onethread::Bool)
       @ JavaCall.JNI ./threadingconstructs.jl:276
     [7] #attach_threads##0
       @ ./threadingconstructs.jl:243 [inlined]
     [8] (::Base.Threads.var"#threading_run##0#threading_run##1"{})()
       @ Base.Threads ./threadingconstructs.jl:177
Stacktrace:
 [1] threading_run(fun::JavaCall.JNI.var"#attach_threads##0#attach_threads##1"{}, static::Bool)
   @ Base.Threads ./threadingconstructs.jl:196
 [2] macro expansion
   @ ./threadingconstructs.jl:213 [inlined]
 [3] attach_threads
   @ ~/.julia/packages/JavaCall/IAoV2/src/JNI.jl:175 [inlined]
 [4] init_new_vm(libpath::Tuple{String}, opts::OrderedCollections.OrderedSet{String})
   @ JavaCall.JNI ~/.julia/packages/JavaCall/IAoV2/src/JNI.jl:164
 [5] _init(opts::OrderedCollections.OrderedSet{String})
   @ JavaCall ~/.julia/packages/JavaCall/IAoV2/src/jvm.jl:291
 [6] init()
   @ JavaCall ~/.julia/packages/JavaCall/IAoV2/src/jvm.jl:277
 [7] top-level scope
   @ REPL[2]:1
Some type information was truncated. Use `show(err)` to see complete types.

julia> versioninfo()
Julia Version 1.12.0-beta1
Commit c175ace780d (2025-04-02 11:19 UTC)
Build Info:
  Official https://julialang.org release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 16 × AMD Ryzen 7 8840HS w/ Radeon 780M Graphics
  WORD_SIZE: 64
  LLVM: libLLVM-18.1.7 (ORCJIT, znver3)
  GC: Built with stock GC
Threads: 1 default, 1 interactive, 1 GC (on 16 virtual cores)
Environment:
  JULIA_REVISE_POLL = 1
  JULIA_COPY_STACKS = 1

This is probably because of

JavaCall.jl/src/JNI.jl

Lines 168 to 178 in 2f1824f

function attach_current_thread(ppenv_thread = Ref{Ptr{JNIEnv}}(C_NULL))
res = ccall(jvmfunc[].AttachCurrentThread, Cint, (Ptr{Nothing}, Ptr{Ptr{JNIEnv}}, Ptr{Nothing}), ppjvm[], ppenv_thread, C_NULL)
res < 0 && throw(JNIError("Unable to attach thread id: $(Threads.threadid())"))
return ppenv_thread[]
end
function attach_threads()
Threads.@threads for i=1:Threads.nthreads()
attach_current_thread(Ref(ppenv, Threads.threadid() ))
end
end

See JuliaFolds2/OhMyThreads.jl#136 (comment) and https://julialang.org/blog/2023/07/PSA-dont-use-threadid/

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions