Others / shredder

Garbage collected smart pointers for Rust
MIT License
266 stars 9 forks source link

Implementing Scan with a channel of Gc<T> #73

Closed awestlake87 closed 2 years ago

awestlake87 commented 2 years ago

I'm pretty new to garbage collectors internals, so sorry if this is a dumb question. I've got a bit of a dilemma in a project that I'm working on and I was wondering if I could get some clarification or advice.

Basically I've got a couple of garbage collected structures that store a channel's Sender<Gc<T>> and Receiver<Gc<T>>. I want the channel to send/recv garbage collected values but the channel I'm using doesn't allow me to access the internal buffer through the public API. It's also potentially problematic since the internal buffer has shared ownership via an Arc inside the Sender<Gc<T>>. So the question is whether or not it's safe to ignore the channel's buffer during a scan and what implications that has on the rest of the program (memory leaks? use-after-free?).

Is it sound to ignore the data in the channel during a scan? Do I have to write a custom channel that stores the buffer as some sort of GMutex<Vec<Gc<T>>>?

I've been reading through the docs and digging through the code a bit, but I'm not quite sure I understand the bigger picture well enough to know for sure if this is a problem or not. My intuition is that the Gc<T> values in the channel will just be treated as roots and function perfectly fine.

Others commented 2 years ago

Missing data in scan is always safe, as long as you fulfill the contract of the GcSafe trait. This is documented here: https://docs.rs/shredder/0.2.0/shredder/trait.Scan.html

However, it is an easy way to end up with a leak. Any missed data will be treated as rooted and still live. Whether this is a problem or not depends on your use case. If you don't have cycles that come back to the data holding the channel, things should eventually work themselves out

awestlake87 commented 2 years ago

Ah ok, I guess I missed that in the docs. I'll just have to test it thoroughly then! Thanks for the help!