Skip to content
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

Panic from debug assertion when calling RwLockWriteGuard::downgrade_to_upgradeable #169

Open
an-owl opened this issue Nov 20, 2024 · 1 comment

Comments

@an-owl
Copy link

an-owl commented Nov 20, 2024

RwLock::try_upgradeable_read() is

    pub fn try_upgradeable_read(&self) -> Option<RwLockUpgradableGuard<T, R>> {
        if self.lock.fetch_or(UPGRADED, Ordering::Acquire) & (WRITER | UPGRADED) == 0 {
            Some(RwLockUpgradableGuard {
                phantom: PhantomData,
                inner: self,
                data: unsafe { &*self.data.get() },
            })
        } else {
            // We can't unflip the UPGRADED bit back just yet as there is another upgradeable or write lock.
            // When they unlock, they will clear the bit.
            None
        }
    }

RwLockUpgradableGuard::try_upgrade_internal() contains compare_exchange( &inner.lock, UPGRADED, WRITER, Ordering::Acquire, Ordering::Relaxed,strong ) which clears UPGRADED and sets WRITER.

If try_upgrade_internal() is called then UPGRADED is cleared and WRITER is set. If try_upgradeable_read() is called on another thread this will set UPGRADED and return None, as it mentions in the fail branch it cant clear UPGRADED and the owner of the lock must clear it. However a number of debug assertions particularly in RwLockWriteGuard::downgrade_to_upgradeable and RwLockUpgradableGuard::drop() will panic in this event, even though RwLockWriteGuard::downgrade_to_upgradeable clears both UPGRADED and WRITER

@zesterer
Copy link
Collaborator

Thanks for the report, I'll try to look over this in more detail soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants