Dexie.js icon indicating copy to clipboard operation
Dexie.js copied to clipboard

uniqueKeys() fails in Safari on empty DB

Open dholth opened this issue 5 years ago • 15 comments

Unhandled rejection: UnknownError: Unable to open cursor

Add a row: it works.

dholth avatar May 21 '20 14:05 dholth

Ok, thanks! We'd need a repro to verify this. Seems it would be easy to patch it in dexie. There still seems to be a few issues with Safari we'll need to patch.

For any voluntary user: A repro of this issue should be placed in a new unit test, in any of the existing modules such as tests-misc.js.

It would be important to know what actually triggers the error. Possible reasons:

  1. Database is empty
  2. Table is empty
  3. No read- or write requests where made to the database since current connection was opened.
  4. No write requests has been made since current connection was opened.

dfahlander avatar May 24 '20 12:05 dfahlander

Also, this issue should be filed in Webkit bugzilla

dfahlander avatar May 24 '20 12:05 dfahlander

This is a dup of #1030. Repro is available in #1030.

edwardxia avatar Jun 08 '20 17:06 edwardxia

Still happening

AlejandroAkbal avatar Mar 11 '24 16:03 AlejandroAkbal

I can't reproduce this in Safari Version 17.3.1 (19617.2.4.11.12). Repro link: https://jsfiddle.net/dfahlander/6rqyfag9/. @AlejandroAkbal Could you run the repro? What version of Safari do you use?

dfahlander avatar Mar 11 '24 20:03 dfahlander

Tried with Browserstack on Safari Version 15.6.1 (17613.3.9.1.16). The repro isn't hit there either. Output:

User agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6.1 Safari/605.1.15
Dexie: 3.2.6
Could NOT reproduce the issue on this browser

dfahlander avatar Mar 11 '24 20:03 dfahlander

I continued downgrading browser and had to go all the way down to Safari 13 to make the repro hit the bug: Safar Version 13.1.2 (15609.3.5.1.3).

Output:

User agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15
Dexie: 3.2.6
Issue Reproduced! UnknownError: Unable to open cursor
 UnknownError: Unable to open cursor

dfahlander avatar Mar 11 '24 20:03 dfahlander

Closing as the bug seem to be solved since Safari 14 (2020).

dfahlander avatar Mar 11 '24 20:03 dfahlander

I got the same error in Safari 17.6 (Mac OS Sonoma 14.6.1)

kushanksriraj avatar Aug 23 '24 09:08 kushanksriraj

I got the same error in Safari 17.6 (Mac OS Sonoma 14.6.1)

Thanks. Does it reproduce with this repro? If not, could you supply a repro for the case where it fails on Safari 17.6, Mac OS Sonoma 14.6.1.

dfahlander avatar Aug 23 '24 09:08 dfahlander

Screenshot 2024-08-23 at 2 43 55 PM

Yes

kushanksriraj avatar Aug 23 '24 09:08 kushanksriraj

Hi!

I've been integrating Dexie to use as a long duration cache and during testing we also noticed this bug. There seems to a difference on how Safari handles uniqueKeys on empty tables compared other browser.

I'm using uniqueKeys to make sure we don't store an excessive amount of data in some of our tables as they quickly can reach thousands of rows per unique key, and cleanup any excess and stale data if necessary. Our current workaround is simply checking if the table is empty, and skip cleanup based on uniqueKeys if it is.

This was tested on OSX 10.15.7 and Safari 17.3.

User agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.3 Safari/605.1.15
Dexie: 4.0.9
UnknownError: Unable to open cursor

The following code reproduces this as of latest Dexie 4.0.9 version:

<!DOCTYPE html>
<html>
  <!-- Include dexie.js -->
  <script src="https://npmcdn.com/dexie/dist/dexie.js"></script>

  <!-- Just a simple log function... -->
  <script>
    function log(txt) {
      document.getElementById("log").value += txt + "\n";
    }

    // Check that browser supports generator functions:
    try {
      eval("(function* (){})");
    } catch (e) {
      log(
        "This browser doesn't support generator functions. Must use a recent version of Chrome, Opera, Firefox or Edge."
      );
    }
  </script>

  <style>
    body {
      font-family: Arial;
      padding: 10px;
    }
    textarea {
      width: 100%;
      height: 50vh;
    }
  </style>

  <body>
    <!-- Some HTML for the log window -->
    <a href="http://dexie.org/docs/API-Reference" target="_new"
      >Dexie API Reference (new tab)</a
    >

    <h3>Log</h3>
    <textarea id="log"></textarea>
    <p id="safari-version" style="display: none">
      This browser denies indexedDB access from iframes.
      <a href="https://fiddle.jshell.net/dfahlander/6rqyfag9/show" target="_top"
        >Click here to open it directly without any iframe.</a
      >
    </p>
  </body>

  <script defer>
    var db = new Dexie("MyFriendDB3");
    db.version(1).stores({
      consumption: "a, b",
    });

    async function main() {
      log("User agent: " + navigator.userAgent);
      log("Dexie: " + Dexie.semVer);
      try {
        const uniqueKeys = await db.consumption.orderBy("a").uniqueKeys();
        log("Unique keys: " + JSON.stringify(uniqueKeys));
      } catch (error) {
        log(error);
      }

      await db.delete();
    }

    main().catch((e) => {
      if (e.inner) e = e.inner;
      if (e.name === "MissingAPIError") log("Couldn't find indexedDB API");
      else if (e.name == "SecurityError") {
        document.getElementById("log").style = "display:none";
        document.getElementById("safari-version").style = "display:";
      } else {
        log(e);
      }
    });
  </script>
</html>

LordXyroz avatar Nov 05 '24 09:11 LordXyroz

I'm not able to reproduce it with Lambdatest or on my own Safari browser but I believe you of course. Just interesting to understand the circumstances where it fails. If I could it would be easier to build a workaround in dexie and test it.

image

In your reports, the issue should be present in:

  • Safari 17.6 (Mac OS Sonoma 14.6.1)
  • Safari 17.3 (OSX 10.15.7)

But I cant reproduce it on any device in neither browserstack och lambdatest or on my laptop except on Safari 13 with Mac OS Catalina on Browserstack.

I'll reopen the issue and plan to do a workaround for this i dexie. If anyone finds some other circumstances that makes this repro succeed to reproduce the issue on a modern Safari, let me know.

dfahlander avatar Nov 05 '24 09:11 dfahlander

I'm not able to reproduce it with Lambdatest or on my own Safari browser but I believe you of course. Just interesting to understand the circumstances where it fails. If I could it would be easier to build a workaround in dexie and test it.

image In your reports, the issue should be present in:
  • Safari 17.6 (Mac OS Sonoma 14.6.1)
  • Safari 17.3 (OSX 10.15.7)

But I cant reproduce it on any device in neither browserstack och lambdatest or on my laptop except on Safari 13 with Mac OS Catalina on Browserstack.

I'll reopen the issue and plan to do a workaround for this i dexie. If anyone finds some other circumstances that makes this repro succeed to reproduce the issue on a modern Safari, let me know.

The weird part is that the same tester tried the jsfiddle and couldn't reproduce it. But when creating that simple html file and loading it in Safari he got the bug. Gotta love edge-case issues 😄

LordXyroz avatar Nov 05 '24 10:11 LordXyroz

The weird part is that the same tester tried the jsfiddle and couldn't reproduce it. But when creating that simple html file and loading it in Safari he got the bug. Gotta love edge-case issues 😄

Wow, thanks for that! I published the repro on https://dexie.org/test/dexie-issue-1052/repro-for-1052.html and could immediately reproduce it on my latest Safari Version 18.0.1 (19619.1.26.111.11, 19619) on Sonoma 14.6.1 (23G93).

dfahlander avatar Nov 05 '24 10:11 dfahlander