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:
1 file changed
+10
-1
@@ -36,6 +36,7 @@ public sealed class SqliteDatabase : IDatabase
|
|||||||
|
|
||||||
internal const int BusyTimeoutMs = 5000;
|
internal const int BusyTimeoutMs = 5000;
|
||||||
private const string LogCategory = "Data.Sqlite";
|
private const string LogCategory = "Data.Sqlite";
|
||||||
|
private static readonly TimeSpan WriteLockTimeout = TimeSpan.FromSeconds(60);
|
||||||
|
|
||||||
private readonly string _filename;
|
private readonly string _filename;
|
||||||
private readonly bool _memory;
|
private readonly bool _memory;
|
||||||
@@ -196,7 +197,15 @@ public sealed class SqliteDatabase : IDatabase
|
|||||||
return ValueTask.CompletedTask;
|
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();
|
internal void ReleaseWrite() => _writeLock.Release();
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user