Skip to content

Throw ConcurrencyViolationError on yield with current_task #53974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 16, 2024
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
8 changes: 6 additions & 2 deletions base/task.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1040,11 +1040,15 @@ end

A fast, unfair-scheduling version of `schedule(t, arg); yield()` which
immediately yields to `t` before calling the scheduler.

Throws a `ConcurrencyViolationError` if `t` is the currently running task.
"""
function yield(t::Task, @nospecialize(x=nothing))
(t._state === task_state_runnable && t.queue === nothing) || error("yield: Task not runnable")
current = current_task()
t === current && throw(ConcurrencyViolationError("Cannot yield to currently running task!"))
(t._state === task_state_runnable && t.queue === nothing) || throw(ConcurrencyViolationError("yield: Task not runnable"))
t.result = x
enq_work(current_task())
enq_work(current)
set_next_task(t)
return try_yieldto(ensure_rescheduled)
end
Expand Down
4 changes: 3 additions & 1 deletion test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,12 @@ end

@test_throws ErrorException("deadlock detected: cannot wait on current task") wait(current_task())

@test_throws ConcurrencyViolationError("Cannot yield to currently running task!") yield(current_task())

# issue #41347
let t = @async 1
wait(t)
@test_throws ErrorException yield(t)
@test_throws ConcurrencyViolationError yield(t)
end

let t = @async error(42)
Expand Down