diff --git a/docs/adr/0007-scelte-fase-1.md b/docs/adr/0007-scelte-fase-1.md index ec63751..0157106 100644 --- a/docs/adr/0007-scelte-fase-1.md +++ b/docs/adr/0007-scelte-fase-1.md @@ -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. diff --git a/src/Anubis.Server/AnubisHost.cs b/src/Anubis.Server/AnubisHost.cs index ada7fda..f5a4490 100644 --- a/src/Anubis.Server/AnubisHost.cs +++ b/src/Anubis.Server/AnubisHost.cs @@ -363,11 +363,15 @@ internal sealed partial class AnubisHost( SetStatus(HostMode.Ready, null); } - /// After an error-page choice changed the database: forget the services so the next step starts over. + /// + /// After an error-page choice changed the database: forget the services so the next step starts over. Called while + /// the choice still holds . + /// internal async Task ResetAsync() { await StopSchedulerAsync().ConfigureAwait(false); _services = null; + SetStatus(HostMode.Starting, null); Wake(); } diff --git a/src/Anubis.Server/Forms.cs b/src/Anubis.Server/Forms.cs index 6bdd4a5..f4a1eeb 100644 --- a/src/Anubis.Server/Forms.cs +++ b/src/Anubis.Server/Forms.cs @@ -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("/"); } diff --git a/src/Anubis/Db/Database.cs b/src/Anubis/Db/Database.cs index 1131647..350bdfb 100644 --- a/src/Anubis/Db/Database.cs +++ b/src/Anubis/Db/Database.cs @@ -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();