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

sync: add mem::forget to RwLockWriteGuard::downgrade. #2957

Merged
merged 2 commits into from Oct 23, 2020

Conversation

zaharidichev
Copy link
Contributor

Motivation

Currently when RwLockWriteGuard::downgrade the MAX_READS - 1
permits are added to the semaphore. When RwLockWriteGuard::drop
gets invoked however another MAX_READS permits are added. This
results in releasing more permits that were actually aquired when
downgrading a write to a read lock.

Solution

This is why we need to call
mem::forget on the RwLockWriteGuard in order to avoid
invoking the destructor.

Additionally doc test for downgrade was not correct as it can hang in the situation where after downgrading all available permits are assigned to the waiting writer before the additional reader gets the change to obtain a permit.

Fixes: #2941

Currently when `RwLockWriteGuard::downgrade` the `MAX_READS - 1`
permits are added to the semaphore. When `RwLockWriteGuard::drop`
gets invoked however another `MAX_READS` permits are added. This
results in releasing more permits that were actually aquired when
downgrading a write to a read lock. This is why we need to call
`mem::forget` on the `RwLockWriteGuard` in order to avoid
invoking the destructor.

Fixes: tokio-rs#2941
@Darksonn Darksonn added A-tokio Area: The main tokio crate C-enhancement Category: A PR with an enhancement or bugfix. M-sync Module: tokio/sync labels Oct 14, 2020
Copy link
Member

@carllerche carllerche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍 Looks like the loom test file is not included (so not running?).

tokio/src/sync/rwlock.rs Show resolved Hide resolved
@@ -76,3 +76,24 @@ fn concurrent_read_write() {
assert_eq!(10, *guard);
});
}
#[test]
fn downgrade() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not look like loom_rwlock.rs is being included here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not. I was not sure whether this is intentional or not. Will include

@carllerche
Copy link
Member

Also, maybe this PR should be submitted against 0.2 and then forward ported.

@zaharidichev zaharidichev changed the base branch from master to v0.2.x October 19, 2020 11:47
@zaharidichev zaharidichev changed the base branch from v0.2.x to master October 19, 2020 11:48
@carllerche
Copy link
Member

Sorry for the delay, I will come back to this soon. Working on v0.3.1 atm.

Copy link
Member

@carllerche carllerche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍

@carllerche
Copy link
Member

Btw, should we call out the deadlock in the docs and explain that it would deadlock because it is fair?

@zaharidichev
Copy link
Contributor Author

@carllerche yes we should. Just need to think of a succinct way to put it. :)

@carllerche carllerche merged commit e804f88 into tokio-rs:master Oct 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-enhancement Category: A PR with an enhancement or bugfix. M-sync Module: tokio/sync
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sync: RwLock::downgrade is not correct
3 participants