Implement safe disposal pattern in AmqpHandler#307
Merged
OrjanSkotnes merged 2 commits intomainfrom Oct 30, 2025
Merged
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR implements a robust disposal pattern for the AMQP handler to prevent resource leaks and handle edge cases during cleanup. The changes ensure idempotent disposal, graceful error handling, and comprehensive test coverage for various disposal scenarios.
Key Changes:
- Introduced idempotent disposal using
Interlocked.Exchangewith a_disposedflag to prevent duplicate cleanup - Added defensive error handling methods (
RunSafe,DisposeSafeAsync,SafelyUnsubscribeConnectionEvents) that catch and log exceptions instead of propagating them - Expanded test suite with 5 new tests covering edge cases: null connections, closed connections, multiple disposal calls, and exception handling during cleanup
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| KS.Fiks.IO.Client/Amqp/AmqpHandler.cs | Implements idempotent disposal pattern with error handling helpers and refactored connection logic |
| KS.Fiks.IO.Client.Tests/Amqp/AmqpHandlerTests.cs | Adds comprehensive tests for disposal edge cases including null connections, closed connections, and exception handling |
| KS.Fiks.IO.Client.Tests/Amqp/AmqpHandlerFixture.cs | Enhances test fixture to support disposal testing by exposing handler instance and enabling connection manipulation via reflection |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
jarleborsheim
approved these changes
Oct 30, 2025
Contributor
jarleborsheim
left a comment
There was a problem hiding this comment.
LGTM 🦖
Men kanskje lurt at noen andre ser på også
Kolbein
reviewed
Oct 30, 2025
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.
This pull request enhances the reliability and robustness of the AMQP handler's disposal logic and significantly expands the test coverage for resource cleanup scenarios. The changes ensure that disposing the AMQP handler is safe to call multiple times, gracefully handles edge cases like null or already disposed connections, and logs issues without throwing exceptions. The test suite is updated to verify these behaviors.
Improvements to disposal logic in
AmqpHandler:DisposeAsyncidempotent by introducing a_disposedflag and usingInterlocked.Exchangeto ensure resources are only cleaned up once.RunSafe,DisposeSafeAsync,SafelyUnsubscribeConnectionEvents) to handleObjectDisposedExceptionand log warnings instead of throwing.Expanded and improved test coverage:
DisposeAsync:These changes collectively ensure that the AMQP handler's cleanup is robust, predictable, and well-tested, reducing the risk of resource leaks or unexpected exceptions during shutdown.