From 9a7fe6bd8045b820531e0989dd61269ef3ca6736 Mon Sep 17 00:00:00 2001 From: Tim Haasdyk Date: Thu, 19 Mar 2026 15:23:15 +0100 Subject: [PATCH 1/3] Don't clear used connection pools --- .../LcmCrdt.Tests/SnapshotAtCommitServiceTests.cs | 2 +- backend/FwLite/LcmCrdt/SnapshotAtCommitService.cs | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/backend/FwLite/LcmCrdt.Tests/SnapshotAtCommitServiceTests.cs b/backend/FwLite/LcmCrdt.Tests/SnapshotAtCommitServiceTests.cs index 74e03cb9b3..77e98d1d3a 100644 --- a/backend/FwLite/LcmCrdt.Tests/SnapshotAtCommitServiceTests.cs +++ b/backend/FwLite/LcmCrdt.Tests/SnapshotAtCommitServiceTests.cs @@ -329,7 +329,7 @@ await _api.CreateWritingSystem(new WritingSystem() public async Task DisposeAsync() { await _dbContext.Database.CloseConnectionAsync(); - SqliteConnection.ClearAllPools(); + SqliteConnection.ClearPool(new SqliteConnection($"Data Source={_dbPath}")); await _dbContext.Database.EnsureDeletedAsync(); await _services.DisposeAsync(); if (File.Exists(_dbPath)) diff --git a/backend/FwLite/LcmCrdt/SnapshotAtCommitService.cs b/backend/FwLite/LcmCrdt/SnapshotAtCommitService.cs index 6f7f3bc5f4..e7dec74363 100644 --- a/backend/FwLite/LcmCrdt/SnapshotAtCommitService.cs +++ b/backend/FwLite/LcmCrdt/SnapshotAtCommitService.cs @@ -24,7 +24,7 @@ public class SnapshotAtCommitService( { using var activity = _activitySource.StartActivity(); activity?.SetTag("app.commit_id", commitId); - var dbContext = await crdtDbContextFactory.CreateDbContextAsync(); + await using var dbContext = await crdtDbContextFactory.CreateDbContextAsync(); var commit = await dbContext.Commits.SingleOrDefaultAsync(c => c.Id == commitId); if (commit is null) { @@ -42,7 +42,7 @@ public class SnapshotAtCommitService( await ForkDatabase(dbPath, forkDbPath); var project = new CrdtProject(currentProjectService.Project.Name, forkDbPath); - var serviceScope = serviceProvider.CreateAsyncScope(); + await using var serviceScope = serviceProvider.CreateAsyncScope(); var scopedCurrentProjectService = serviceScope.ServiceProvider.GetRequiredService(); var projectData = await scopedCurrentProjectService.SetupProjectContext(project); @@ -51,7 +51,7 @@ public class SnapshotAtCommitService( var optionsBuilder = new DbContextOptionsBuilder() .UseSqlite($"Data Source={forkDbPath}"); LcmCrdtKernel.ConfigureDbOptions(serviceScope.ServiceProvider, optionsBuilder); - ICrdtDbContext forkDbContext = new LcmCrdtDbContext(optionsBuilder.Options, crdtConfig); + await using var forkDbContext = new LcmCrdtDbContext(optionsBuilder.Options, crdtConfig); var deleted = await DeleteCommitsAfter(forkDbContext, commit, preserveAllFieldWorksCommits); logger.LogInformation("Deleted {Deleted} commits after {CommitId}", deleted, commitId); @@ -68,7 +68,10 @@ public class SnapshotAtCommitService( { try { - SqliteConnection.ClearAllPools(); + // Clear only the fork connection pool, not all pools. + // ClearAllPools() would destroy connections to other databases + // (including in-memory test databases). + SqliteConnection.ClearPool(new SqliteConnection($"Data Source={forkDbPath}")); File.Delete(forkDbPath); } catch (Exception ex) From 24ebdc0cf10c3d01375b229133e808fe56910710 Mon Sep 17 00:00:00 2001 From: Tim Haasdyk Date: Thu, 19 Mar 2026 17:09:27 +0100 Subject: [PATCH 2/3] Dispose created connections --- backend/FwLite/LcmCrdt.Tests/SnapshotAtCommitServiceTests.cs | 4 +++- backend/FwLite/LcmCrdt/SnapshotAtCommitService.cs | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/backend/FwLite/LcmCrdt.Tests/SnapshotAtCommitServiceTests.cs b/backend/FwLite/LcmCrdt.Tests/SnapshotAtCommitServiceTests.cs index 77e98d1d3a..af2d446a9a 100644 --- a/backend/FwLite/LcmCrdt.Tests/SnapshotAtCommitServiceTests.cs +++ b/backend/FwLite/LcmCrdt.Tests/SnapshotAtCommitServiceTests.cs @@ -5,6 +5,7 @@ using SIL.Harmony.Db; using Microsoft.Data.Sqlite; using SIL.Harmony.Core; +using Bogus.DataSets; namespace LcmCrdt.Tests; @@ -329,7 +330,8 @@ await _api.CreateWritingSystem(new WritingSystem() public async Task DisposeAsync() { await _dbContext.Database.CloseConnectionAsync(); - SqliteConnection.ClearPool(new SqliteConnection($"Data Source={_dbPath}")); + using var clearConn = new SqliteConnection($"Data Source={_dbPath}"); + SqliteConnection.ClearPool(clearConn); await _dbContext.Database.EnsureDeletedAsync(); await _services.DisposeAsync(); if (File.Exists(_dbPath)) diff --git a/backend/FwLite/LcmCrdt/SnapshotAtCommitService.cs b/backend/FwLite/LcmCrdt/SnapshotAtCommitService.cs index e7dec74363..aa36fa2dc3 100644 --- a/backend/FwLite/LcmCrdt/SnapshotAtCommitService.cs +++ b/backend/FwLite/LcmCrdt/SnapshotAtCommitService.cs @@ -71,7 +71,8 @@ public class SnapshotAtCommitService( // Clear only the fork connection pool, not all pools. // ClearAllPools() would destroy connections to other databases // (including in-memory test databases). - SqliteConnection.ClearPool(new SqliteConnection($"Data Source={forkDbPath}")); + using var clearConn = new SqliteConnection($"Data Source={forkDbPath}"); + SqliteConnection.ClearPool(clearConn); File.Delete(forkDbPath); } catch (Exception ex) From 75e737f088cfda78b796e4c80ffef481086aa943 Mon Sep 17 00:00:00 2001 From: Tim Haasdyk Date: Mon, 23 Mar 2026 09:53:39 +0100 Subject: [PATCH 3/3] Dispose db context --- backend/FwLite/LcmCrdt.Tests/SnapshotAtCommitServiceTests.cs | 1 + backend/FwLite/LcmCrdt/SnapshotAtCommitService.cs | 3 --- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/backend/FwLite/LcmCrdt.Tests/SnapshotAtCommitServiceTests.cs b/backend/FwLite/LcmCrdt.Tests/SnapshotAtCommitServiceTests.cs index af2d446a9a..f1cde771bc 100644 --- a/backend/FwLite/LcmCrdt.Tests/SnapshotAtCommitServiceTests.cs +++ b/backend/FwLite/LcmCrdt.Tests/SnapshotAtCommitServiceTests.cs @@ -333,6 +333,7 @@ public async Task DisposeAsync() using var clearConn = new SqliteConnection($"Data Source={_dbPath}"); SqliteConnection.ClearPool(clearConn); await _dbContext.Database.EnsureDeletedAsync(); + await _dbContext.DisposeAsync(); await _services.DisposeAsync(); if (File.Exists(_dbPath)) { diff --git a/backend/FwLite/LcmCrdt/SnapshotAtCommitService.cs b/backend/FwLite/LcmCrdt/SnapshotAtCommitService.cs index aa36fa2dc3..c6ce53549f 100644 --- a/backend/FwLite/LcmCrdt/SnapshotAtCommitService.cs +++ b/backend/FwLite/LcmCrdt/SnapshotAtCommitService.cs @@ -68,9 +68,6 @@ public class SnapshotAtCommitService( { try { - // Clear only the fork connection pool, not all pools. - // ClearAllPools() would destroy connections to other databases - // (including in-memory test databases). using var clearConn = new SqliteConnection($"Data Source={forkDbPath}"); SqliteConnection.ClearPool(clearConn); File.Delete(forkDbPath);