-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
SharedChan can be used to create cycles #10835
Comments
I don't think it's worth it. I would like to remove that bound on |
@pcwalton That seems extremely dangerous: won't it risk newbie programmers familiar with GC languages (i.e. all popular languages except C/C++/ObjC) using RcMut/RWARC as if it were a GC pointer and creating programs that leak gobs of memory? SharedChan is bit harder to misuse this way though, since while object graphs referencing each other with RWARC and calling methods on each other are equivalent to task graphs referencing each other with SharedChan and sending synchronous messages to each other, task graphs are more expensive and harder to implement. |
I feel like I'm still a little fuzzy on how this can acutally become a problem, can you post some code which exhibits the leak? |
@pcwalton: AFAICT, without the bound of |
@alexcrichton Something like this (not tested whether it compiles):
|
Or in fact, even simpler:
|
A solution to this: #10837 |
@bill-myers I don't see it as extremely dangerous. It's just something you have to learn. In Servo I found the We have never prevented memory leaks in Rust. You can always leak memory by, for example, having two tasks in an infinite loop ping ponging each other repeatedly. We just make it hard to leak memory in practice because we encourage use of data structures that don't leak (unique ownership, and in the future |
@thestinger Seems to me we should solve that problem then, perhaps by moving to Hans Boehm's suggestion of how to handle destructors in managed pointers (don't run any destructors on managed pointers until all objects are judged dead). The bound of |
@pcwalton: A cycle checking lint would have an easy opt-out unlike the |
@pcwalton |
@thestinger We tried that in very early versions of Rust. You end up having to be so conservative around |
@pcwalton: Ah, that makes sense. It's a bit sad that we won't be offering anything better than C or C++ in regards to this though. |
I'm fully convinced that it's better to leave off the It's sad that we can't do something better here, but doing nothing is better than crippling these types. |
I'm convinced that attempting to prevent this statically is indeed wrong. However, I think there needs to be a leak detector that detects if any SharedChans and similar are still alive at program termination and prints an error. This should result in catching most leaks in testing, although it won't help programs that cause leaks either with very low probability or on paths there are not covered by testing. The user can then use valgrind --track-origins to figure out where the leaked allocation happened. |
Closing, we have decided to remove the |
new lint: `drain_collect` Closes rust-lang#10818. This adds a new lint that looks for `.drain(..).collect()` and suggests replacing it with `mem::take`. changelog: [`drain_collect`]: new lint
Here is how one does it:
The Linux kernel has the exact same issue with file descriptor passing on Unix sockets and they solve it with an global in-kernel garbage collector for unix sockets (http://lxr.free-electrons.com/source/net/unix/garbage.c)
Rust can't do the GC approach easily because it can pass structures containing SharedChans in addition to just SharedChan like Linux.
However, we can prevent cycles just like we do for RWARC: by adding a Freeze bound to SharedChan's T and making sure SharedChan is no_freeze.
Should this be done?
If not, why shouldn't the Freeze be removed from RWARC too, since they are effectively equivalent functionality? (method calls on RWARC-protected objects are equivalent to sending a message via SharedChan to a task that calls the corresponding method)
The text was updated successfully, but these errors were encountered: