isomorphic-git / lightning-fs

A lean and fast 'fs' for the browser
MIT License
493 stars 48 forks source link

Uncaught (in promise) DOMException: LockManager.request: request() is not allowed in this context #114

Open Kreijstal opened 1 year ago

Kreijstal commented 1 year ago

In Firefox, see https://observablehq.com/@a10k/hello-lightning-fs-isomorphic-git Works perfectly in chrome but when creating the new filesystem, DOM refuses.

jcubic commented 1 year ago

promises functions as the name suggest are async, you can't execute a stat on the next line without await in front.

jcubic commented 1 year ago

Check this demo https://codepen.io/jcubic/pen/zYLdPxK?editors=0011 and you should not use @beta version.

jcubic commented 1 year ago

If you can create a demo that will clean after itself it would be great.

jcubic commented 1 year ago

The error itself looks like a problem with observablehq, if you can please create a self-contained code without any extra middleman.

jcubic commented 1 year ago

Also if error came from lightning-fs when creating FS please try to minimize the usage of libraries and Use only things to make reproducing the error possible. You don't even use isomorphic-git properly because there is no "master" branch in git repo.

Kreijstal commented 1 year ago

Yeah, didn't give you a minimal example, sorry about that.

This however works in chrome/edge. So it can't be a problem with observablehq. You can remove the rest of the code, it doesn't matter, the code errors as soon as you use new FS("something") When I have time I'll create a minimal working example outside observable. Observable will always await for thenables automatically between cells

Kreijstal commented 1 year ago

Check this demo https://codepen.io/jcubic/pen/zYLdPxK?editors=0011 and you should not use @beta version.

oh wow, your example breaks on my machine, same error image

Kreijstal commented 1 year ago

without plugins image on firefox 108.0.2 windows 10. image

Kreijstal commented 1 year ago

ok, managed to somehow reproduce it. This is based on https://github.com/isomorphic-git/lightning-fs/blob/b041900186c126691cddcfd0b355b417404f0c16/src/Mutex2.js#L31

new Promise((resolve, reject) => {
      const controller = new AbortController();
      setTimeout(() => {
        controller.abort();
        reject(new Error("Mutex timeout"));
      }, 3);
      navigator.locks.request(this._database + "_lock", {signal: controller.signal}, (lock) => {
        this._has = !!lock;
        resolve(!!lock);
        return new Promise((resolve2) => {
          console.log('aaa');
window.helpme=resolve2;
        });
      });
    });

firefox: image chrome: image

jcubic commented 1 year ago

But it works when I use the same code outside of CodePen.

This is my own playground that I work on and it works fine there:

https://p5.javascript.org.pl/motionless-age?template=none&base=base.js

Kreijstal commented 1 year ago

Yes! It works on your playground.., to my amazement. image It doesn't work on codepen, or observable. Interesting... It works in chrome tho. This is interesting. Maybe it's the eval context? I tried just using that snipped of code on codepen and your playground, breaks in codepen, works on your playground.

jcubic commented 1 year ago

I suspect that this may be something with Sandboxing, I don't use any protection on my playground. Maybe I will add some kind of Sandboxing later in the process. My application is a prototype.

Anyway, I contacted CodeSanbox support maybe they will know what is happening. If a lock can't be used in the Sandboxed environment then the library should protect against it, maybe disabling the lock, use something else, throwing a proper error, or maybe showing a warning.

jcubic commented 1 year ago

Another explanation is that this is a bug in Firefox. I don't see any information about Sandbox on MDN.

jcubic commented 1 year ago

I've contacted CodePen support they can reproduce the issue but don't have a solution. I will try to recreate the issue on sandboxed iframe.

Kreijstal commented 1 year ago

https://bugzilla.mozilla.org/show_bug.cgi?id=1798493 hmm, can we maybe detect this case and downgrade or at least give a meaningful error message