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
@@ -81,6 +81,9 @@ public sealed class ValueConversionTests
|
||||
"INSERT INTO t (a, b, c) VALUES ($1, $2, $3) ON CONFLICT (a) DO UPDATE SET b = excluded.b, c = excluded.c",
|
||||
SqlDialect.Upsert("t", ["a", "b", "c"], 1));
|
||||
Assert.Equal("INSERT INTO t (a, b) VALUES ($1, $2) ON CONFLICT (a, b) DO NOTHING", SqlDialect.Upsert("t", ["a", "b"], 2));
|
||||
Assert.Equal(
|
||||
"INSERT INTO t (a, b, c) VALUES ($1, $2, $3) ON CONFLICT (a) DO UPDATE SET c = excluded.c",
|
||||
SqlDialect.Upsert("t", ["a", "b", "c"], 1, ["b"]));
|
||||
Assert.Equal("%50\\%\\_off\\\\%", SqlDialect.ContainsPattern("50%_off\\"));
|
||||
Assert.Equal("\"we\"\"ird\"", SqlDialect.QuoteIdentifier("we\"ird"));
|
||||
}
|
||||
|
||||
@@ -18,8 +18,11 @@ public sealed class PgProtocolUnitTests
|
||||
Assert.Equal(
|
||||
"c=biws,r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,p=dHzbZapWIk4jUhN+Ute9ytag9zjfMHgsqmmiz7AndVQ=",
|
||||
Encoding.UTF8.GetString(final));
|
||||
scram.VerifyServerFinal("v=6rriTRBi23WpRR/wtup+mMhUZUn/dB5nLTJRsjl95G4="u8);
|
||||
Assert.False(scram.IsServerVerified);
|
||||
Assert.Throws<System.Security.Authentication.AuthenticationException>(() => scram.VerifyServerFinal("v=AAAA"u8));
|
||||
Assert.False(scram.IsServerVerified);
|
||||
scram.VerifyServerFinal("v=6rriTRBi23WpRR/wtup+mMhUZUn/dB5nLTJRsjl95G4="u8);
|
||||
Assert.True(scram.IsServerVerified);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -271,6 +271,24 @@ public sealed class PgWireTests(TestContext context) : DatabaseTest(context)
|
||||
Assert.Equal(1, await session.ScalarAsync<int>("SELECT 1", [], Ct));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DisposingABusySessionInATransactionFreesItsPoolSlot()
|
||||
{
|
||||
var database = await OpenAsync("Maximum Pool Size=1;Timeout=3");
|
||||
var session = await database.OpenAsync(Ct);
|
||||
await session.ExecuteScriptAsync("CREATE TABLE t (id integer)", Ct);
|
||||
var tx = await session.BeginAsync(Ct);
|
||||
await session.ExecuteAsync("INSERT INTO t VALUES ($1)", [1], Ct);
|
||||
var rows = session.QueryAsync("SELECT generate_series(1, 1000)", [], Ct).GetAsyncEnumerator(Ct);
|
||||
Assert.True(await rows.MoveNextAsync());
|
||||
await tx.DisposeAsync();
|
||||
Assert.True(session.InTransaction, "a busy session cannot roll back yet");
|
||||
await session.DisposeAsync();
|
||||
|
||||
await using var next = await database.OpenAsync(Ct);
|
||||
Assert.Equal(0L, await next.ScalarAsync<long>("SELECT count(*) FROM t", [], Ct));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task NoticesAndScriptsDoNotDisturbTheSession()
|
||||
{
|
||||
|
||||
@@ -304,6 +304,8 @@ public abstract class CatalogRepositoryTests(TestContext context) : RepositoryTe
|
||||
await db.Renders.InsertAsync(first, Ct);
|
||||
await db.Renders.InsertAsync(second, Ct);
|
||||
Assert.Equal(2, (await db.Sequences.GetAsync(sequence.Id, Ct))!.RenderedCount);
|
||||
await db.Sequences.UpsertAsync(sequence with { Title = "Renamed from a stale copy" }, Ct);
|
||||
Assert.Equal(2, (await db.Sequences.GetAsync(sequence.Id, Ct))!.RenderedCount);
|
||||
var list = await db.Renders.ListBySequenceAsync(sequence.Id, Ct);
|
||||
Assert.SequenceEqual([second.Id, first.Id], list.Select(r => r.Id));
|
||||
Assert.Equal(second, list[0]);
|
||||
|
||||
@@ -117,6 +117,29 @@ public sealed class SqliteBindingTests(TestContext context) : DatabaseTest(conte
|
||||
Assert.Equal("original", await session.ScalarAsync<string>("SELECT v FROM b WHERE id = 1", [], Ct));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task RestoreWaitsForSessionsThatStillWrite()
|
||||
{
|
||||
var path = Path.Combine(Context.CreateTempDirectory(), "titano.db");
|
||||
var database = (SqliteDatabase)Own(DatabaseFactory.Open(DatabaseOptions.Sqlite(path)));
|
||||
await using (var setup = await database.OpenAsync(Ct))
|
||||
await setup.ExecuteScriptAsync("CREATE TABLE b (id INTEGER PRIMARY KEY)", Ct);
|
||||
var backup = await database.BackupAsync(Context.CreateTempDirectory(), cancellationToken: Ct);
|
||||
|
||||
var session = await database.OpenAsync(Ct);
|
||||
var restore = database.RestoreAsync(backup, Ct);
|
||||
await Task.Delay(100, Ct);
|
||||
Assert.False(restore.IsCompleted, "restore must wait for the open session");
|
||||
// The session can still take the write lock while the restore waits for its pool slot.
|
||||
await session.ExecuteAsync("INSERT INTO b VALUES ($1)", [1], Ct);
|
||||
await using (var tx = await session.BeginAsync(Ct)) await tx.CommitAsync(Ct);
|
||||
await session.DisposeAsync();
|
||||
await restore.WaitAsync(TimeSpan.FromSeconds(10), Ct);
|
||||
|
||||
await using var check = await database.OpenAsync(Ct);
|
||||
Assert.Equal(0L, await check.ScalarAsync<long>("SELECT count(*) FROM b", [], Ct));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task RestoreRefusesAFileThatIsNotADatabase()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user