Skip to content

Profiler deadlocks on Windows because of handle use-after-free #60042

@xal-0

Description

@xal-0

This test case printed 8/100 deadlocked on f80fd6fe76 when I ran it on Windows.

total = 100
failed = 0
code = "using Profile; Profile.@profile identity(1)"
cmd = `$(Base.julia_cmd()) -t2,0 -e $code`
for i=1:total
    p = open(cmd)
    t = Timer(2) do t
        println("failed to exit after 2 seconds")
        kill(p, Base.SIGKILL)
        global failed += 1
    end
    wait(p)
    close(t)
    close(p)
end

println("$failed/$total deadlocked")

I believe this is the same underlying bug as one of the recurring CI failures (https://buildkite.com/julialang/julia-master/builds/51844/steps/canvas?sid=019a412b-595c-48a7-9f8c-7e024f5ab77f).

I discovered this by adding some logging when threads are created and when jl_thread_suspend_and_get_state suspends them:

started worker GetThreadId(): 6392, uv_thread_self() handle: 00000000000002bc
started mark thread id 7584, uv_thread_self() handle: 00000000000002c0
gc thread handle has id 0 after detaching 712
suspending thread GetThreadId(): 6392, using handle: 00000000000002bc
profiler thread GetCurrentThreadId(): 8116, uv_thread_self() handle: 00000000000000d8
suspending thread GetThreadId(): 7100, using handle: 00000000000002b8
profiler thread GetCurrentThreadId(): 8116, uv_thread_self() handle: 00000000000000d8
suspending thread GetThreadId(): 8116, using handle: 00000000000002c0
profiler thread GetCurrentThreadId(): 8116, uv_thread_self() handle: 00000000000000d8

Roughly 10% of the time, the profiler hangs because the closed handle it is trying to suspend now refers to itself. This patch makes the issue go away:

diff --git i/src/gc-stock.c w/src/gc-stock.c
index 312faad91e6..704ca0e28be 100644
--- i/src/gc-stock.c
+++ w/src/gc-stock.c
@@ -3586,7 +3586,6 @@ void jl_start_gc_threads(void)
         else {
             uv_thread_create(&uvtid, jl_parallel_gc_threadfun, t);
         }
-        uv_thread_detach(&uvtid);
     }
 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIndicates an unexpected problem or unintended behaviormultithreadingBase.Threads and related functionalityprofilersystem:windowsAffects only Windows

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions