Skip to content

Commit cc9aee8

Browse files
committed
specify that sync::OnceCell deadlocks when initialized reentrantly
1 parent 07c3c59 commit cc9aee8

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

text/0000-standard-lazy-types.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl<T> OnceCell<T> {
7070
/// remains uninitialized.
7171
///
7272
/// It is an error to reentrantly initialize the cell from `f`. Doing
73-
/// so results in a panic.
73+
/// so results in a panic or a deadlock.
7474
pub fn get_or_init<F>(&self, f: F) -> &T
7575
where
7676
F: FnOnce() -> T,
@@ -86,7 +86,7 @@ impl<T> OnceCell<T> {
8686
/// remains uninitialized.
8787
///
8888
/// It is an error to reentrantly initialize the cell from `f`. Doing
89-
/// so results in a panic.
89+
/// so results in a panic or a deadlock.
9090
pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E>
9191
where
9292
F: FnOnce() -> Result<T, E>,
@@ -246,6 +246,8 @@ Non thread-safe flavor can be added to `core` as well.
246246
The thread-safe variant is implemented similarly to `std::sync::Once`.
247247
Crucially, it has support for blocking: if many threads call `get_or_init` concurrently, only one will be able to execute the closure, while all other threads will block.
248248
For this reason, most of `std::sync::OnceCell` API can not be provided in `core`.
249+
In the `sync` case, reliably panicking on re-entrant initialization is not trivial.
250+
For this reason, the implementaion would simply deadlock, with a note that a deadlock might be elevated to panic in the future.
249251

250252
# Drawbacks
251253
[drawbacks]: #drawbacks

0 commit comments

Comments
 (0)