-
Notifications
You must be signed in to change notification settings - Fork 1.9k
MutexImpl.unlock()
hangs forever when locked through Semaphore.aquire()
#4026
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
Comments
It is, by the substitution principle (https://en.wikipedia.org/wiki/Liskov_substitution_principle) |
However, given that it requires a cast that relies on knowing the implementation details, it's a bug typically invisible to the user, so it's low-priority. When we merge #2045, this will get fixed automatically. |
@lukellmann could you please elaborate on how you've stumbled across this bug?
|
I read the source and realized that this could happen. I then constructed this code. So it doesn't impact any real code of mine. |
Example by Dmitry to better showcase the potential confusion:
|
The following code hangs forever on the call to
mutex.unlock()
:The reason seems to be that
MutexImpl
extendsSemaphoreImpl
butSemaphoreImpl.aquire()
bypasses the invariants ofMutexImpl.owner
:aquire
doesn't changeowner
butunlock
spin loops because it expects to see something different thanNO_OWNER
.Similar bugs might be possible with other combinations of
Mutex
andSemaphore
calls forMutexImpl
, I haven't checked yet.Admittedly, this is a bit contrived, so I'm not sure if this is really a bug or just a missuse of implementatiom details (the public
Mutex
interface doesn't extendSemaphore
). But if this is considered worth fixing I see two possible approaches:SemaphoreImpl
methods (through override etc.) so they don't breakMutexImpl
invariantsSemaphoreImpl
into a new abstract base type for bothMutex
andSemaphore
(that however doesn't implementSemaphore
directly) and a thin wrapper around it to actually implementSemaphore
- that way interacting with instances ofMutexImpl
through the methods ofSemaphore
won't be possible in the first placeThe text was updated successfully, but these errors were encountered: