Skip to content

Deadlock in Profile on MacOS with multithreaded program #40972

@NHDaly

Description

@NHDaly

The following simple multithreaded program deadlocks hangs (I don't actually know why it's hanging):

julia> Threads.nthreads()
4

julia> using Profile

julia> const b = Threads.Atomic{Bool}(true)
Base.Threads.Atomic{Bool}(true)

julia> const out = rand(2,2)
2×2 Matrix{Float64}:
 0.0811545  0.0654667
 0.850495   0.684218

julia> @profile peakflops()
1.501919194588322e11

julia> t = Threads.@spawn while b[] out[1] = out[1] + 1 end
Task (runnable) @0x0000000112478340

julia> t
Task (runnable) @0x0000000112478340

julia> Profile.clear() ; Profile.init(n=Int(5e7), delay=0.0001) ; @profile peakflops()
^C^C^C^C

Same with this even simpler final line:

julia> Profile.clear() ; Profile.init(n=Int(5e7), delay=0.0001) ; @profile sleep(1)

BUT, interestingly, this doesn't get stuck if you change the final line to actually stop the infinite loop thread before finishing the profiling:

julia> Profile.clear() ; Profile.init(n=Int(5e7), delay=0.0001) ; @profile (sleep(1) ; b[] = false)

So my guess is it has something to do with resuming the program after finishing profiling while there is a thread sitting in a loop?


Also, i thought maybe this was because after the profiling, the GC tried to pause-the-world for GC, but because the thread has no yield-points, everything is stuck waiting for that thread to yield? But I tried adding a yield-point to the while loop, and it still hangs:

julia> using Profile

julia> const b = Threads.Atomic{Bool}(true)
Base.Threads.Atomic{Bool}(true)

julia> const out = rand(2,2)
2×2 Matrix{Float64}:
 0.342338  0.339537
 0.854418  0.0590949

julia> @profile peakflops()
1.2399335606400201e11

julia> t = Threads.@spawn while b[] out[1] = out[1] + 1 ; yield() end
Task (runnable) @0x0000000114378340

julia> t
Task (runnable) @0x0000000114378340

julia> Profile.clear() ; Profile.init(n=Int(5e7), delay=0.0001) ; @profile peakflops()
^C^C^C^C^CWARNING: Force throwing a SIGINT
^C^C^C^C

I see this currently on julia 1.6.1 and on julia master. Here's my versioninfo for 1.6:

julia> versioninfo()
Julia Version 1.6.1
Commit 6aaedecc44 (2021-04-23 05:59 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin19.6.0)
  CPU: Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, skylake)
Environment:
  JULIA_NUM_THREADS = 4

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIndicates an unexpected problem or unintended behaviormultithreadingBase.Threads and related functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions