-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Clarify use of unsafe
in extra::arc
#9251
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
Comments
Perhaps access should fail when it is called by the same task twice? It would be nice if people didn't have to use unsafe blocks just to get normal stuff done. |
Nested locks in the same task aren't the only way you can run into deadlock: extern mod extra;
use std::cell::Cell;
use extra::arc::MutexArc;
use extra::comm::DuplexStream;
fn main() {
let arc1 = MutexArc::new(());
let arc2 = MutexArc::new(());
let (stream1, stream2) = DuplexStream();
let arc11 = Cell::new(arc1.clone());
let arc21 = Cell::new(arc2.clone());
let stream1 = Cell::new(stream1);
do spawn {
let arc1 = arc11.take();
let arc2 = arc21.take();
let stream = stream1.take();
do arc1.access |_| {
stream.send(());
stream.recv();
do arc2.access |_| {}
}
}
let arc12 = Cell::new(arc1);
let arc22 = Cell::new(arc2);
let stream2 = Cell::new(stream2);
do spawn {
let arc1 = arc12.take();
let arc2 = arc22.take();
let stream = stream2.take();
do arc2.access |_| {
stream.send(());
stream.recv();
do arc1.access |_| {}
}
}
} |
I am aware of that, but it seems to me this particular error would be easy and cheap to catch, so if we aren't doing that yet, we probably should. |
Consensus in the past has been that deadlock is not unsafe (#2821). You can deadlock with pipes too, with just two lines of code. The non- Regarding "cheap and easy to catch" - I don't believe we should try to fail in the single-task deadlock case unless we can reliably fail in all deadlock cases (which is not out of the question but requires scheduler support, see #7889). A user who is used to seeing a failure message from their previous attempts which had deadlocked would be all the more confused when they find their cross-task deadlocking code does not fail. |
Is the possibility of memory leakage really considered unsafe? |
Neither |
We currently don't officially consider leaks to be |
The box annihilator is not a good solution to rc reference cycles, but it is at least a solution. Cycles of MutexArcs will produce valgrind errors. |
|
improve [`match_same_arms`] messages, enable rustfix test closes: rust-lang#9251 don't worry about the commit size, most of them are generated --- changelog: improve [`match_same_arms`] lint messages
extra::arc::MutexArc
contains two sets of methods to access the Arc's wrapped object.unsafe_access
andunsafe_access_cond
are tagged unsafe as "it is possible to construct a circular reference among multiple Arcs by mutating the underlying data. This creates potential for deadlock, but worse, this will guarantee a memory leak of all involved Arcs." There are alsoaccess
andaccess_cond
methods defined when the inner type is freezable as that guarantees the type can't contain aMutexArc
. However, it's still trivially easy to deadlock using the safe methods:It seems that the consensus in #2821 was that possibility of deadlock is inherently unsafe, so should
access
andaccess_cond
be removed?cc @bblum
The text was updated successfully, but these errors were encountered: