drift icon indicating copy to clipboard operation
drift copied to clipboard

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

Open chaudharydeepanshu opened this issue 1 year ago • 4 comments

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.

chaudharydeepanshu avatar Sep 25 '24 09:09 chaudharydeepanshu

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).

simolus3 avatar Sep 25 '24 22:09 simolus3

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.

chaudharydeepanshu avatar Sep 26 '24 12:09 chaudharydeepanshu

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

simolus3 avatar Sep 30 '24 20:09 simolus3

Thanks for providing that! I'll add the worker and the dependency_override as suggested. I'll get back soon with the details after using these changes.

chaudharydeepanshu avatar Oct 07 '24 21:10 chaudharydeepanshu

I recently encountered the same problem. Has it been resolved yet?

miti-mio avatar Nov 04 '25 09:11 miti-mio

No updates yet. Would you be interested in trying out a worker that prints debug messages to help diagnose this issue @miti-mio?

simolus3 avatar Nov 04 '25 10:11 simolus3