-
Notifications
You must be signed in to change notification settings - Fork 53
Fix GrpcChannel handle leak in AzureManaged backend #625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
torosent
wants to merge
4
commits into
main
Choose a base branch
from
torosent/fix-grpc-channel-dispose
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+327
−10
Conversation
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
- Add GrpcChannelCache for thread-safe channel caching by endpoint - Update Client/Worker extensions to use shared cache - Ensure channels are disposed when ServiceProvider disposes - Add comprehensive unit and integration tests
Member
|
I asked Copilot CLI to review this with the following prompt since the currently proposed solution looks far more complicated than I was expecting.
Here's what the response was: I can try to review this more deeply tomorrow. |
- Move channel factory call outside lock to prevent deadlock - Combine nested if statements in Replace method - Use 'using' statement for channel disposal - Catch Exception instead of bare catch - Remove unused variable in test
- Remove separate GrpcChannelCache class - Inline channel caching directly in ConfigureGrpcChannel using ConcurrentDictionary<string, Lazy<GrpcChannel>> - Make ConfigureGrpcChannel implement IDisposable for proper channel disposal - Remove unused Replace() and TryRemove() methods - Add disposal verification tests - Reduces complexity from 170+ LOC to ~40 LOC per extension
- Use LINQ Where() instead of if inside foreach for filtering channels - Narrow catch (Exception) to specific types (OperationCanceledException, ObjectDisposedException)
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
What changed?
ConfigureGrpcChannelclass usingConcurrentDictionary<string, Lazy<GrpcChannel>>ConfigureGrpcChannelimplementIDisposableto properly dispose channels when theServiceProvideris disposedWhy is this change needed?
IConfigureNamedOptions<DurableTaskSchedulerOptions>.Configure()method was being called multiple times (on each retry)GrpcChannel, which allocates internal HTTP handlers and socket connectionsoptions.Channelis pre-set (AzureManaged case),GetCallInvoker()returnsdefaultfor the disposable referenceIssues / work items
Project checklist
release_notes.mdAI-assisted code disclosure (required)
Was an AI tool used? (select one)
If AI was used:
src/Client/AzureManaged/DurableTaskSchedulerClientExtensions.cs(modified)src/Worker/AzureManaged/DurableTaskSchedulerWorkerExtensions.cs(modified)test/Client/AzureManaged.Tests/DurableTaskSchedulerClientExtensionsTests.cs(modified)test/Worker/AzureManaged.Tests/DurableTaskSchedulerWorkerExtensionsTests.cs(modified)GrpcChannelCacheclass to inlineConcurrentDictionary<string, Lazy<GrpcChannel>>per reviewer feedbackAI verification (required if AI was used):
Testing
Automated tests
test/Client/AzureManaged.Tests- 33 tests passedtest/Worker/AzureManaged.Tests- 31 tests passedtest/Shared/AzureManaged.Tests- 20 tests passedManual validation (only if runtime/behavior changed)
Notes for reviewers
ConfigureGrpcChannelclass now usesConcurrentDictionary<string, Lazy<GrpcChannel>>for thread-safe channel cachingLazy<GrpcChannel>ensures thread-safe initialization without holding locks during channel creation (avoids potential deadlocks)ShutdownAsync()is called beforeDispose()for graceful shutdown of in-flight RPCsvolatilekeyword on thedisposedfield ensures proper memory visibility when checking disposal state from multiple threads