From b3262b80306100160409be664dfd963dd4ad4de6 Mon Sep 17 00:00:00 2001 From: zarzet <42882290+zarzet@users.noreply.github.com> Date: Sun, 22 Nov 2026 14:01:17 +0700 Subject: [PATCH] docs(history): state the real lock holder for the write retry The retry added for issue 948 explained itself with a claim that desktop connections ignore busy_timeout. That only shows in test runs on Linux; the same bundled SQLite waits the full timeout elsewhere. The lock that matters is a Library write transaction, which also write-locks the attached history.db until it commits. Also puts the dart: imports first and updates the helper comment that still spoke of three attempts. --- lib/services/history_database.dart | 22 ++++------------------ lib/services/sqlite_helpers.dart | 3 ++- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/lib/services/history_database.dart b/lib/services/history_database.dart index 7a14f4808..86d848c57 100644 --- a/lib/services/history_database.dart +++ b/lib/services/history_database.dart @@ -1,7 +1,6 @@ import 'dart:convert'; - -import 'package:flutter/foundation.dart' show visibleForTesting; import 'dart:io'; +import 'package:flutter/foundation.dart' show visibleForTesting; import 'package:sqflite/sqflite.dart'; import 'package:path_provider/path_provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -118,19 +117,8 @@ class HistoryDatabase { HistoryDatabase._init(); - /// History writes race the native backend's own history.db writers and - /// library merges on the attached Library connection. On desktop the - /// connection reports BUSY from BEGIN IMMEDIATE almost immediately even - /// with `PRAGMA busy_timeout` set, so the retry schedule must span several - /// seconds of real time to cover a competing merge. - /// - /// The bound is attempt-based, not a wall-clock deadline: the initial - /// attempt plus up to 7 retries, with scheduled sleeps totalling ~10.3s. - /// Each attempt can additionally wait inside SQLite up to the connection's - /// busy_timeout (5s in production), so under these assumptions a fully - /// contended write is modeled to take roughly 8 x busy_timeout + 10.3s - /// (~50s) before surfacing. Persistent locks still surface after the last - /// delay. + /// Library write transactions lock the attached history.db for their whole + /// length, so a history write retries BEGIN long enough to outlast a merge. static const List _writeRetryDelays = [ Duration(milliseconds: 100), Duration(milliseconds: 200), @@ -150,9 +138,7 @@ class HistoryDatabase { retryDelays: _writeRetryDelays, ); - /// Test seam: replaces the lazily opened connection with [db] so host - /// tests can drive real contention against a temporary file. Passing null - /// restores the production opener for the next access. + /// Test seam: uses [db] as the connection; null restores the real opener. @visibleForTesting static void debugReplaceDatabaseForTest(Database? db) { _database.reset(); diff --git a/lib/services/sqlite_helpers.dart b/lib/services/sqlite_helpers.dart index ce49035fa..dab1aa6e2 100644 --- a/lib/services/sqlite_helpers.dart +++ b/lib/services/sqlite_helpers.dart @@ -139,7 +139,8 @@ String normalizeLookupText(String? value) { /// Retry only lock contention while acquiring BEGIN, before the callback runs. /// History download writers can also lock transactions on an attached Library /// connection. Never replay a body or COMMIT failure: it may have side effects -/// or an already durable result. Persistent locks still surface after 3 attempts. +/// or an already durable result. Persistent locks still surface once +/// [retryDelays] run out (two quick retries by default). Future transactionWithBusyRetry( Database db, Future Function(Transaction txn) action, {