-
Notifications
You must be signed in to change notification settings - Fork 760
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
Fix potential panics caused by Garbage Collector #855
Conversation
Really good catch, thank you!
I think it's OK, though it's really difficult to test. Maybe it's sufficient to check the |
I'm trying to craft something with a method that blocks while borrowing, and a background thread that calls the GC, but I have no luck currently. I can confirm this test works on the codebase I'm using but since the test case (which comes from the CPython test suite) is very cryptic I can't port it as is. |
Looks good to me, thanks 👍 I think that doing nothing if already borrowed is probably fine for I think we might have to look carefully in the future at |
Okay, I have a test case where I'm manually invoking the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome 💯 , thank you!
let ty = TraversableClass::type_object().as_ref(py).as_type_ptr(); | ||
let traverse = (*ty).tp_traverse.unwrap(); | ||
|
||
// create an object and check that traversing it works normally |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good test 👍
As discovered in #854, having a mutable borrow at any point on a
PyClass
that also implements the Garbage Collection protocol leads to unexpected panics, since the garbage collector can be run at any time, including when our class is already mutably borrowed.As a solution, the wrapper for the garbage collector now calls
PyCell::try_borrow
instead ofPyCell::borrow
, and simply returns if it could not acquire the cell. I'm not sure about the expected behaviour here though, feedback is welcome.Maintenance
cargo fmt
✅cargo clippy
and check there are no hard errors (There are a bunch of existing warnings; This is also checked by travis) ✅black .
. You can install black withpip install black
)