fix(data): close concurrency and cleanup gaps found in review
- SQLite restore holds every pool slot instead of the write lock first, removing a lock-order deadlock with sessions that begin to write. - PostgreSQL sessions always return their connection on dispose, a rollback while disposing never masks the original exception, and a busy session keeps its transaction until it is discarded. - The pool disposes a connection whose health check the caller cancels. - Listener commands keep their lock until the server answers and never tear a write with the caller's token; closing and pending no longer race. - Tag replacement locks the sequence and the tag row; unused-tag cleanup locks candidates and re-checks them with a fresh snapshot; render counts and stats refreshes lock the sequence row on PostgreSQL. - Sequence upserts keep rendered_count, which renders maintain. - AuthenticationOk after an unfinished SCRAM exchange is rejected. - Oversized read buffers are released; failed backups leave no .tmp; a zero idle lifetime is rejected. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
17 files changed
+240
-61
No files matched your search
@@ -42,6 +42,7 @@ public sealed class SqliteDatabase : IDatabase
|
||||
private readonly int _maxConnections;
|
||||
private readonly SemaphoreSlim _slots;
|
||||
private readonly SemaphoreSlim _writeLock = new(1, 1);
|
||||
private readonly SemaphoreSlim _restoreGate = new(1, 1);
|
||||
private readonly Stack<SqliteConnection> _idle = new();
|
||||
private readonly Lock _gate = new();
|
||||
private bool _disposed;
|
||||
@@ -126,9 +127,17 @@ public sealed class SqliteDatabase : IDatabase
|
||||
await Task.Delay(2, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
var temp = target + ".tmp";
|
||||
await using (var session = (SqliteSession)await OpenAsync(cancellationToken).ConfigureAwait(false))
|
||||
await Task.Run(() => session.VacuumInto(temp, cancellationToken), cancellationToken).ConfigureAwait(false);
|
||||
File.Move(temp, target);
|
||||
try
|
||||
{
|
||||
await using (var session = (SqliteSession)await OpenAsync(cancellationToken).ConfigureAwait(false))
|
||||
await Task.Run(() => session.VacuumInto(temp, cancellationToken), cancellationToken).ConfigureAwait(false);
|
||||
File.Move(temp, target);
|
||||
}
|
||||
catch
|
||||
{
|
||||
File.Delete(temp);
|
||||
throw;
|
||||
}
|
||||
|
||||
var pattern = new Regex("^" + Regex.Escape(stem) + @"-\d{8}-\d{6}-\d{3}\.db$", RegexOptions.CultureInvariant);
|
||||
var backups = Directory.EnumerateFiles(directory)
|
||||
@@ -142,7 +151,8 @@ public sealed class SqliteDatabase : IDatabase
|
||||
|
||||
/// <summary>
|
||||
/// Replaces the database file with a backup after checking it with <c>PRAGMA quick_check</c>. Waits until
|
||||
/// every session of this instance is returned; no other process may have the file open.
|
||||
/// every session of this instance is returned (a caller must not hold one meanwhile); no other process may
|
||||
/// have the file open.
|
||||
/// </summary>
|
||||
public async Task RestoreAsync(string backupPath, CancellationToken cancellationToken = default)
|
||||
{
|
||||
@@ -150,7 +160,8 @@ public sealed class SqliteDatabase : IDatabase
|
||||
if (!File.Exists(backupPath)) throw new FileNotFoundException("Backup file not found.", backupPath);
|
||||
Validate(backupPath);
|
||||
|
||||
await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
// Holding every pool slot excludes all sessions, and with them the write lock, which only sessions take.
|
||||
await _restoreGate.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
var taken = 0;
|
||||
try
|
||||
{
|
||||
@@ -170,7 +181,7 @@ public sealed class SqliteDatabase : IDatabase
|
||||
finally
|
||||
{
|
||||
if (taken > 0) _slots.Release(taken);
|
||||
_writeLock.Release();
|
||||
_restoreGate.Release();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user