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

[WEB] await WasmDatabase.open is not responding and get stuck infinitely #3242

Open
chaudharydeepanshu opened this issue Sep 25, 2024 · 3 comments

Comments

@chaudharydeepanshu
Copy link

chaudharydeepanshu commented Sep 25, 2024

Describe the bug

Sometimes the (await WasmDatabase.open) never completes for our web app and just gets stuck on await. I don't know what could cause that as no errors were reported at that time. So, to catch such issues we added a timeout and from that, we were able to see it happening.

Here is the code I'm using:

/// Obtains a database connection for running drift on the web.
DatabaseConnection connect(DbType dbType) {
  return DatabaseConnection.delayed(
    Future(() async {
      try {
        final db = await WasmDatabase.open(
          databaseName: dbType.name,
          sqlite3Uri: Uri.parse('sqlite3.wasm'),
          driftWorkerUri: Uri.parse('drift_worker.js'),
        ).timeout(
          const Duration(seconds: 15),
          onTimeout: () {
            // Log error for stuck connection
            debugPrint('Database connection timed out for ${dbType.name}.');
            throw TimeoutException('Database connection stuck.');
          },
        );

        if (db.missingFeatures.isNotEmpty) {
          debugPrint('Using ${db.chosenImplementation} due to unsupported '
              'browser features: ${db.missingFeatures}');
        }

        return db.resolvedExecutor;
      } catch (e) {
        // Log error and rethrow for further handling
        debugPrint('Error while connecting to database: $e');
        throw Exception('Failed to connect to database: $e');
      }
    }),
  );
}

After the browser restarts it starts working again but cannot be fixed by a refresh on the web app. So, Do you have any idea what could have caused this?

Probable reproducible step: Trying multiple hot reloads on the web. (I was able to see it in debug mode also doing that)
Dependencies\Files:

drift: ^2.20.2
sqlite3_flutter_libs: ^0.5.24
sqlite3: ^2.4.6

sqlite3.wasm (2.4.6)
drift_worker.js (2.20.1)

Let me know if I can help you in any way to find the cause of this issue.

@simolus3
Copy link
Owner

Thanks for the report! I'll try to come up with a version of the worker/drift that logs interactions so that we can trace this down more easily.

Trying multiple hot reloads on the web. (I was able to see it in debug mode also doing that)

Did you only hit this in debug mode or also in release builds? Do you have a distribution of browsers where this happens (just wondering if it happens more often on some browsers/devices than others).

@chaudharydeepanshu
Copy link
Author

chaudharydeepanshu commented Sep 26, 2024

Hey, thank you for looking into it.

Thanks for the report! I'll try to come up with a version of the worker/drift that logs interactions so that we can trace this down more easily.

Yes, agreed. Let me know once you can give that.

Did you only hit this in debug mode or also in release builds? Do you have a distribution of browsers where this happens (just wondering if it happens more often on some browsers/devices than others).

I have hit this in both debug and deployed profile builds. A profile build would work as a release build in this case, right? I don't have "distribution of browsers" data, but it happened on the Edge in debug and Chrome in the deployed profile build.

@simolus3
Copy link
Owner

simolus3 commented Sep 30, 2024

Alright, here you go: https://storage.googleapis.com/simon-public-euw3/assets/drift/drift_worker_3242.js The worker is not minified and should print messages it receives, so hopefully that helps to track this down a bit further.

To also see what the main tab is receiving from the worker, can you add this to your pubspec?

dependency_overrides:
  drift:
    git:
      url: https://github.com/simolus3/drift.git
      ref: worker-debug
      path: drift

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

No branches or pull requests

2 participants