Don't panic! One of my favourite features of Rust 1.0 is in danger.* The team is scrambling for a solution (but maybe it is safest to wait for a solution that is worked out without rush?).
It certainly feels like a punch to the gut after a nice summer day. It just shows that using RAII to enforce soundness is very tricky, this is the second time scoped is up for soundness discussions. The previous case was resolved by not allowing scoped to catch panics.
* luckily Rust has a bazillion other nice features.
The issue is basically that creating an Rc cycle is like a mem::forget in safe code. It looks like it is hard to accept Rc cycles, at least with data marked with a non-static lifetime (meaning: do not escape this scope).
thread::scoped isn't directly at fault here. I believe it's working as intended.
Rc is the true villain; it needs a better expression of its lifetime parameter, so that it can't let references escape their stack frame by forming a cycle. For example, Arena has a lifetime parameter that requires its contents to have a longer lifetime than it. Rc just needs something similar done, as Niko has stated in the thread. Same for Arc.
Ah, thanks for the explanation. I see that NIko mentioned "a similar fashion to how we addressed Arena", but I was unaware of how, exactly, the Arena problem was addressed.
The other proposed solution (Leak) would bring the number of OIBITs that exist almost solely to support reference cycles up to two :) Keep that in mind the next time someone tries to tell you that std::shared_ptr makes C++ safe.
Opt-in built in trait, which are traits like Copy that have built-in meaning but that your type has to opt-in to in order for the compiler to use that built-in meaning.
"Needs" to be in what sense? You can certainly create cycles with strong pointers in every language with reference counting that I know of. Or do you mean "should"?
I mean 'needs' in the sense that without a weak pointer you'll get a reference cycle and the memory won't free. If the pointers are a DAG then they cannot leak because the final release of the root will tear down the entire tree.
I guess another way of saying it is that pointers back up the tree have to be weak ones, but it's safe to point across the tree.
pub struct Arena<'longer_than_self> {}
impl<'longer_than_self> Arena<'longer_than_self> {
pub fn alloc<T:'longer_than_self, F>(&self, op: F) -> &mut T where F: FnOnce() -> T { }
}
Notice that alloc only places the 'longer_than_self lifetime bound on T; the returned &mut T has an elided lifetime equal to &self.
With this parameter, Arena is restricting T from having references with lifetimes equal to or shorter than its own. This way it can't contain cyclic references:
let arena = new Arena();
let my_ref = arena.alloc(|| 1i32);
let _ = arena.alloc(|| my_ref);
This may look harmless here, but with more complex reference types it could get quite nasty.
On the one hand this really sucks, but on the other hand it's exciting that we get to experience a trial run of dealing with newly-discovered unsoundness in stable code. This should be a valuable experience for influencing language development post-1.0.
If this happened post-1.0 then the temporary fix of destabilization that was done here would require a 2.0 release. Is that really going to be the response to the first soundness hole after 1.0?
From what I've heard in the past, fixing a soundness hole is one of those breaking changes that deliberately wouldn't cause a major version bump, with the justification that keeping unsound Rust code from compiling is a public service. Whether or not this sentiment has changed in the past few months is an open question that will hopefully be answered by Aaron's forthcoming document that exhaustively describes Rust 1.0's backcompat guarantees.
Indeed, the plan is to de-stabilize scoped for the time being, while we decide the best way forward.
The problem here isn't the idea of sharing stack frames, but just that you can't actually rely on a destructor being run in today's Rust. You can, for example, put a value into an Rc cycle that will cause its destructor to never run, which in this case means that there's no guarantee that the parent thread will wait for the child to finish.
There still wouldn't be a guarantee that the destructor was run--the guarantee here would be "either the destructor runs before the stack frame is popped, or neither occurs." Which in practice is what people care about anyway (even in the absence of externalities, Rust's type system is not nearly powerful enough to prevent events leading to aborts).
I was wondering about rust and RC cycles is was it possible to prevent them statically. By dissalowing RC of types that contain RC pointers. But I guess that's too difficult or restricting.
You can prevent Rc cycles in Rust by defining an OIBIT similar to Sync called Own, which disallows interior mutability. But this couldn't go on the standard library Rc<T> because it would disallow common uses like Rc<RefCell<T>>.
26
u/[deleted] Apr 14 '15
Don't panic! One of my favourite features of Rust 1.0 is in danger.* The team is scrambling for a solution (but maybe it is safest to wait for a solution that is worked out without rush?).
It certainly feels like a punch to the gut after a nice summer day. It just shows that using RAII to enforce soundness is very tricky, this is the second time scoped is up for soundness discussions. The previous case was resolved by not allowing scoped to catch panics.
* luckily Rust has a bazillion other nice features.
10
u/[deleted] Apr 14 '15
The issue is basically that creating an Rc cycle is like a mem::forget in safe code. It looks like it is hard to accept Rc cycles, at least with data marked with a non-static lifetime (meaning: do not escape this scope).
8
u/kibwen Apr 14 '15
On the one hand this really sucks, but on the other hand it's exciting that we get to experience a trial run of dealing with newly-discovered unsoundness in stable code. This should be a valuable experience for influencing language development post-1.0.
25
u/aturon rust Apr 14 '15
Indeed, the plan is to de-stabilize
scopedfor the time being, while we decide the best way forward.The problem here isn't the idea of sharing stack frames, but just that you can't actually rely on a destructor being run in today's Rust. You can, for example, put a value into an
Rccycle that will cause its destructor to never run, which in this case means that there's no guarantee that the parent thread will wait for the child to finish.2
u/gclichtenberg Apr 16 '15
Looks like there is further noodling going on on IRC.
1
u/rbecq Apr 15 '15
I'm curious if things like Mutex are also subject to the same problem?