Skip to content

Commit 824ceef

Browse files
authored
Clarify wait(::Task) documentation (#53722)
Small follow-up to #53685, since having critical documentation for a specific method be only mentioned in a catch-all docstring isn't quite as helpful as it could be (e.g. when looking at inline documentation with LSP, where it happens to know that the variable being passed is a `Task`). In addition, this also documents that `wait` on a `Task` will throw if given `current_task`. I've also split the function into a prototype and the actual implementation, so that the same "more specific docs at the method definition" transform can be applied for `GenericCondition` too.
1 parent f259eba commit 824ceef

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

base/condition.jl

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,16 @@ end
103103
"""
104104
wait([x])
105105
106-
Block the current task until some event occurs, depending on the type of the argument:
106+
Block the current task until some event occurs.
107107
108108
* [`Channel`](@ref): Wait for a value to be appended to the channel.
109109
* [`Condition`](@ref): Wait for [`notify`](@ref) on a condition and return the `val`
110-
parameter passed to `notify`. Waiting on a condition additionally allows passing
111-
`first=true` which results in the waiter being put _first_ in line to wake up on `notify`
112-
instead of the usual first-in-first-out behavior.
110+
parameter passed to `notify`. See the `Condition`-specific docstring of `wait` for
111+
the exact behavior.
113112
* `Process`: Wait for a process or process chain to exit. The `exitcode` field of a process
114113
can be used to determine success or failure.
115-
* [`Task`](@ref): Wait for a `Task` to finish. If the task fails with an exception, a
116-
`TaskFailedException` (which wraps the failed task) is thrown. Waiting on a task
117-
additionally allows passing `throw=false` which prevents throwing a `TaskFailedException`
118-
when the task fails.
114+
* [`Task`](@ref): Wait for a `Task` to finish. See the `Task`-specific docstring of `wait` for
115+
the exact behavior.
119116
* [`RawFD`](@ref): Wait for changes on a file descriptor (see the `FileWatching` package).
120117
121118
If no argument is passed, the task blocks for an undefined period. A task can only be
@@ -124,6 +121,16 @@ restarted by an explicit call to [`schedule`](@ref) or [`yieldto`](@ref).
124121
Often `wait` is called within a `while` loop to ensure a waited-for condition is met before
125122
proceeding.
126123
"""
124+
function wait end
125+
126+
"""
127+
wait(c::GenericCondition; first::Bool=false)
128+
129+
Wait for [`notify`](@ref) on `c` and return the `val` parameter passed to `notify`.
130+
131+
If the keyword `first` is set to `true`, the waiter will be put _first_
132+
in line to wake up on `notify`. Otherwise, `wait` has first-in-first-out (FIFO) behavior.
133+
"""
127134
function wait(c::GenericCondition; first::Bool=false)
128135
ct = current_task()
129136
_wait2(c, ct, first)

base/task.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,18 @@ function _wait2(t::Task, waiter::Task)
355355
nothing
356356
end
357357

358+
"""
359+
wait(t::Task; throw=true)
360+
361+
Wait for a `Task` to finish.
362+
363+
The keyword `throw` (defaults to `true`) controls whether a failed task results
364+
in an error, thrown as a [`TaskFailedException`](@ref) which wraps the failed task.
365+
366+
Throws a `ConcurrencyViolationError` if `t` is the currently running task, to prevent deadlocks.
367+
"""
358368
function wait(t::Task; throw=true)
359-
t === current_task() && error("deadlock detected: cannot wait on current task")
369+
t === current_task() && Core.throw(ConcurrencyViolationError("deadlock detected: cannot wait on current task"))
360370
_wait(t)
361371
if throw && istaskfailed(t)
362372
Core.throw(TaskFailedException(t))

test/misc.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ let c = Ref(0),
251251
@test c[] == 100
252252
end
253253

254-
@test_throws ErrorException("deadlock detected: cannot wait on current task") wait(current_task())
254+
@test_throws ConcurrencyViolationError("deadlock detected: cannot wait on current task") wait(current_task())
255255

256256
# issue #41347
257257
let t = @async 1

0 commit comments

Comments
 (0)