Repository navigation
Open
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCE-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.E-help-wantedCall for participation: Help is requested to fix this issue.I-libs-radarLibs issues that are tracked on the team's radar.T-libsRelevant to the library team, which will review and decide on the PR/issue.
Description
Feature gates:
#![feature(sync_nonpoison)]#![feature(nonpoison_condvar)]#![feature(nonpoison_mutex)]#![feature(nonpoison_rwlock)]
This is a tracking issue for versions of synchronization primitives that do not not need to worry about poison.
Public API
sync_nonpoison
The module itself and common types will be gated by this feature:
// std::sync
mod nonpoison {
pub type TryLockResult<Guard> = Result<Guard, WouldBlock>;
// Error type for failed locking
pub struct WouldBlock;
}nonpoison_condvar
// std::sync::nonpoison
pub struct Condvar { /* ... */ }
impl Condvar {
pub const fn new() -> Self;
pub fn wait<T>(&self, guard: &mut MutexGuard<'_, T>)
pub fn notify_one(&self);
pub fn notify_all(&self);
pub fn wait_while<T, F>(&self, guard: &mut MutexGuard<'_, T>, mut condition: F)
where
F: FnMut(&mut T) -> bool;
pub fn wait_timeout<T>(
&self,
guard: &mut MutexGuard<'_, T>,
dur: Duration,
) -> WaitTimeoutResult;
pub fn wait_timeout_while<T, F>(
&self,
guard: &mut MutexGuard<'_, T>,
dur: Duration,
mut condition: F,
) -> WaitTimeoutResult
where
F: FnMut(&mut T) -> bool;
}
/* trait implementations from `std::sync::poison::Condvar` */nonpoison_mutex
// std::sync::nonpoison
pub struct Mutex<T: ?Sized> { /* ... */ }
impl<T> Mutex<T> {
pub const fn new(t: T) -> Self;
}
impl<T: ?Sized> Mutex<T> {
pub fn lock(&self) -> MutexGuard<'_, T>;
pub fn try_lock(&self) -> TryLockResult<MutexGuard<'_, T>>;
pub fn get_mut(&mut self) -> &mut T;
pub fn into_inner(self) -> T
where
T: Sized;
}
/* trait implementations from `std::sync::poison::Mutex` */
pub struct MutexGuard<'a, T: ?Sized + 'a> { /* ... */ }
impl<'a, T: ?Sized> MutexGuard<'a, T> {
// Unstable API from `mapped_lock_guards`
}
/* trait implementations from `std::sync::poison::MutexGuard` */
// Currently unstable under `mapped_lock_guards`, see that tracking issue for more
pub struct MappedMutexGuard<'a, T: ?Sized + 'a> { /* ... */ }nonpoison_rwlock
// std::sync::nonpoison
pub struct RwLock<T: ?Sized> { /* ... */ }
impl<T> RwLock<T> {
pub const fn new(t: T) -> Self;
}
impl<T: ?Sized> RwLock<T> {
pub fn read(&self) -> RwLockReadGuard<'_, T>;
pub fn try_read(&self) -> TryLockResult<RwLockReadGuard<'_, T>>;
pub fn write(&self) -> RwLockWriteGuard<'_, T>;
pub fn try_write(&self) -> TryLockResult<RwLockWriteGuard<'_, T>>;
pub fn get_mut(&mut self) -> &mut T;
pub fn into_inner(self) -> T
where
T: Sized;
}
/* trait implementations from `std::sync::poison::RwLock` */
pub struct RwLockReadGuard<'a, T: ?Sized + 'a> { /* private fields */ }
impl<'a, T: ?Sized> RwLockReadGuard<'a, T> {
// Unstable API from `mapped_lock_guards`
}
/* trait implementations from `std::sync::poison::RwLockReadGuard` */
impl<'a, T: ?Sized> RwLockWriteGuard<'a, T> {
// Unstable API from `mapped_lock_guards`
}
/* trait implementations from `std::sync::poison::RwLockWriteGuard` */
// Currently unstable under `mapped_lock_guards`, see that tracking issue for more
pub struct MappedRwLockReadGuard<'a, T: ?Sized + 'a> { /* ... */ }
pub struct MappedRwLockReadGuard<'a, T: ?Sized + 'a> { /* ... */ }Steps / History
- ACP: Adding locks disregarding poison libs-team#169
-
Muteximplementation: Implementation:#[feature(sync_nonpoison)],#[feature(nonpoison_mutex)]#144022 -
RwLockimplementation Implementation:#[feature(nonpoison_rwlock)]#144648 -
Condvarimplementation Implementation:#[feature(nonpoison_condvar)]#144651 - Final comment period (FCP)1
- Stabilization PR
Unresolved Questions
- Should existing types without poison be moved to this module?
Barrier,LazyLock,OnceLock,ReentrantLock. (Answer: no, it only makes sense to move to thepoisonandnonpoisonmodules if there is a version for each) - Should we use this as an opportunity to change the variance of
RwLockto supporttry_upgrade? AddRwlocktry_upgrademethod libs-team#514 / Tracking Issue forrwlock_try_upgrade#138559 (comment)
Related
- Move all existing poisonable types to
std::sync::poisonand reexport them Tracking Issue forsync_poison_mod#134646 - The unstable
ReentrantLockcurrently does not support poisoning Tracking Issue forReentrantLock#121440 mapped_lock_guardsadds API to the guard types Tracking Issue formapped_lock_guards(MappedMutexGuard,MappedRwLockReadGuard,MappedRwLockWriteGuard) #117108- Discussion for changing the defaults Edition 2027: Consider replacing
std::sync::Mutexand other locks withstd::sync::nonpoisonversions #149359
Footnotes
Reacted by Nazar Mokrynskyi, ahoyiski, Nano, Cherry, David Hoppenbrouwers, zopsicle, Chris Hamons, qouteall, Armin Ronacher, Falk Woldmann Lu and 4 more
Reacted by oxalica, Visne, Bryan Hitchcock, James C. Wise, Hiro Saito, Nano, Cherry, papill9, Chris Hamons, NOT_A_ROBOT and 3 more
Metadata
Metadata
Assignees
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCE-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.E-help-wantedCall for participation: Help is requested to fix this issue.I-libs-radarLibs issues that are tracked on the team's radar.T-libsRelevant to the library team, which will review and decide on the PR/issue.
Activity
[-]Tracking Issue for `nonpoison_mutex`[/-][+]Tracking Issue for `nonpoison_mutex` and `sync_poison`[/+][-]Tracking Issue for `nonpoison_mutex` and `sync_poison`[/-][+]Tracking Issue for `nonpoison_mutex`[/+][-]Tracking Issue for `nonpoison_mutex`[/-][+]Tracking Issue for `sync_nonpoison` and `nonpoison_{condvar,mutex,once,rwlock}`[/+]GrigorenkoPV commentedon Dec 23, 2024
Should
Barrier,LazyLock, andOnceLock(all of which ignore poison) be moved tostd::sync::nonpoisonas well (and then unconditionally reexported instd::sync)?tgross35 commentedon Dec 23, 2024
I am not sure - for types where we are unlikely to have both versions, it doesn't gain too much. This is also applicable for something like the unstable
ReentrantLock(cc @joboet) which is currently nonpoison-only.Maybe just leave it as-is for now, I'll add this as an unresolved question.
connortsui20 commentedon Jul 19, 2025
@rustbot claim
connortsui20 commentedon Jul 20, 2025
Unresolved questions (mainly for
Once):nonpoisonmean all evidence of poison / panicking should be hidden?Onceto exist innonpoisonwhenOncesupports both poisoning and nonpoisoning (see Tracking issue for std::sync::Once poisoning #33577)?nonpoisonmeans forOnce. Thecall_once_forcemethod that exists onpoison::Oncetechnically "ignores" poison, yet the closure is given aOnceStatethat tells the caller if the state was poisoned. Shouldcall_once_forcebe included in anonpoison::Once?Onceshould exist innonpoison, should the API for [nonpoison::Once] be reduced to just a [call_once] and [wait] method, where they are actually just copies of [poison::Once::call_once_force] and [poison::Once::wait_force]? That makes perfect sense forwait_force->waitbut not forcall_once_force->call_oncebecause of theOnceStatementioned above.Personally, I think it makes sense to completely get rid of
call_once_forcein [nonpoison::Once] and then figure out if it makes sense for [nonpoison::Once::call_once] to pass aOnceStateto the closure (which only tells the closure if it is poisoned or not).Rollup merge of rust-lang#144022 - connortsui20:sync_nonpoison, r=tgr…
58 remaining items
clarfonthey commentedon Oct 13, 2026
Note: our general opinion was that this is going to need deeper investigation into implementation and we're likely not going to stabilize this API as-is. In particular, we probably want someone to take a stab at refactoring the nonpoisoning and poisoning mutexes to use more of the same code, even if they're just wrappers over the same thing.
connortsui20 commentedon Oct 13, 2026
@clarfonthey I am happy to give unifying the code a stab (though probably not immediately as I am busy until closer to the end of the month).
Though I am wondering why the internal implementation is a blocker on the API itself?
clarfonthey commentedon Oct 13, 2026
So, the main reason why is because there are folks who are convinced that we can make the poisoning effectively zero-cost, and you could just choose which to do, in which case we would probably want to combine the APIs and make it just that you choose whether you want poisoning when locking.
But, such questions would require presenting a working solution for that rather than just guessing.
joboet commentedon Oct 15, 2026
That's only possible on platforms where there are unused bits in the lock state, which is not the case on Windows 7 and on future
os_unfair_lock-basedMutexon the Apple platforms.clarfonthey commentedon Oct 22, 2026
Yes, it is worth noting that this would be "effectively zero-cost" on most platforms, although still cc @nia-e since she was the main proponent of this idea.
connortsui20 commentedon Nov 3, 2026
Sorry for the radio silence. I've been experimenting a bit with this, and I wanted to share some of my findings before I propose anything concrete (as the solution space here is not exactly small).
There seem to be 2 main open questions in this thread:
For 1, there are many ways to share code, but we would want a reasonable (maintainable) implementation. And we also do not want to make a breaking public API change. I think this is definitely possible, and maybe the first step is to unify the existing implementations. But I am hesitant to do that now, especially if a good chunk of the code is going to change due to the next question...
For 2, it is possible on some platforms to use spare bits for the poisoning flag, but very early and quick experiments on my x86 linux machine showed that packing a poison flag into the futex regressed uncontended lock/mutate/unlock cycles by a median 21% compared to the existing poison mutex implementation. I'm pretty sure this is due to the atomic state gymnastics (can't clear everything to 0 if I have a poison bit set). I would personally say that this large of a regression is unacceptable (again, early benchmarks, but not super encouraging).
These questions lead to a followup question: If they have shared implementations, and poisoning is actually zero cost, can a single lock type express both behaviors? Even if this is possible, I am not sure this is something we would want from an API design perspective.
There is also a followup question of how best to represent poisoning as a type. I believe I saw a previous proposal to have a
Poison<T>type as a wrapper to make poisoning a composable abstraction. I think this is at odds with the idea that poisoning can be zero-cost w.r.t. space, but if we are willing to relax the zero-cost space constraint, that could potentially be useful.Those are some of my high-level findings, and I will continue to experiment. Interested in what people think!
connortsui20 commentedon Nov 13, 2026
Ok so I have done a bit more thinking but unfortunately am not able to come to a better conclusion or make progress... So it might be good if there's a bit more discussion on the above.
@rustbot label +I-libs-nominated
Again, we can make progress by unifying implementations, but if there are more ambitious plans to both unify the APIs and also make poisoning "free" on certain platforms (and not others) then the unification is probably a waste of time.
clarfonthey commentedon Nov 23, 2026
(Note: was deferred last week's meeting, Nia had some more investigating she wanted to do.)
clarfonthey commentedon Nov 24, 2026
(Note 2: Nia is busy today so she won't be at the meeting, so, I'll just demote this to radar and she can promote back to nominated when she's ready. Hopefully she can be cured of the scourge that is having work to do so that she can get back to work on the real work.)
@rustbot label -I-libs-nominated +I-libs-radar
mitsuhiko commentedon Nov 26, 2026
I have some vested interest in having non poisoning mutexes in the standard library. I did some testing and unfortunately I don't think it's possible to have a
Poison<T>type be zero cost but you can have poisoning and non poisoning mutexes share most code without introducing cost by having a policy type internally.General slop warning here but I had an agent try a few different approaches for code sharing and I think ultimately it's worth it: https://github.com/mitsuhiko/mutexes
The basic idea is here: https://github.com/mitsuhiko/mutexes/blob/main/SHARED_IMPL.md
Internally one adds those types:
connortsui20 commentedon Nov 26, 2026
@mitsuhiko Thanks for looking into this! I think the repo you linked is private though?
I also have my own robot-generated experiments and I believe I came to a similar conclusion of using an internal generic "policy" type to unify the internal implementations.
Here's a high-level overview of something similar to what you are proposing (LLM-assisted code disclosure):
Details
I think the main difference is that the result type is separate out into the public newtypes.
I have other prototypes and ideas, but all of them have different tradeoffs and costs. If I have time next week I'll clean everything up and put it in a repo.
On a separate note, I still believe it is unlikely for poisoning to be zero cost (and as you say, a
Poison<T>wrapper is not zero cost in general, for bothPoison<Mutex<T>andMutex<Poison<T>). But if @nia-e's experiments show otherwise, then there is reason to investigate a different API. So basically, I am waiting on those results to continue work on this.mitsuhiko commentedon Nov 26, 2026
@connortsui20 sorry for that. I opened it up now. It has all that validation in it.