fix issues with change streams listening for lock changes - #59
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens the MongoDB distributed lock’s change-stream-based change detection so lock acquisition/keep-alive reacts only to relevant external changes, cancels promptly, and disposes resources safely to avoid leaks and race conditions.
Changes:
- Refactors change stream support into an
IChangeStreamNotifierreturned byChangeStreamListener.ListenAsync, with improved filtering (ignore self-owner replace/insert) and safer background-task disposal. - Fixes lock lifecycle behaviors in
DistributedLockThingy(correct cancellation propagation, disposal on happy-path and error-path). - Updates tests/DI wiring to pass the change stream listener dependency; adds a new test asserting keep-alive renewals don’t log “potentially lost”; migrates solution/build references to
.slnx.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/MongoDb.Tests/Synchronization/DistributedLockThingyTests.cs | Updates lock tests for new ctor dependency; adds log-based regression test for self-change-stream events. |
| tests/MongoDb.Tests/Polling/BatchFetcherTests.cs | Updates polling tests to construct DistributedLockThingy with a ChangeStreamListener. |
| tests/MongoDb.Tests/MongoDb.Tests.csproj | Adds diagnostics testing package for fake logger/collector usage in tests. |
| src/MongoDb/Synchronization/InternalDistributedLockDefinition.cs | Renames/reshapes internal lock definition to hold an IChangeStreamNotifier. |
| src/MongoDb/Synchronization/DistributedLockThingy.cs | Integrates notifier lifecycle and cancellation into acquisition/keep-alive/release flows. |
| src/MongoDb/Synchronization/ChangeStreamListener.cs | Reworks listener into a factory returning a notifier; adds filtering + structured logging + safer disposal sequencing. |
| src/MongoDb/Polling/ConfigurationImplementation.cs | Registers ChangeStreamListener and injects it into the keyed DistributedLockThingy registration. |
| OutboxKit.slnx | Adds new solution format file. |
| OutboxKit.sln | Removes legacy .sln solution file. |
| global.json | Bumps Cake SDK used by the build. |
| docker-compose.yml | Pins MongoDB container image to a major tag. |
| Directory.Packages.props | Adds central version for Microsoft.Extensions.Diagnostics.Testing. |
| build/CakeRunner.cs | Updates build to target the new .slnx solution file. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- fix issue where listener reacted to changes caused by its owner distributed lock
(missing filters when analyzing change stream events)
- fix issue where listener kept waiting after cancellation was requested
(only used top level cancellation token tied to application lifetime, ignoring the ct associated with the lock keep alive task)
- fix issue where listener wasn't disposed on the happy path
(missing dispose call when lock was released)
- fix issue where cursor was disposed before background task stopped due to signaled cancellation token
(missing background task await when disposing the listener)
joaofbantunes
force-pushed
the
distributed-lock-with-change-streams-fix
branch
from
July 25, 2026 12:09
456b84c to
d53728a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixed a bunch of issues with the distributed lock, when using change streams to detect lock changes
(missing filters when analyzing change stream events)
(only used top level cancellation token tied to application lifetime, ignoring the ct associated with the lock keep alive task)
(missing dispose call when lock was released)
(missing background task await when disposing the listener)