Re: [PATCH 3/3] rust: devres: fix race in Devres::drop()
From: Danilo Krummrich
Date: Thu Jun 12 2025 - 06:32:13 EST
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.
> > + // We could not remove the devres action, which means that it now runs concurrently,
> > + // hence signal that `self.0.data` has been revoked successfully.
> > + self.0.revoke.complete_all();
> > + }
> > + }
> > }
> > }
>