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

mutex.waitForUnlock() bug? #52

Closed
ricardojosegomezulmke opened this issue Feb 9, 2022 · 3 comments
Closed

mutex.waitForUnlock() bug? #52

ricardojosegomezulmke opened this issue Feb 9, 2022 · 3 comments
Labels

Comments

@ricardojosegomezulmke
Copy link

Not sure if this is a bug or if I'm using it incorrectly.

version: 0.3.2
node: v16.6.1
typescript: Version 4.5.5

const myMutex = new Mutex();

await myMutex.runExclusive( async () => {
await myAsyncFunc();
});

// now wait until unlocked ...
await myMutex.waitForUnlock();
if(myMutex.isLocked()) throw new Error('myMutex is locked'); // <== this throws the error all the time

// instead, this works fine
const releaser = await myMutex.acquire();
releaser();
if(myMutex.isLocked()) throw new Error('myMutex is locked); // <== never throws, it works

BTW, awesome library!

@DirtyHairy
Copy link
Owner

Sorry for the late reply. Indeed, you're right, I checked the code, if there are more tasks blocked in the queue, then the semaphore will have decremented again before the waiter is resolved. This will still happen at the correct time (after the previous task has released the mutex and before the next task has acquired it, but this is not reflected by isLocked() --- this only evaluates to true if no more tasks are waiting to acquire the mutex.

I'll add a fix in the next release.

@nklhtv
Copy link

nklhtv commented Apr 6, 2022

i am facing the same issue
what i do for now is replacing
await mutex.waitForUnlock();
with
await mutex.acquire().then((release) => release());
....
awesome library btw!!!

@DirtyHairy
Copy link
Owner

Fixed in 0.4.0

DirtyHairy added a commit that referenced this issue Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants