Skip to content

fix issues with change streams listening for lock changes - #59

Merged
joaofbantunes merged 1 commit into
mainfrom
distributed-lock-with-change-streams-fix
Jul 25, 2026
Merged

joaofbantunes merged 1 commit into
mainfrom
distributed-lock-with-change-streams-fix

Conversation

@joaofbantunes

Copy link
Copy Markdown
Member

Fixed a bunch of issues with the distributed lock, when using change streams to detect lock changes

  • 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)

Copilot AI left a comment

Copy link
Copy Markdown

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 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 IChangeStreamNotifier returned by ChangeStreamListener.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.

Comment thread tests/MongoDb.Tests/Synchronization/DistributedLockThingyTests.cs
Comment thread docker-compose.yml
- 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
joaofbantunes force-pushed the distributed-lock-with-change-streams-fix branch from 456b84c to d53728a Compare July 25, 2026 12:09
@joaofbantunes
joaofbantunes merged commit 305e89a into main Jul 25, 2026
1 check passed
@joaofbantunes
joaofbantunes deleted the distributed-lock-with-change-streams-fix branch July 25, 2026 14:12
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.

2 participants