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

InvalidStateError: Failed to execute 'transaction' on 'IDBDatabase': The database connection is closing with ios safari #1139

Closed
Q8root opened this issue Oct 6, 2020 · 16 comments

Comments

@Q8root
Copy link

Q8root commented Oct 6, 2020

I am getting error on log when this get executed:-

import React, { useEffect } from "react";
import db from "../config/db";

useEffect(() => {
    db.reads
      .get(props.match.params.id)
      .then((data) => {})
      .catch((e) => {
        console.log(e);
      });
  }, []);
import Dexie from "dexie";
const db = new Dexie("bookDB");
db.version(2).stores({
  reads: "hash,token,sa,ss",
  books: "hash,token",
});

export default db;

I am getting many errors :-

InvalidStateError: Failed to execute 'transaction' on 'IDBDatabase': The database connection is closing.

UserAgent:-

Mozilla/5.0 (iPhone; CPU iPhone OS 13_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Mobile/15E148 Safari/604.1

I could not find steps that produce the following error, some iPhone's i've tested does not return any error.

@dfahlander
Copy link
Collaborator

dfahlander commented Oct 6, 2020

This happens when database is closing down when the component is mounting. The reason could be if the application at another place calls db.close(), or if the database is forcibly deleted in devtools, or if there is a newer version of the db loaded in another tab or window, so the db has to close down to resume an upgrade.

It could also be a iOS Safari related issue. Do you only see the issue on Safari or have you just tested it there?

@Q8root
Copy link
Author

Q8root commented Oct 6, 2020

This happens when database is closing down when the component is mounting. The reason could be if the application at another place calls db.close(), or if the database is forcibly deleted in devtools, or if there is a newer version of the db loaded in another tab or window, so the db has to close down to resume an upgrade.

It could also be a iOS Safari related issue. Do you only see the issue on Safari or have you just tested it there?

All the other components files using the same, import db then use .get or .put method, this issue, regarding the version upgade, currently we have version 2 since 4 months ago, and users who got this error are first time they visit our website.
This error only occurred with ios users.

@Q8root
Copy link
Author

Q8root commented Oct 7, 2020

20201007_114743
20201007_114721

Screenshot showing the user-agent that got the error

@trackedsupport
Copy link

This happens when database is closing down when the component is mounting. The reason could be if the application at another place calls db.close(), or if the database is forcibly deleted in devtools, or if there is a newer version of the db loaded in another tab or window, so the db has to close down to resume an upgrade.

It could also be a iOS Safari related issue. Do you only see the issue on Safari or have you just tested it there?

So how should we catch and fix this issue?

@dfahlander
Copy link
Collaborator

dfahlander commented May 14, 2021

So how should we catch and fix this issue?

Too little info about the cause of this issue to tell. Try make a repro.

  • Is the reason that your own application is closing down the db? Fix it in your app.
  • Is the reason that another tab/window is loading a newer version of the app, making the old close down? Listen to db.on('versionchange') to customize how your app should act when this happens.
  • Is the reason due to a bug in Safari? - File an issue in webkit bugzilla.

@trackedsupport
Copy link

I never close the dexie db.
I can't reproduce but I know it happened for someone using Firefox 88 on windows. Also I know that once I changed from storing blobs to storing ArrayBuffer that this issue happens a lot more frequently.
I have never changed versions of my db - so that should never be called right?

@dfahlander
Copy link
Collaborator

right. Pay attention to errors occurring before this error happens. Are you also getting quotaexceeded errors? I don't see the reason why the browser closes the db for anything else than an unexpected internal error, but i could be wrong. Would be interesting to know.

@trackedsupport
Copy link

I wasn’t getting quotaexceeded until I did the ArrayBuffer but still I’m get those error far less frequently.

The one Firefox user didn’t have any issues when saving blob without doing the ArrayBuffer. I wouldn’t know there was an issue until I saw only 15 bytes retrieved from the db.

@Q8root
Copy link
Author

Q8root commented May 15, 2021

So how should we catch and fix this issue?

Too little info about the cause of this issue to tell. Try make a repro.

  • Is the reason that your own application is closing down the db? Fix it in your app.
  • Is the reason that another tab/window is loading a newer version of the app, making the old close down? Listen to db.on('versionchange') to customize how your app should act when this happens.
  • Is the reason due to a bug in Safari? - File an issue in webkit bugzilla.

I begin to think that it's a bug in Safari, because i noticed not only indexedDB data get cleared from those users, also the localStorage get cleared.

If I'm setting the version 2 for more than one year ago, is it possible that user may still using an old version, also most of users having this issue, they are first time ever opened our website ?

@quolpr
Copy link

quolpr commented May 17, 2021

Yep, it is a safari bug - https://bugs.webkit.org/show_bug.cgi?id=197050 . It may happen when you switch the site between foreground and background. For now as one of the solution is to reload the app if such an error happens.

Here is the same issue at firebase - firebase/firebase-js-sdk#3495 . They fixed it with firebase/firebase-js-sdk#3535 . They reopen the DB connection if it was closed. @dfahlander could Dexie have such a feature too?

@dfahlander
Copy link
Collaborator

Thanks @quolpr! Yes, that would be great if we could workaround this. I posted an idea on how to fix it Based on mikekreeki's workaround but without having to test IDB state for every request.

@jcimoch
Copy link

jcimoch commented Jun 18, 2021

This is occurring on latest chrome randomly as well, I noticed that it happens more often when browser was put in the background for a while

@jeremad
Copy link

jeremad commented Feb 16, 2022

#1398

Might fix that?

@dfahlander
Copy link
Collaborator

#1398

Might fix that?

Yes, #1398 works around the exact same problem - catches exception thrown when executing db.transaction and reopens the connection. It's released in version 3.2.1.

@jeremad
Copy link

jeremad commented Feb 17, 2022

Thank you, I'll test test that!

@jeremad
Copy link

jeremad commented Feb 17, 2022

It seems indeed to workaround the issue pretty well! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants