diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-08-19 09:42:04 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-19 09:42:04 -0500 |
| commit | 036227ad2f4b99556e23d6a64d30f031510cd8c4 (patch) | |
| tree | 8ac58451b7e0b2c97d88c9bdd48171ce4927a78c /src | |
| parent | f2b1461fa4ddf746752045ac098866c247f2e094 (diff) | |
| parent | 59c71426366964a17f131756281a5c7816422c8e (diff) | |
| download | slskd-036227ad2f4b99556e23d6a64d30f031510cd8c4.tar.gz | |
Merge pull request #604 from slskd/sqlite
Add SQLiteThreadingMode flag, explicitly check and configure SQLite threading config
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Core/Options.cs | 8 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 39 |
2 files changed, 44 insertions, 3 deletions
diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 3ce620c1..4ff785bb 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -312,6 +312,14 @@ namespace slskd [Description("log SQL queries generated by Entity Framework")] [RequiresRestart] public bool LogSQL { get; init; } = false; + + /// <summary> + /// Gets the SQLite threading mode; MULTITHREADED (2) or SERIALIZED (3). Default is MULTITHREADED. + /// </summary> + [Argument(default, "sqlite-threading-mode")] + [EnvironmentVariable("SQLITE_THREADING_MODE")] + [Description("sets the SQLite threading mode to MULTITHREADED (2) or SERIALIZED (3)")] + public int SQLiteThreadingMode { get; init; } = 3; } /// <summary> diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 189c015d..ca583bde 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -452,12 +452,11 @@ namespace slskd services.AddSingleton<IConnectionWatchdog, ConnectionWatchdog>(); + ConfigureSQLite(); + services.AddDbContext<SearchDbContext>("search.db"); services.AddDbContext<TransfersDbContext>("transfers.db"); - // https://github.com/aspnet/EntityFrameworkCore/issues/9994#issuecomment-508588678 - SQLitePCL.raw.sqlite3_config(2 /*SQLITE_CONFIG_MULTITHREAD*/); - services.AddSingleton<IBrowseTracker, BrowseTracker>(); services.AddSingleton<IConversationTracker, ConversationTracker>(); services.AddSingleton<IRoomTracker, RoomTracker>(_ => new RoomTracker(messageLimit: 250)); @@ -480,6 +479,40 @@ namespace slskd return services; } + private static void ConfigureSQLite() + { + // initialize + // avoids: System.Exception: You need to call SQLitePCL.raw.SetProvider(). If you are using a bundle package, this is done by calling SQLitePCL.Batteries.Init(). + SQLitePCL.Batteries.Init(); + + // check the threading mode set at compile time. if it is 0 it is unsafe to use in a multithreaded application, which slskd is. + // https://www.sqlite.org/compile.html#threadsafe + var threadSafe = SQLitePCL.raw.sqlite3_threadsafe(); + + if (threadSafe == 0) + { + throw new InvalidOperationException($"SQLite binary was not compiled with THREADSAFE={threadSafe}, which is not compatible with this application. Please create a GitHub issue to report this and include details about your environment."); + } + + Log.Debug("SQLite was compiled with THREADSAFE={Mode}", threadSafe); + + var selectedThreadingMode = OptionsAtStartup.Flags.SQLiteThreadingMode; + + if (selectedThreadingMode != SQLitePCL.raw.SQLITE_CONFIG_MULTITHREAD && selectedThreadingMode != SQLitePCL.raw.SQLITE_CONFIG_SERIALIZED) + { + throw new InvalidOperationException($"Invalid SQLite threading mode {selectedThreadingMode}; select either MULTITHREADED (2) or SERIALIZED (3)"); + } + + var modeNames = new[] { null, "MULTITHREADED", "SERIALIZED" }; + + if (SQLitePCL.raw.sqlite3_config(selectedThreadingMode) != SQLitePCL.raw.SQLITE_OK) + { + throw new InvalidOperationException($"SQLite config could not be set to {modeNames[selectedThreadingMode]}. Please create a GitHub issue to report this and include details about your environment."); + } + + Log.Debug("SQLite threading mode set to {Mode} ({Number})", modeNames[selectedThreadingMode], selectedThreadingMode); + } + private static IServiceCollection ConfigureAspDotNetServices(this IServiceCollection services) { services.AddCors(options => options.AddPolicy("AllowAll", builder => builder |
