diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-11-15 15:53:00 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-15 15:53:00 -0600 |
| commit | 7d67dc1d6a452812981d6a0965c3a2e07737b726 (patch) | |
| tree | 80bd0ab9aa930425d59a1d8de7886641a19e057b | |
| parent | d3302657a6f63c5bb28ceaabff6e7e00cb04835e (diff) | |
| parent | 880d65489d9153e2f5084ad1bc2064ed4fb76793 (diff) | |
| download | slskd-7d67dc1d6a452812981d6a0965c3a2e07737b726.tar.gz | |
Merge pull request #1925 from slskd/volatile
Fix volatile mode by holding connections open
| -rw-r--r-- | src/slskd/Program.cs | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 879f76b7..57d967f0 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -57,6 +57,7 @@ namespace slskd using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Server.Kestrel.Core; + using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -278,6 +279,7 @@ namespace slskd private static Mutex Mutex { get; set; } private static IDisposable DotNetRuntimeStats { get; set; } private static VolatileOverlayConfigurationSource<OptionsOverlay> VolatileOverlayConfigurationSource { get; set; } = new VolatileOverlayConfigurationSource<OptionsOverlay>(); + private static List<SqliteConnection> VolatileDatabaseKeepaliveConnections { get; } = []; [Argument('g', "generate-cert", "generate X509 certificate and password for HTTPs")] private static bool GenerateCertificate { get; set; } @@ -747,7 +749,7 @@ namespace slskd var pooling = OptionsAtStartup.Flags.NoSqlitePooling ? "False" : "True"; // don't invert and ToString this it is confusing var connStr = OptionsAtStartup.Flags.Volatile - ? $"Data Source=file:{database}?mode=memory;Pooling={pooling};" + ? $"Data Source=file:{database}?mode=memory&cache=shared;Pooling={pooling};" : $"Data Source={Path.Combine(DataDirectory, $"{database}.db")};Pooling={pooling}"; return new KeyValuePair<Database, ConnectionString>(database, connStr); @@ -1526,6 +1528,17 @@ namespace slskd } }); + // in-memory databases are destroyed when the last connection to them closes. hold one open for the + // lifetime of the application so the data (and schema) survive between operations. this must be + // opened before EnsureCreated(), otherwise the schema is created in a database that is destroyed + // as soon as EnsureCreated() closes its connection + if (OptionsAtStartup.Flags.Volatile) + { + var keepalive = new SqliteConnection(connectionString); + keepalive.Open(); + VolatileDatabaseKeepaliveConnections.Add(keepalive); + } + /* instantiate the DbContext and make sure it is created */ |
