Skip to content

Add CheckpointAsync and DisposeAsync to DocumentDbContext#120

Merged
mrdevrobot merged 2 commits into
mainfrom
copilot/fix-wal-checkpoint-on-dispose
May 11, 2026
Merged

Add CheckpointAsync and DisposeAsync to DocumentDbContext#120
mrdevrobot merged 2 commits into
mainfrom
copilot/fix-wal-checkpoint-on-dispose

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 11, 2026

DocumentDbContext.Dispose() releases the storage engine without flushing the WAL, leaving the .blite file potentially non-self-contained — fragile to copy or ship cold.

Changes

  • IDocumentDbContext — extends IAsyncDisposable; adds Task CheckpointAsync(CancellationToken ct = default)
  • DocumentDbContext — implements CheckpointAsync() (delegates to _storage.CheckpointAsync(ct)); implements DisposeAsync() which checkpoints under a try/finally so resources are released even if the checkpoint throws
  • Tests — three new tests covering explicit CheckpointAsync, await using auto-checkpoint, and interface-level access

Usage

// Option 1: explicit flush before close
using var db = new HelpDbContext(path);
await db.HelpChunks.InsertBulkAsync(chunks, transaction: null);
await db.CheckpointAsync(); // WAL merged into page file

// Option 2: auto-checkpoint via DisposeAsync
await using var db = new HelpDbContext(path);
await db.HelpChunks.InsertBulkAsync(chunks, transaction: null);
// DisposeAsync checkpoints automatically — file is self-contained after this point

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • av-build-tel-api-v1.avaloniaui.net
    • Triggering command: /usr/share/dotnet/dotnet dotnet exec --runtimeconfig /home/REDACTED/.nuget/packages/avalonia.buildservices/11.3.2/tools/netstandard2.0/runtimeconfig.json /home/REDACTED/.nuget/packages/avalonia.buildservices/11.3.2/tools/netstandard2.0/Avalonia.BuildServices.Collector.dll (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Agent-Logs-Url: https://github.com/EntglDb/BLite/sessions/8d7dcaa2-4647-4fe7-85f4-e85efd6ba007

Co-authored-by: mrdevrobot <12503462+mrdevrobot@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix DocumentDbContext.Dispose() to checkpoint WAL on close Add CheckpointAsync and DisposeAsync to DocumentDbContext May 11, 2026
Copilot AI requested a review from mrdevrobot May 11, 2026 11:26
@mrdevrobot mrdevrobot marked this pull request as ready for review May 11, 2026 11:37
Copilot AI review requested due to automatic review settings May 11, 2026 11:37
@mrdevrobot mrdevrobot merged commit bf9f2a3 into main May 11, 2026
6 checks passed
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds an explicit checkpointing API to DocumentDbContext and introduces async disposal semantics so callers can ensure committed WAL records are merged into the main page file before closing—making the primary .blite/.db file self-contained for cold copy/shipping scenarios.

Changes:

  • Extend IDocumentDbContext to IAsyncDisposable and add Task CheckpointAsync(CancellationToken ct = default).
  • Implement DocumentDbContext.CheckpointAsync() (delegates to StorageEngine.CheckpointAsync) and DocumentDbContext.DisposeAsync() (checkpoint + guaranteed resource cleanup via try/finally).
  • Add tests for explicit checkpointing, await using auto-checkpoint, and interface-level access.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
tests/BLite.Tests/DbContextTests.cs Adds tests for explicit CheckpointAsync, DisposeAsync auto-checkpoint, and interface exposure.
src/BLite.Core/IDocumentDbContext.cs Extends the interface with IAsyncDisposable and adds CheckpointAsync(ct).
src/BLite.Core/DocumentDbContext.cs Adds CheckpointAsync(ct) and DisposeAsync() implementation to checkpoint WAL before releasing resources.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +127 to +143
[Fact]
public async Task DbContext_CheckpointAsync_PersistsDataToPageFile()
{
// Insert data and call CheckpointAsync explicitly via the public API.
await using (var db = new TestDbContext(_dbPath))
{
await db.Users.InsertAsync(new User { Name = "Alice", Age = 30 });
// Checkpoint flushes the WAL into the main page file.
await db.CheckpointAsync();
}

// Re-open: data must be visible without replaying the WAL from scratch.
await using var db2 = new TestDbContext(_dbPath);
var all = await db2.Users.FindAllAsync().ToListAsync();
Assert.Single(all);
Assert.Equal("Alice", all[0].Name);
}
Comment on lines +145 to +159
[Fact]
public async Task DbContext_DisposeAsync_CheckpointsBeforeClose()
{
// Insert data and dispose via DisposeAsync (which checkpoints automatically).
await using (var db = new TestDbContext(_dbPath))
{
await db.Users.InsertAsync(new User { Name = "Bob", Age = 25 });
}

// Re-open: data must survive without any explicit checkpoint call.
await using var db2 = new TestDbContext(_dbPath);
var all = await db2.Users.FindAllAsync().ToListAsync();
Assert.Single(all);
Assert.Equal("Bob", all[0].Name);
}
Comment on lines 546 to 557
@@ -545,6 +557,33 @@ public void Dispose()
GC.SuppressFinalize(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DocumentDbContext.Dispose() does not checkpoint WAL - file may not be self-contained after close

3 participants