Skip to content

Commit 75858d7

Browse files
authored
[Tasks] Mark parent as sticky if we use @async (#41334)
Fixes #41324 for 1.7
1 parent 61572a6 commit 75858d7

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

base/task.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,12 @@ function enq_work(t::Task)
621621
# 3. The multiq is full (can be fixed by making it growable).
622622
if t.sticky || Threads.nthreads() == 1
623623
if tid == 0
624+
# Issue #41324
625+
# t.sticky && tid == 0 is a task that needs to be co-scheduled with
626+
# the parent task. If the parent (current_task) is not sticky we must
627+
# set it to be sticky.
628+
# XXX: Ideally we would be able to unset this
629+
current_task().sticky = true
624630
tid = Threads.threadid()
625631
ccall(:jl_set_task_tid, Cvoid, (Any, Cint), t, tid-1)
626632
end

test/threads_exec.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,19 @@ fib34666(x) =
840840
end
841841
@test fib34666(25) == 75025
842842

843+
# issue #41324
844+
@testset "Co-schedule" begin
845+
parent = Threads.@spawn begin
846+
@test current_task().sticky == false
847+
child = @async begin end
848+
@test current_task().sticky == true
849+
@test Threads.threadid() == Threads.threadid(child)
850+
wait(child)
851+
end
852+
wait(parent)
853+
@test parent.sticky == true
854+
end
855+
843856
function jitter_channel(f, k, delay, ntasks, schedule)
844857
x = Channel(ch -> foreach(i -> put!(ch, i), 1:k), 1)
845858
y = Channel(k) do ch

0 commit comments

Comments
 (0)