diff --git a/base/task.jl b/base/task.jl index 611135cb6981c..feec0371f754a 100644 --- a/base/task.jl +++ b/base/task.jl @@ -776,17 +776,25 @@ function enq_work(t::Task) # Sticky tasks go into their thread's work queue. if t.sticky tid = Threads.threadid(t) - if tid == 0 && !GC.in_finalizer() + if tid == 0 # The task is not yet stuck to a thread. Stick it to the current # thread and do the same to the parent task (the current task) so # that the tasks are correctly co-scheduled (issue #41324). # XXX: Ideally we would be able to unset this. - tid = Threads.threadid() - ccall(:jl_set_task_tid, Cint, (Any, Cint), t, tid-1) - current_task().sticky = true + if GC.in_finalizer() + # The task was launched in a finalizer. There is no thread to sticky it + # to, so just allow it to run anywhere as if it had been non-sticky. + t.sticky = false + @goto not_sticky + else + tid = Threads.threadid() + ccall(:jl_set_task_tid, Cint, (Any, Cint), t, tid-1) + current_task().sticky = true + end end push!(workqueue_for(tid), t) else + @label not_sticky tp = Threads.threadpool(t) if Threads.threadpoolsize(tp) == 1 # There's only one thread in the task's assigned thread pool;