fix(data): bound the SQLite write-lock wait

A transaction left open by mistake now fails other writers with a
transient SQLITE_BUSY after 60 s instead of hanging them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 10:27:52 +00:00
1 parent fb42423346
commit 2849b5062d
1 file changed
+10 -1
+10 -1
View File
@@ -36,6 +36,7 @@ public sealed class SqliteDatabase : IDatabase
internal const int BusyTimeoutMs = 5000;
private const string LogCategory = "Data.Sqlite";
private static readonly TimeSpan WriteLockTimeout = TimeSpan.FromSeconds(60);
private readonly string _filename;
private readonly bool _memory;
@@ -196,7 +197,15 @@ public sealed class SqliteDatabase : IDatabase
return ValueTask.CompletedTask;
}
internal Task AcquireWriteAsync(CancellationToken cancellationToken) => _writeLock.WaitAsync(cancellationToken);
/// <summary>
/// Queues for the write lock. The wait is bounded so a transaction left open by mistake surfaces as a transient
/// SQLITE_BUSY instead of hanging every writer; the bound is generous because bulk imports hold the lock for long.
/// </summary>
internal async Task AcquireWriteAsync(CancellationToken cancellationToken)
{
if (!await _writeLock.WaitAsync(WriteLockTimeout, cancellationToken).ConfigureAwait(false))
throw new SqliteException($"database is locked: no write lock within {WriteLockTimeout.TotalSeconds} s", SQLITE_BUSY);
}
internal void ReleaseWrite() => _writeLock.Release();