Re: [PATCH 3/3] rust: devres: fix race in Devres::drop()
From: Benno Lossin
Date: Thu Jun 12 2025 - 07:05:18 EST
On Thu Jun 12, 2025 at 12:31 PM CEST, Danilo Krummrich wrote:
> On Thu, Jun 12, 2025 at 10:13:29AM +0200, Benno Lossin wrote:
>> On Tue Jun 3, 2025 at 10:48 PM CEST, Danilo Krummrich wrote:
>> > impl<T> Drop for Devres<T> {
>> > fn drop(&mut self) {
>> > - DevresInner::remove_action(&self.0);
>> > + // SAFETY: When `drop` runs, it is guaranteed that nobody is accessing the revocable data
>> > + // anymore, hence it is safe not to wait for the grace period to finish.
>> > + if unsafe { self.revoke_nosync() } {
>> > + // We revoked `self.0.data` before the devres action did, hence try to remove it.
>> > + if !DevresInner::remove_action(&self.0) {
>>
>> Shouldn't this not be inverted? (ie 's/!//')
>>
>> Otherwise this will return `true`, get negated and we don't run the code
>> below and the `inner.data.revoke()` in `devres_callback` will return
>> `false` which will get negated and thus it will never return.
>
> DevresInner::remove_action() returns false it means that the devres action has
> already been taken from the list and will be run eventually, hence we have to
> complete the completion.
>
> If DevresInner::remove_action() returns true, it means that we removed the
> action from the list and the callback will never be exectuted, hence nothing to
> do.
Ahh I mixed up which call was which, I thought this was
`self.revoke_nosync()`...
---
Cheers,
Benno