If weakrefs are ever used with Series's inner Arc, Series._get_inner_mut() could start panicing #21004
Open
2 tasks done
Labels
bug
Something isn't working
needs triage
Awaiting prioritization by a maintainer
rust
Related to Rust Polars
Checks
Issue description
Consider the implementation of this function:
Arc::weak_count()
andArc::strong_count()
are internally doingload(Relaxed)
.The
expect()
can trigger a panic in the following scenario:Arc::get_mut()
.Arc::get_mut()
uses a privateArc::is_unique()
method that is correctly doing consistency, and so it figures out total refcount is 2 (1 strong + 1 week), returnsNone
, we panic onexpect()
.This is more likely to happen on ARM than x86-64 because of weaker memory consistency. And presumably it doesn't happen very often, but can happen.
In practice, I see no code in Polars that actually creates weak refs to the Arc. So this is merely theoretical at the moment. But it does mean there is an optimizations I don't think should be implemented since it would be unsound if someone started using weakrefs somewhere else in the codebase.
Potential solutions
Two
Arc::get_mut()
Unfortunately the borrow checker is overzealous and won't allow this, at least for now.
Refactor
Series
There is probably some structure within
Series
that would fix this, maybestruct Series(pub Option<Arc<...>>)
? This is a pretty pervasive change though, and seems kinda ugly given it's a workaround for a borrow checker limitation.Switch to
Arc
implementation that lacks weakrefs, e.g.triomphe::Arc
The
triomphe
create reimplementsArc
without weakrefs, and it has a publicArc::is_unique()
method. This would also be faster, which is helpful for list operations that useamortized_iter()
and therefore call_get_inner_mut()
for every single value in aSeries
.Insofar as weakrefs aren't used at all for this code path AFAICT, this shouldn't break anything, but it does mean extra dependency and non-backwards compatible API change. In an ideal world the inner Arc wouldn't be public, though...
Installed versions
Git as of Jan 30, 2025.
The text was updated successfully, but these errors were encountered: