Fix disposal race condition in AsyncBatchHandler - #158
Merged
Conversation
Contributor
posthog-dotnet Compliance ReportDate: 2026-02-25 23:23:33 UTC ✅ All Tests Passed!29/29 tests passed Capture Tests✅ 29/29 tests passed View Details
|
`DisposeAsync` now awaits both background tasks (`_timerTask` and `_flushSignalTask`) before attempting the final flush. Previously, cancellation and resource disposal happened without waiting for in-flight flushes to complete, so the final `FlushBatchesAsync` could be silently skipped when `_flushing` was still held at 1. Fixes #146
haacked
force-pushed
the
haacked/fix-flushing-behavior
branch
from
February 25, 2026 23:20
1198e24 to
69202c8
Compare
Piccirello
approved these changes
Feb 25, 2026
haacked
marked this pull request as ready for review
February 25, 2026 23:22
haacked
enabled auto-merge
February 25, 2026 23:23
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.
Summary
DisposeAsyncnow stores and awaits both the timer and flush-signalTaskreferences before attempting the final flush. Previously, cancellation fired without waiting for in-flight flushes, so the finalFlushBatchesAsynccould be silently skipped when_flushingwas still 1.CancelAsync.FlushesItemWhenDisposedDuringInFlightFlushblocks a flush handler, starts disposal concurrently, then unblocks and verifies the item was sent.Fixes #146
Test plan
AsyncBatchHandlertests pass (FlushesBatchOnTimer,FlushesBatchWhenDisposed,DoesNotDisposeTwice, etc.)FlushesItemWhenDisposedDuringInFlightFlushtest passes on both net8.0 and netcoreapp3.1AppDomainshutdown