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

Fix Mutex.tryLock() non-linearizability #3781

Merged
merged 2 commits into from
Jun 21, 2023
Merged

Fix Mutex.tryLock() non-linearizability #3781

merged 2 commits into from
Jun 21, 2023

Conversation

ndkoval
Copy link
Member

@ndkoval ndkoval commented Jun 17, 2023

No description provided.

@ndkoval ndkoval requested a review from qwwdfsad June 17, 2023 20:21
@qwwdfsad
Copy link
Contributor

Fixes #3745

@@ -189,12 +191,14 @@ internal open class MutexImpl(locked: Boolean) : SemaphoreImpl(1, if (locked) 1
// locked by our owner.
if (owner != null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Previously, the following interleaving was possible:

tryLock(o)

  1. tryAcquire fails
  2. holdsLock also fails -- somebody else owns the lock
  3. isLocked returned false, yet between 2 and 3 there was a parallel execution that might have locked the mutex with the very same o

Copy link
Member Author

@ndkoval ndkoval Jun 20, 2023

Choose a reason for hiding this comment

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

No, in case "somebody else owns the lock", tryLock() is eligible to return false.

The problem is that:
(0) the mutex was locked with the same owner
(1) tryAcquire() fails
< another thread releases the mutex >
(2) holdsLock(o) fails, as the mutex is unlocked
< another thread acquires the mutex with owner o >
(3) isLocked returns true, and tryLock(o) returns false. However, tryLock(o) should throw an exception.

@qwwdfsad qwwdfsad merged commit 7b867fe into develop Jun 21, 2023
@qwwdfsad qwwdfsad deleted the fix-mutex-again branch June 21, 2023 08:48
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

Successfully merging this pull request may close these issues.

2 participants