diff --git a/Changelog.md b/Changelog.md index 1f2a0e8..20cf7b2 100644 --- a/Changelog.md +++ b/Changelog.md @@ -31,6 +31,7 @@ - Async observer reads now use `EnumerateMongoAsync` to translate only cursor acquisition and advancement errors, matching synchronous `EnumerateMongo`. Mongo exceptions from application deserializers, `OnNextAsync`, and cursor disposal reach `OnErrorAsync` unchanged instead of being wrapped as storage failures. Driver errors still retain the original exception as `InnerException`. No public observer signatures change. [#108](https://github.com/NEventStore/NEventStore.Persistence.MongoDB/issues/108) - Default async observer reads invoke the protected `Task`-returning (non-result) `TryMongoAsync` guard for acquisition and each batch advance, with no observer argument, rather than once around the whole read. The existing result-returning guard overload is not used by these observer reads. Overrides no longer encompass application callbacks or cleanup. Per-batch calls enforce disposed-engine checks like sync enumeration. Cursor disposal remains unconditional and outside translation, like sync `using`; a cleanup failure may replace an earlier error under normal `using` semantics. [#108](https://github.com/NEventStore/NEventStore.Persistence.MongoDB/issues/108) +- Active async observer reads now check engine disposal before every cursor batch advancement, matching synchronous enumeration. After disposal is observed, the owned cursor is disposed and `ObjectDisposedException` is sent once to `OnErrorAsync`, without completion notification; a successful error handler leaves the returned task successful, while a failing handler propagates its exception. Already-disposed invocations still fault directly without observer notification. In-flight acquisition/advancement and callbacks are not canceled, and documents in an already fetched batch can still be delivered. Early observer stop still completes normally. [#109](https://github.com/NEventStore/NEventStore.Persistence.MongoDB/issues/109) - `EmptyRecycleBin()` and `EmptyRecycleBinAsync(...)` now translate checkpoint-read driver failures to `StorageException` or `StorageUnavailableException` (for `MongoConnectionException`), retaining the original inner exception. Consumers catching raw driver exceptions must handle the persistence exceptions instead. The existing protected virtual guard is still invoked once, but its callback now includes checkpoint acquisition and iteration before deletion. Non-driver exceptions and cancellation retain their identity. [#107](https://github.com/NEventStore/NEventStore.Persistence.MongoDB/issues/107) @@ -47,6 +48,7 @@ ### Migration notes - For async observer reads, handle application Mongo exceptions directly in `OnErrorAsync`. Set `MongoPersistenceOptions.WrapAsyncApplicationExceptions = true` to select the legacy whole-read guard once per operation, restoring legacy application/cleanup wrapping and protected guard scope. The option is read once per read and does not alter other async operations. Subclasses relying on whole-read timing, retries or interception should opt out or adapt to the new driver-operation scope. Cancellation, early-stop completion and terminal callback failures retain their observer contract. [#108](https://github.com/NEventStore/NEventStore.Persistence.MongoDB/issues/108) +- Keep the engine alive until all synchronous enumerations and async observer reads finish. For shutdown, cancel and await active async reads before disposing the shared engine; disposal is a batch-boundary guard, not immediate cancellation or atomic exclusion of concurrent reads. No data migration is required. [#109](https://github.com/NEventStore/NEventStore.Persistence.MongoDB/issues/109) - `Initialize()` now creates `GetFrom_RecycleBin_Index` on existing databases. No data backfill or old-index replacement is required, but deployments must budget index build time and disk/I/O costs and retain index-creation permission. Operators can pre-create the documented definition to move build work out of startup. See [recycle-bin rollout and indexing](docs/Recycle-Bin.md#recycle-lookup-index-and-deployment). [#87](https://github.com/NEventStore/NEventStore.Persistence.MongoDB/issues/87) diff --git a/src/NEventStore.Persistence.MongoDB.Tests/AcceptanceTests/Issues/Issue109AsyncDisposal.cs b/src/NEventStore.Persistence.MongoDB.Tests/AcceptanceTests/Issues/Issue109AsyncDisposal.cs new file mode 100644 index 0000000..d583024 --- /dev/null +++ b/src/NEventStore.Persistence.MongoDB.Tests/AcceptanceTests/Issues/Issue109AsyncDisposal.cs @@ -0,0 +1,152 @@ +using FakeItEasy; +using FluentAssertions; +using MongoDB.Bson; +using MongoDB.Driver; +using NEventStore.Persistence.AcceptanceTests; +using NEventStore.Persistence.AcceptanceTests.BDD; +using NEventStore.Serialization; +#if NUNIT +using NUnit.Framework; +namespace NEventStore.Persistence.MongoDB.Tests.AcceptanceTests.Issues.Async +{ + [TestFixtureSource(nameof(Cases))] + public class when_Issue109_disposal_crosses_an_async_read_boundary(int path, string stage, bool legacy) : SpecificationBase + { + public static IEnumerable Cases() + { + for (int path = 0; path < 7; path++) + foreach (var stage in new[] { "before", "acquisition", "advance", "observer", "stop", "normal", "error handler" }) + foreach (var legacy in new[] { false, true }) + yield return new object[] { path, stage, legacy }; + } + private readonly IAsyncCursor _cursor = A.Fake>(); + private readonly TaskCompletionSource _entered = new(TaskCreationOptions.RunContinuationsAsynchronously); + private readonly TaskCompletionSource _resume = new(TaskCreationOptions.RunContinuationsAsynchronously); + private MongoPersistenceEngine _engine = null!; + private Exception? _reported, _escaped; + private readonly Exception _terminalFailure = new InvalidOperationException("Terminal callback failed."); + private int _finds, _moves, _next, _errors, _completed; + protected override void Context() + { + var database = A.Fake(); + var collection = A.Fake>(); + A.CallTo(() => collection.Settings).Returns(new MongoCollectionSettings()); + A.CallTo(() => collection.WithWriteConcern(A._)).Returns(collection); + A.CallTo(() => database.GetCollection(A._, A._)).Returns(collection); + A.CallTo(() => collection.FindAsync(A>._, + A>._, CancellationToken.None)).ReturnsLazily(async () => + { + _finds++; + if (stage == "acquisition") await Pause(); + return _cursor; + }); + A.CallTo(() => _cursor.MoveNextAsync(CancellationToken.None)).ReturnsLazily(async () => + { + _moves++; + if (_moves == 1 && stage == "advance") await Pause(); + return _moves <= 2; + }); + // Two documents prove disposal is checked at batch boundaries, not per document. + A.CallTo(() => _cursor.Current).Returns(new[] { Document(path == 5 ? 7 : 0, 1, false), Document(path == 5 ? 7 : 0, 2, false) }); + _engine = new MongoPersistenceEngine(database, A.Fake(), new MongoPersistenceOptions { WrapAsyncApplicationExceptions = legacy }); + } + private async Task Pause() + { + _entered.TrySetResult(true); + await _resume.Task.ConfigureAwait(false); + } + private IAsyncObserver Observer() + { + var observer = A.Fake>(); + A.CallTo(() => observer.OnNextAsync(A._, CancellationToken.None)).ReturnsLazily(async () => + { + _next++; + if (_next == 1 && (stage == "observer" || stage == "stop" || stage == "error handler")) await Pause(); + return stage != "stop"; + }); + A.CallTo(() => observer.OnErrorAsync(A._, CancellationToken.None)) + .ReturnsLazily((Exception error, CancellationToken _) => { _reported = error; _errors++; return stage == "error handler" ? Task.FromException(_terminalFailure) : Task.CompletedTask; }); + A.CallTo(() => observer.OnCompletedAsync(CancellationToken.None)) + .ReturnsLazily(() => { _completed++; return Task.CompletedTask; }); + return observer; + } + protected override async Task BecauseAsync() + { + if (stage == "before") _engine.Dispose(); + var observer = Observer(); + var operation = Catch.ExceptionAsync(() => path switch + { + 0 => _engine.GetFromAsync("bucket", "stream", 0, int.MaxValue, observer, CancellationToken.None), + 1 => _engine.GetFromAsync("bucket", 0L, observer, CancellationToken.None), + 2 => _engine.GetFromToAsync("bucket", 0L, long.MaxValue, observer, CancellationToken.None), + 3 => _engine.GetFromAsync(0L, observer, CancellationToken.None), + 4 => _engine.GetFromToAsync(0L, long.MaxValue, observer, CancellationToken.None), + 5 => _engine.GetStreamsToSnapshotAsync("bucket", 0, Observer(), CancellationToken.None), + _ => _engine.GetDeletedCommitsAsync(observer, CancellationToken.None) + }); + if (stage != "before" && stage != "normal") + { + var reached = await Task.WhenAny(_entered.Task, operation, Task.Delay(TimeSpan.FromSeconds(10))); + reached.Should().BeSameAs(_entered.Task); + operation.IsCompleted.Should().BeFalse(); + _engine.Dispose(); + _resume.TrySetResult(true); + } + _escaped = await operation.ConfigureAwait(false); + } + [Fact] + public void Should_preserve_terminal_notification_contract() + { + if (stage == "before") _escaped.Should().BeOfType(); + else if (stage == "error handler") _escaped.Should().BeSameAs(_terminalFailure); + else _escaped.Should().BeNull(); + bool error = stage == "acquisition" || stage == "advance" || stage == "observer" || stage == "error handler"; + if (error) _reported.Should().BeOfType(); + else _reported.Should().BeNull(); + _errors.Should().Be(error ? 1 : 0); + _completed.Should().Be(stage == "normal" || stage == "stop" ? 1 : 0); + } + [Fact] + public void Should_stop_at_the_next_batch_boundary_and_release_the_owned_cursor() + { + _finds.Should().Be(stage == "before" ? 0 : 1); + _moves.Should().Be(stage == "before" || stage == "acquisition" ? 0 : stage == "normal" ? 3 : 1); + _next.Should().Be(stage == "before" || stage == "acquisition" ? 0 : stage == "normal" ? 4 : stage == "stop" ? 1 : 2); + if (stage == "before") A.CallTo(() => _cursor.Dispose()).MustNotHaveHappened(); + else A.CallTo(() => _cursor.Dispose()).MustHaveHappenedOnceExactly(); + } + protected override void Cleanup() { _resume.TrySetResult(true); _engine.Dispose(); } + private static BsonDocument Document(int path, int batch, bool serializedPayload) + { + if (path == 7) + return new BsonDocument + { + { MongoStreamHeadFields.Id, new BsonDocument + { + { MongoStreamHeadFields.BucketId, "bucket" }, + { MongoStreamHeadFields.StreamId, "stream" } + } + }, + { MongoStreamHeadFields.HeadRevision, batch }, + { MongoStreamHeadFields.SnapshotRevision, 0 } + }; + return new BsonDocument + { + { MongoCommitFields.CheckpointNumber, (long)batch }, + { MongoCommitFields.BucketId, "bucket" }, + { MongoCommitFields.StreamId, "stream" }, + { MongoCommitFields.StreamRevisionFrom, batch }, + { MongoCommitFields.StreamRevisionTo, batch }, + { MongoCommitFields.CommitSequence, batch }, + { MongoCommitFields.CommitStamp, new BsonDateTime(DateTime.UtcNow) }, + { MongoCommitFields.Headers, new BsonArray() }, + { MongoCommitFields.Events, serializedPayload + ? new BsonArray { new BsonDocument(MongoCommitFields.Payload, new BsonBinaryData(new byte[] { 1 })) } + : new BsonArray() + } + }; + } + + } +} +#endif diff --git a/src/NEventStore.Persistence.MongoDB/MongoPersistenceEngine.Async.cs b/src/NEventStore.Persistence.MongoDB/MongoPersistenceEngine.Async.cs index 8875152..1e83ad8 100644 --- a/src/NEventStore.Persistence.MongoDB/MongoPersistenceEngine.Async.cs +++ b/src/NEventStore.Persistence.MongoDB/MongoPersistenceEngine.Async.cs @@ -575,7 +575,7 @@ private async Task EnumerateMongoAsync(Func>> ac using var cursor = legacy ? await acquireCursorAsync().ConfigureAwait(false) : await TryMongoAsync(acquireCursorAsync, cancellationToken).ConfigureAwait(false); - Func> moveNextAsync = () => cursor.MoveNextAsync(cancellationToken); + Func> moveNextAsync = () => MoveNextWhileActiveAsync(cursor, cancellationToken); while (legacy ? await moveNextAsync().ConfigureAwait(false) : await TryMongoAsync(moveNextAsync, cancellationToken).ConfigureAwait(false)) @@ -588,6 +588,14 @@ private async Task EnumerateMongoAsync(Func>> ac } } + // Check before advancing, not within a fetched batch; in-flight work is not canceled. + private Task MoveNextWhileActiveAsync(IAsyncCursor cursor, CancellationToken cancellationToken) + { + if (IsDisposed) + throw new ObjectDisposedException("Attempt to use storage after it has been disposed."); + return cursor.MoveNextAsync(cancellationToken); + } + // Preserve the existing protected guard for driver operations, with the request token. private async Task TryMongoAsync(Func> callbackAsync, CancellationToken cancellationToken) {