Fix two startup races found by CI on Linux (refs #9 #14)
ci / test (postgres) (push) Successful in 2m31s
ci / test (sqlite) (push) Successful in 2m8s
ci / image (push) Successful in 59s
ci / test (postgres) (pull_request) Successful in 2m23s
ci / test (sqlite) (pull_request) Successful in 2m4s
ci / image (pull_request) Successful in 9s

An error-page choice released the startup loop before resetting the
services, so the loop could start the application and then lose its
services while reporting ready. The reset now happens while the loop is
still held, and moves the status back to Starting.

A SQLite file could vanish between the existence check and the open;
with create allowed, the open would put an empty database in its place.
Once the file has been seen, connections open read-write only and fail
on a missing file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-10 12:51:33 +01:00
1 parent f9d71559e8
commit 101dcbb152
4 files changed
+25 -3

No files matched your search

+4
View File
@@ -77,6 +77,10 @@ La Fase 1 costruisce le fondamenta: piattaforma, database su due motori, accesso
- L'identità si controlla prima di migrare. Un database che appartiene a un'altra installazione non viene toccato. Uno vuoto resta vuoto, quindi accoglie anche un salvataggio con uno schema più vecchio.
- Dopo un ripristino, un'importazione o "usa questo database", il bot riparte con "Ferma tutto" acceso (SPEC 15.1), qualunque cosa dicesse l'archivio.
- Un archivio rovinato dà un messaggio sulla pagina, non un errore del server.
- La scelta azzera lo stato dell'applicazione prima di lasciare ripartire il ciclo d'avvio. Prima lo faceva dopo, e il ciclo poteva ripartire nel frattempo: l'applicazione risultava pronta ma senza servizi. Il difetto è emerso nella CI su Linux.
- **File SQLite che sparisce durante l'apertura.**
- Controllare che il file esista non basta: può sparire tra il controllo e l'apertura, e un'apertura che può creare il file metterebbe al suo posto un database vuoto.
- Dopo la prima apertura riuscita, il file si apre solo se c'è già.
- **Pagine che leggono prima di disegnare.**
- Con il rendering statico Blazor disegna una prima volta mentre `OnInitializedAsync` aspetta ancora.
- Con SQLite le letture finiscono subito e il difetto non si vede. Con PostgreSQL la pagina si rompe.
+5 -1
View File
@@ -363,11 +363,15 @@ internal sealed partial class AnubisHost(
SetStatus(HostMode.Ready, null);
}
/// <summary>After an error-page choice changed the database: forget the services so the next step starts over.</summary>
/// <summary>
/// After an error-page choice changed the database: forget the services so the next step starts over. Called while
/// the choice still holds <see cref="ExclusiveAsync"/>.
/// </summary>
internal async Task ResetAsync()
{
await StopSchedulerAsync().ConfigureAwait(false);
_services = null;
SetStatus(HostMode.Starting, null);
Wake();
}
+4 -1
View File
@@ -371,8 +371,11 @@ internal static partial class Forms
}
LogChoice(logger, choice);
exclusive.Dispose();
// Reset while the loop is still held: released first, it could start the application again before the reset
// and then lose the services it had just built while showing "ready".
await host.ResetAsync().ConfigureAwait(false);
exclusive.Dispose();
await WaitBrieflyAsync(host).ConfigureAwait(false);
return Results.Redirect("/");
}
+12 -1
View File
@@ -16,6 +16,9 @@ public sealed class Database : IAsyncDisposable
{
private readonly SemaphoreSlim _writer = new(1, 1);
private readonly string _connectionString;
// SQLite once the file has been seen: the same place, but never created again.
private readonly string? _existingConnectionString;
private readonly NpgsqlDataSource? _postgres;
private volatile bool _sqliteSeen;
@@ -24,6 +27,11 @@ public sealed class Database : IAsyncDisposable
Engine = engine;
SqlitePath = sqlitePath is null ? null : Path.GetFullPath(sqlitePath);
_connectionString = connectionString;
if (engine == DbEngine.Sqlite)
{
_existingConnectionString = new SqliteConnectionStringBuilder(connectionString) { Mode = SqliteOpenMode.ReadWrite }.ToString();
}
_postgres = postgres;
Description = description;
}
@@ -123,7 +131,9 @@ public sealed class Database : IAsyncDisposable
throw new FileNotFoundException("Il file del database SQLite non c'è più: " + SqlitePath);
}
var connection = new SqliteConnection(_connectionString);
// Checking first is not enough: the file can go between the check and the open, and an open allowed to create
// would put an empty database in its place. Once seen, the open itself refuses a missing file.
var connection = new SqliteConnection(_sqliteSeen ? _existingConnectionString : _connectionString);
try
{
await connection.OpenAsync(cancellationToken).ConfigureAwait(false);
@@ -219,6 +229,7 @@ public sealed class Database : IAsyncDisposable
else
{
SqliteConnection.ClearPool(new SqliteConnection(_connectionString));
SqliteConnection.ClearPool(new SqliteConnection(_existingConnectionString));
}
_writer.Dispose();