Skip to content

Fatal error on multi-threaded code processing image files #32939

@nlw0

Description

@nlw0

The following script processes a couple of images with some file I/O. Running it with @threads causes a failure. It always breaks when calling the script directly with JULIA_NUM_THREADS>1 , though when includeing it in a running session it seems it breaks the first time and later it works.

Am I wrong in assuming this code is thread-safe in the first place, or could this be a bug somewhere?

using FileIO
using Images

imgs = [
"https://images.unsplash.com/photo-1554266183-2696fdafe3ff?ixlib=rb-1.2.1&auto=format&fit=crop&w=564&q=80"
"https://images.unsplash.com/photo-1527026950045-9e066846bae4?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1419&q=80"
"https://images.unsplash.com/photo-1549287748-f095932c9f81?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=606&q=80"
"https://images.unsplash.com/photo-1437448317784-3a480be9571e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80"
]

files = download.(imgs)

Threads.@threads for f in files
    a = load(f)
    r = rand(eltype(a), size(a)...)
    a .= map(clamp01nan, a + 0.5*r)
    save(f*".png", a)
end

The error:

$ JULIA_NUM_THREADS=4 julia mycode.jl
Error encountered while loading "/tmp/jl_5OG85C".
Fatal error:
Error encountered while loading "/tmp/jl_hZpwGF".
Fatal error:
Error encountered while loading "/tmp/jl_BlmZ9H".
Fatal error:
ERROR: LoadError: concurrency violation detected
Stacktrace:
 [1] try_yieldto(::typeof(Base.ensure_rescheduled), ::Base.RefValue{Task}) at ./task.jl:552
 [2] wait() at ./task.jl:609
 [3] wait(::Base.GenericCondition{Base.Threads.SpinLock}) at ./condition.jl:107
 [4] wait(::Task) at ./task.jl:214
 [5] top-level scope at ./threadingconstructs.jl:75
 [6] include at ./boot.jl:328 [inlined]
 [7] include_relative(::Module, ::String) at ./loading.jl:1094
 [8] include(::Module, ::String) at ./Base.jl:31
 [9] exec_options(::Base.JLOptions) at ./client.jl:295
 [10] _start() at ./client.jl:468
in expression starting at /home/user/src/julia-test/mycode.jl:13
julia> versioninfo()
Julia Version 1.3.0-alpha.1
Commit f2513a8ca6* (2019-07-23 05:06 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
Environment:
  JULIA_NUM_THREADS = 4
  JULIA_EDITOR = emacsclient

Metadata

Metadata

Assignees

No one assigned

    Labels

    multithreadingBase.Threads and related functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions