Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

[UnityCache] indexedDB database could not be opened Error

Discussion in 'WebGL' started by Baraphor, Jul 18, 2018.

  1. Baraphor

    Baraphor

    Joined:
    Nov 16, 2016
    Posts:
    32
    Hello,

    What appears to be randomly we will get this error with all of our WebGL games, when this occurs the game will just progress to the end of the WebGL loading bar and then just get stuck and never load, which makes sense.

    Unfortunately I cannot reproduce it, it does happen randomly, nothing seems to stand out, expect that error message is always there.

    Has anyone figured out a way to fix this error?
     
  2. Picky-Salamander

    Picky-Salamander

    Joined:
    Apr 26, 2013
    Posts:
    27
    Hey,

    Yeah we've been having the issue as well. I just found out where this issue is (or where I think it is). It appears to be related to a issue in UnityLoader.js:

    Code (JavaScript):
    1. try {
    2.   var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
    3.   var openRequest = indexedDB.open(databaseName);
    4.   openRequest.onupgradeneeded = function (e) {
    5.     var objectStore = e.target.result.createObjectStore(store, { keyPath: "url" });
    6.     ["version", "company", "product", "updated", "revalidated", "accessed"].forEach(function (index) { objectStore.createIndex(index, index); });
    7.   };
    8.   openRequest.onsuccess = function (e) { initDatabase(e.target.result); };
    9.   openRequest.onerror = function () { initDatabase(null); };
    10.   setTimeout(openRequest.onerror, 1000);
    11. } catch (e) {
    12.   initDatabase(null);
    13. }
    The relevant line of code that is causing the issue is: setTimeout(openRequest.onerror, 1000). Basically, what seems to be happening is that the loader waits for IndexedDB to start up for 1 second then gives up. If the user has a slower machine this might happen. This will just cause PlayerPrefs to not save and data caching to fail. I think Unity fixed the data caching crash in the early 2018 releases, but the underlying issue still seems to exist.

    You can reliably reproduce the error if you put the below JavaScript on your page. It will delay IndexedDB opens by 5 seconds.

    Code (JavaScript):
    1. (function() {
    2.     var delegate = window.indexedDB.open;
    3.  
    4.     window.indexedDB.open = function(databaseName) {
    5.         var orgRequest = delegate.call(this, databaseName);
    6.  
    7.         var fakeRequest = {};
    8.  
    9.         orgRequest.onsuccess = function(e) {
    10.             fakeRequest.result = orgRequest.result;
    11.             setTimeout(function() {
    12.                 fakeRequest.onsuccess(e);
    13.             }, 5000);
    14.         };
    15.  
    16.         return fakeRequest;
    17.     };
    18. })();
    How to fix it (TLDR):

    I think you can avoid the issue by upgrading to the most recent version of Unity or turn off "data caching" in the publishing settings in WebGL's player settings (more info here).

    Either way though, the PlayerPrefs will stop working occasionally until Unity fixes the UnityLoader.js file. I'm pretty sure this issue still exists in Unity 2018.2. You could change UnityLoader.js to have a longer timeout, but that's kind of annoying to do (if you want to I can walk you through that process).
     
  3. Baraphor

    Baraphor

    Joined:
    Nov 16, 2016
    Posts:
    32
    Thanks,

    That is really useful.

    However for us it looks like the entire game fails to load, which is much worse than just not having player prefs, have you seen this happen as well?

    We are running our builds on Latest 2018.1, we have flipped to 2018.2 as we like to give a month for patches to come out.
     
  4. Picky-Salamander

    Picky-Salamander

    Joined:
    Apr 26, 2013
    Posts:
    27
    Yes. That's what I meant by the data cache crashing. I observed it in 2017.2, and I thought they fixed it in 2018.1, but maybe not? Try compiling with "data caching" turned off. This fixed the freeze in loading for us.

    If you want to force the IndexedDB failure so that you can test it, put the second piece of JavaScript from above into your web page.

    So this what I think is happening to you:
    1. IndexedDB takes too long to start and UnityLoader.js gives up loading IndexedDB.
    2. Data caching crashes, because IndexedDB didn't start.
    3. The loading routine freezes.
     
  5. Baraphor

    Baraphor

    Joined:
    Nov 16, 2016
    Posts:
    32
    Ok, Thanks I wasn't sure if that is what you meant, I am going to pass this along to our deployment dev and see if there is anything we can do about this.
     
  6. AlexHell

    AlexHell

    Joined:
    Oct 2, 2014
    Posts:
    90
    What is "Data caching crashes, because IndexedDB didn't start" ?
    My Firefox version have bugs with IndexedDB and this unity code, related to "data caching" option, log to console "indexedDB database could not be opened", simple, and not crashes. Why it should crash?