-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix(redis)!: fix multiplexer ownership #10146
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
Merged
ReubenBond
merged 4 commits into
dotnet:main
from
ReubenBond:reubenbond/fix-redis-multiplexer-ownership
Jun 4, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ | |
|
|
||
| namespace Orleans.GrainDirectory.Redis | ||
| { | ||
| public partial class RedisGrainDirectory : IGrainDirectory, ILifecycleParticipant<ISiloLifecycle> | ||
| public partial class RedisGrainDirectory : IGrainDirectory, ILifecycleParticipant<ISiloLifecycle>, IDisposable, IAsyncDisposable | ||
| { | ||
| private readonly RedisGrainDirectoryOptions _directoryOptions; | ||
| private readonly ClusterOptions _clusterOptions; | ||
|
|
@@ -25,6 +25,7 @@ public partial class RedisGrainDirectory : IGrainDirectory, ILifecycleParticipan | |
| // Both are initialized in the Initialize method. | ||
| private IConnectionMultiplexer _redis = null!; | ||
| private IDatabase _database = null!; | ||
| private bool _redisIsShared; | ||
|
|
||
| private bool _disposed; | ||
|
|
||
|
|
@@ -172,12 +173,12 @@ public Task UnregisterSilos(List<SiloAddress> siloAddresses) | |
|
|
||
| public void Participate(ISiloLifecycle lifecycle) | ||
| { | ||
| lifecycle.Subscribe(nameof(RedisGrainDirectory), ServiceLifecycleStage.RuntimeInitialize, Initialize, Uninitialize); | ||
| lifecycle.Subscribe(nameof(RedisGrainDirectory), ServiceLifecycleStage.RuntimeInitialize, Initialize); | ||
| } | ||
|
|
||
| public async Task Initialize(CancellationToken ct = default) | ||
| { | ||
| _redis = await _directoryOptions.CreateMultiplexer(_directoryOptions); | ||
| (_redis, _redisIsShared) = await _directoryOptions.CreateMultiplexer(_directoryOptions); | ||
|
|
||
| // Configure logging | ||
| _redis.ConnectionRestored += LogConnectionRestored; | ||
|
|
@@ -188,16 +189,51 @@ public async Task Initialize(CancellationToken ct = default) | |
| _database = _redis.GetDatabase(); | ||
| } | ||
|
|
||
| private async Task Uninitialize(CancellationToken arg) | ||
| public void Dispose() | ||
| { | ||
| if (_redis != null && _redis.IsConnected) | ||
| var redis = _redis; | ||
| if (redis is null) | ||
| { | ||
| _disposed = true; | ||
| return; | ||
| } | ||
|
|
||
| var redisIsShared = _redisIsShared; | ||
| redis.ConnectionRestored -= LogConnectionRestored; | ||
| redis.ConnectionFailed -= LogConnectionFailed; | ||
| redis.ErrorMessage -= LogErrorMessage; | ||
| redis.InternalError -= LogInternalError; | ||
| _disposed = true; | ||
| _redis = null!; | ||
| _database = null!; | ||
| _redisIsShared = false; | ||
|
ReubenBond marked this conversation as resolved.
|
||
|
|
||
| if (!redisIsShared) | ||
| { | ||
| redis.Dispose(); | ||
| } | ||
| } | ||
|
|
||
| await _redis.CloseAsync(); | ||
| _redis.Dispose(); | ||
| _redis = null!; | ||
| _database = null!; | ||
| public async ValueTask DisposeAsync() | ||
| { | ||
| var redis = _redis; | ||
| if (redis is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var redisIsShared = _redisIsShared; | ||
| redis.ConnectionRestored -= LogConnectionRestored; | ||
| redis.ConnectionFailed -= LogConnectionFailed; | ||
| redis.ErrorMessage -= LogErrorMessage; | ||
| redis.InternalError -= LogInternalError; | ||
| _disposed = true; | ||
| _redis = null!; | ||
| _database = null!; | ||
| _redisIsShared = false; | ||
|
ReubenBond marked this conversation as resolved.
|
||
|
|
||
| if (!redisIsShared) | ||
| { | ||
| await redis.DisposeAsync().ConfigureAwait(false); | ||
| } | ||
| } | ||
|
Comment on lines
+216
to
238
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 0d58300: same change applied to DisposeAsync() — _disposed is set before the early return so the object behaves consistently as disposed even when initialization never completed. |
||
|
|
||
|
|
||
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 0d58300: _disposed is now set at the very start of Dispose(), before the early
eturn when _redis is null. So disposing before (or without) Initialize leaves the instance in a disposed state and later Lookup calls return null instead of throwing a NullReferenceException.