rollbar / rollbar-flutter

Rollbar for Dart and Flutter
https://docs.rollbar.com/docs/flutter
MIT License
21 stars 27 forks source link

SQLite throws seemingly random errors during usage and testing #42

Closed matux closed 2 years ago

matux commented 2 years ago

We're getting database is locked errors during testing and usage.

This seems to be happening because Dart doesn't support reading and writing concurrently and our usage of Isolates for threading in our library is not correct.

Currently we have a rudimentary hack in place in rollbar_infrastructure.dart:

static Future<bool> _process(dynamic message) async {
  // HACK: This delay helps to avoid occasional sqlite's "db locked" errors/exceptions.
  // Keep it until we figure out why sqlite has these errors randomly:
  await Future.delayed(Duration(milliseconds: 25));
  ...

This doesn't fix the issue and it should go, too, and a proper fix should be found.

Errors are similar to these:

test       Shell: [ERROR:flutter/runtime/dart_isolate.cc(1098)] Unhandled exception:
Shell: SqliteException(5): database is locked
Shell:   Causing statement:     DELETE FROM "destinations"
Shell:     WHERE NOT EXISTS (
Shell:       SELECT
Shell:       1
Shell:       FROM
Shell:       "payload_records"
Shell:       WHERE
Shell:       "payload_records.destination_id"
Shell:       = "destinations.id"
Shell:     )
Shell:
Shell: #0      DatabaseImpl.execute (package:sqlite3/src/impl/database.dart:129:9)
Shell: #1      DbDataAccess.deleteUnusedDestinations (package:rollbar_dart/src/payload_repository/db_data_access.dart:35:8)
Shell: #2      PayloadRepository.removeUnusedDestinations (package:rollbar_dart/src/payload_repository/payload_repository.dart:48:17)
Shell: #3      PayloadRepository.removeUnusedDestinationsAsync (package:rollbar_dart/src/payload_repository/payload_repository.dart:142:5)
Shell: #4      RollbarInfrastructure._processAllPendingRecords (package:rollbar_dart/src/rollbar_infrastructure.dart:163:18)
Shell: #5      RollbarInfrastructure._process (package:rollbar_dart/src/rollbar_infrastructure.dart:65:13)
Shell: <asynchronous suspension>
Shell: #6      RollbarInfrastructure._processWorkItemsInBackground (package:rollbar_dart/src/rollbar_infrastructure.dart:48:33)
Shell: <asynchronous suspension>
test      Shell: [ERROR:flutter/runtime/dart_isolate.cc(1098)] Unhandled exception:
Shell: SqliteException(6922): disk I/O error, disk I/O error (code 6922)
Shell:   Causing statement:     DELETE FROM "payload_records"
Shell:     WHERE "id" = ?
Shell:
Shell: #0      throwException (package:sqlite3/src/impl/exception.dart:34:3)
Shell: #1      PreparedStatementImpl._execute (package:sqlite3/src/impl/statement.dart:57:7)
Shell: #2      PreparedStatementImpl.execute (package:sqlite3/src/impl/statement.dart:34:5)
Shell: #3      DatabaseImpl.execute (package:sqlite3/src/impl/database.dart:134:14)
Shell: #4      DbDataAccess.deletePayloadRecordWithID (package:rollbar_dart/src/payload_repository/db_data_access.dart:59:8)
Shell: #5      DbDataAccess.deletePayloadRecord (package:rollbar_dart/src/payload_repository/db_data_access.dart:52:7)
Shell: #6      PayloadRepository.removePayloadRecord (package:rollbar_dart/src/payload_repository/payload_repository.dart:104:17)
Shell: #7      RollbarInfrastructure._processPendingRecord (package:rollbar_dart/src/rollbar_infrastructure.dart:137:12)
Shell: <asynchronous suspension>
Shell: #8      RollbarInfrastructure._processDestinationPendindRecords (package:rollbar_dart/src/rollbar_infrastructure.dart:120:12)
Shell: <asynchronous suspension>
Shell: #9      RollbarInfrastructure._processPayloadRecord (package:rollbar_dart/src/rollbar_infrastructure.dart:97:7)
Shell: <asynchronous suspension>
Shell: #10     RollbarInfrastructure._process (package:rollbar_dart/src/rollbar_infrastructure.dart:68:7)
Shell: <asynchronous suspension>
Shell: #11     RollbarInfrastructure._processWorkItemsInBackground (package:rollbar_dart/src/rollbar_infrastructure.dart:48:33)
Shell: <asynchronous suspension>