Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions base/condition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ function _wait2(c::GenericCondition, waiter::Task)
ct = current_task()
assert_havelock(c)
push!(c.waitq, waiter)
# since _wait2 is similar to schedule, we should observe the sticky bit now
if waiter.sticky && Threads.threadid(waiter) == 0
# Issue #41324
# t.sticky && tid == 0 is a task that needs to be co-scheduled with
# the parent task. If the parent (current_task) is not sticky we must
# set it to be sticky.
# XXX: Ideally we would be able to unset this
ct.sticky = true
tid = Threads.threadid()
ccall(:jl_set_task_tid, Cvoid, (Any, Cint), waiter, tid-1)
end
return
end

Expand Down
13 changes: 13 additions & 0 deletions base/task.jl
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,18 @@ function _wait2(t::Task, waiter::Task)
if !istaskdone(t)
push!(t.donenotify.waitq, waiter)
unlock(t.donenotify)
# since _wait2 is similar to schedule, we should observe the sticky
# bit, even if we aren't calling `schedule` due to this early-return
if waiter.sticky && Threads.threadid(waiter) == 0
# Issue #41324
# t.sticky && tid == 0 is a task that needs to be co-scheduled with
# the parent task. If the parent (current_task) is not sticky we must
# set it to be sticky.
# XXX: Ideally we would be able to unset this
current_task().sticky = true
tid = Threads.threadid()
ccall(:jl_set_task_tid, Cvoid, (Any, Cint), waiter, tid-1)
Comment on lines +317 to +319
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vtjnash and @vchuravy: I think this is wrong. Here, waiter is waiting on t, not on current_task() which can be completely different (e.g. see bind).

Apparently, we want to pin t and waiter to the same thread. But I'm not sure how this follows from the original problem: #41324. It's perfectly reasonable for a task in the interactive threadpool to wait on a task in the default threadpool, but this makes that unachievable.

I know it's been a while, but can either of you remember why this was added after #41334?

Copy link
Member Author

@vtjnash vtjnash May 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_wait2 is defined as semantically a call to schedule the Task waiter to immediately call wait on some condition variable t. Hence why it has the same side-effects as calling schedule: it should pin current_task and waiter to one thread, but not t (which will have completed before waiter runs anyways).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That task t will have completed before waiter runs makes sense. But this _wait2 as opposed to the one in condition.jl has nothing to do with Conditions. t is a Task here, not a Condition. The semantics of this _wait2 seem unrelated to current_task().

end
return nothing
else
unlock(t.donenotify)
Expand Down Expand Up @@ -455,6 +467,7 @@ function errormonitor(t::Task)
end
nothing
end
t2.sticky = false
_wait2(t, t2)
return t
end
Expand Down