fix(data): never block the caller's thread and never lose a SQLite cancel
SQLite steps ran synchronously on the calling thread, so a repository awaited on the WPF UI thread would freeze the window for the length of the query. Blocking calls now continue on the thread pool. A cancel that landed before sqlite3_step started the statement was lost (SQLite clears the interrupt when no statement is running) and the query ran to completion. Each connection now has a cancel flag polled by a progress handler; it is cleared outside every interruptible operation so a stale cancel cannot stop a later statement. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
6 files changed
+98
-7
No files matched your search
@@ -27,6 +27,26 @@ public sealed class SqliteBindingTests(TestContext context) : DatabaseTest(conte
|
||||
Assert.Equal(268435456L, await session.ScalarAsync<long>("PRAGMA mmap_size", [], Ct));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task CallsFromAUiThreadReturnBeforeTheStatementRuns()
|
||||
{
|
||||
await using var session = await (await OpenDatabaseAsync()).OpenAsync(Ct);
|
||||
using var cts = CancellationTokenSource.CreateLinkedTokenSource(Ct);
|
||||
Task? query = null;
|
||||
var ui = new Thread(() =>
|
||||
{
|
||||
// A UI thread: has a synchronization context and is not a pool thread.
|
||||
SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
|
||||
query = session.ExecuteAsync(
|
||||
"WITH RECURSIVE r(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM r WHERE n < 2000000000) SELECT count(*) FROM r", [], cts.Token);
|
||||
});
|
||||
ui.Start();
|
||||
Assert.True(ui.Join(TimeSpan.FromSeconds(5)), "the calling thread was blocked by the statement");
|
||||
Assert.False(query!.IsCompleted);
|
||||
await cts.CancelAsync();
|
||||
await Assert.ThrowsAsync<OperationCanceledException>(() => query);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task RejectsScriptsAndWrongParameterCounts()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user