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:
Claude committed 2026-10-02 10:27:19 +00:00
1 parent 8d9b04c713
commit fb42423346
17 files changed
+240 -61

No files matched your search

+10 -9
View File
@@ -27,6 +27,9 @@ public sealed class SqlDialect
/// <summary>Row lock for read-modify-write inside a transaction; empty on SQLite, whose <c>BEGIN IMMEDIATE</c> already holds the write lock.</summary>
public string ForUpdate => IsPostgres ? " FOR UPDATE" : string.Empty;
/// <summary>Like <see cref="ForUpdate"/>, but rows another transaction has locked are skipped instead of awaited.</summary>
public string ForUpdateSkipLocked => IsPostgres ? " FOR UPDATE SKIP LOCKED" : string.Empty;
/// <summary>Calendar year of a timestamp column (SQLite stores ISO-8601 text).</summary>
public string Year(string expression) => IsPostgres
? $"CAST(EXTRACT(YEAR FROM {expression}) AS integer)"
@@ -44,21 +47,19 @@ public sealed class SqlDialect
/// <summary>
/// <c>INSERT … VALUES ($1…$n) ON CONFLICT (keys) DO UPDATE SET …</c>; the first <paramref name="keyCount"/>
/// columns form the conflict target. The syntax is shared by SQLite ≥ 3.24 and PostgreSQL ≥ 9.5.
/// columns form the conflict target and <paramref name="insertOnly"/> columns keep their stored value on
/// conflict. The syntax is shared by SQLite ≥ 3.24 and PostgreSQL ≥ 9.5.
/// </summary>
public static string Upsert(string table, IReadOnlyList<string> columns, int keyCount)
public static string Upsert(string table, IReadOnlyList<string> columns, int keyCount, IReadOnlyCollection<string>? insertOnly = null)
{
ArgumentOutOfRangeException.ThrowIfLessThan(keyCount, 1);
ArgumentOutOfRangeException.ThrowIfGreaterThan(keyCount, columns.Count);
var sb = new StringBuilder("INSERT INTO ").Append(table).Append(" (").AppendJoin(", ", columns).Append(") VALUES (");
for (var i = 1; i <= columns.Count; i++) sb.Append(i > 1 ? ", $" : "$").Append(i);
sb.Append(") ON CONFLICT (");
for (var i = 0; i < keyCount; i++) sb.Append(i > 0 ? ", " : string.Empty).Append(columns[i]);
if (keyCount == columns.Count) return sb.Append(") DO NOTHING").ToString();
sb.Append(") DO UPDATE SET ");
for (var i = keyCount; i < columns.Count; i++)
sb.Append(i > keyCount ? ", " : string.Empty).Append(columns[i]).Append(" = excluded.").Append(columns[i]);
return sb.ToString();
sb.Append(") ON CONFLICT (").AppendJoin(", ", columns.Take(keyCount)).Append(')');
var updated = columns.Skip(keyCount).Where(c => insertOnly is null || !insertOnly.Contains(c)).ToList();
if (updated.Count == 0) return sb.Append(" DO NOTHING").ToString();
return sb.Append(" DO UPDATE SET ").AppendJoin(", ", updated.Select(c => $"{c} = excluded.{c}")).ToString();
}
/// <summary>Double-quotes an identifier (both dialects).</summary>