fast-mutex icon indicating copy to clipboard operation
fast-mutex copied to clipboard

Bug? Or does chrome now do weird things?

Open justinbmeyer opened this issue 4 years ago • 5 comments

I'm trying to use this to build a web-locks polyfill. However, I'm seeing that two different browser tabs can acquire a lock at the same time. It appears they will both:

  • Set X
  • See Y is null
  • Set Y as themselves
  • And read X as themselves.

Obviously, that last set shouldn't be possible.

Any idea what's going on? I'm seeing this in chrome. Thanks!

justinbmeyer avatar Jan 09 '21 16:01 justinbmeyer

Putting another setTimeout here:

setTimeout(()=>{

   // if x was changed, another client is contending for an inner lock
   let lsX = this.getItem(x);
   if (lsX !== this.clientId) {

seems to fix things. It seems to let chrome actually see the other tab's write.

justinbmeyer avatar Jan 09 '21 16:01 justinbmeyer

Hello!

After playing with FastMutex for a while, I wasn't able to get it to work. I took some ideas from it and did a rewrite where that uses "only one setTimeout" at a time.

This seems to have made it work.

You can see the result here: https://github.com/bitovi/web-locks-polyfill.

I added a reference in the main function.

Thanks for building fast-mutex. It saved me at least 2 days of work.

justinbmeyer avatar Jan 10 '21 22:01 justinbmeyer

any updates on this issue? @chieffancypants

happy15 avatar May 24 '21 03:05 happy15

I'm trying to use this to build a web-locks polyfill. However, I'm seeing that two different browser tabs can acquire a lock at the same time. It appears they will both:

  • Set X
  • See Y is null
  • Set Y as themselves
  • And read X as themselves.

Obviously, that last set shouldn't be possible.

Any idea what's going on? I'm seeing this in chrome. Thanks!

Are these two tabs opened at the same time? @justinbmeyer

nieyuyao avatar May 24 '21 07:05 nieyuyao

Putting another setTimeout here:

setTimeout(()=>{

   // if x was changed, another client is contending for an inner lock
   let lsX = this.getItem(x);
   if (lsX !== this.clientId) {

seems to fix things. It seems to let chrome actually see the other tab's write.

is this because that localStorage needs time to sync data?

happy15 avatar May 24 '21 11:05 happy15