-
Notifications
You must be signed in to change notification settings - Fork 486
Fix Dapr client startup deadlock in distributed deployment #713
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
Open
Copilot
wants to merge
3
commits into
master
Choose a base branch
from
copilot/fix-dapr-client-start-issue
base: master
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.
Open
Changes from all commits
Commits
Show all changes
3 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
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
src/Dapr/ChatServer.Host/ChatServerHostedServiceWrapper.cs
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 |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // <copyright file="ChatServerHostedServiceWrapper.cs" company="MUnique"> | ||
| // Licensed under the MIT License. See LICENSE file in the project root for full license information. | ||
| // </copyright> | ||
|
|
||
| namespace MUnique.OpenMU.ChatServer.Host; | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
| using MUnique.OpenMU.Dapr.Common; | ||
| using MUnique.OpenMU.DataModel.Configuration; | ||
| using MUnique.OpenMU.PlugIns; | ||
| using ChatServer = MUnique.OpenMU.ChatServer.ChatServer; | ||
|
|
||
| /// <summary> | ||
| /// A wrapper which takes a <see cref="ChatServer"/> and wraps it as <see cref="IHostedLifecycleService"/>, | ||
| /// so that additional initialization can be done before actually starting it. | ||
| /// The actual server start is deferred to <see cref="StartedAsync"/> which is called after the web application | ||
| /// has started (i.e. the HTTP API is already available), breaking the circular startup dependency with the Dapr sidecar. | ||
| /// TODO: listen to configuration changes/database reinit. | ||
| /// See also: ServerContainerBase. | ||
| /// </summary> | ||
| public class ChatServerHostedServiceWrapper : IHostedLifecycleService | ||
| { | ||
| private readonly IServiceProvider _serviceProvider; | ||
| private ChatServer? _chatServer; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ChatServerHostedServiceWrapper"/> class. | ||
| /// </summary> | ||
| /// <param name="serviceProvider">The service provider.</param> | ||
| public ChatServerHostedServiceWrapper(IServiceProvider serviceProvider) | ||
| { | ||
| this._serviceProvider = serviceProvider; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public Task StartingAsync(CancellationToken cancellationToken) => Task.CompletedTask; | ||
|
|
||
| /// <inheritdoc/> | ||
| public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask; | ||
|
|
||
| /// <inheritdoc/> | ||
| public async Task StartedAsync(CancellationToken cancellationToken) | ||
| { | ||
| await this._serviceProvider.WaitForDatabaseInitializationAsync(cancellationToken).ConfigureAwait(false); | ||
|
|
||
| if (this._serviceProvider.GetService<ICollection<PlugInConfiguration>>() is { } plugInCollection) | ||
| { | ||
| if (plugInCollection is not List<PlugInConfiguration> plugInConfigurations) | ||
| { | ||
| throw new InvalidOperationException($"The registered {nameof(ICollection<PlugInConfiguration>)} must be a {nameof(List<PlugInConfiguration>)} to be able to load plugin configurations."); | ||
| } | ||
|
|
||
| await this._serviceProvider.TryLoadPlugInConfigurationsAsync(plugInConfigurations).ConfigureAwait(false); | ||
| } | ||
|
|
||
| var settings = this._serviceProvider.GetRequiredService<ChatServerDefinition>().ConvertToSettings(); | ||
| this._chatServer = this._serviceProvider.GetRequiredService<ChatServer>(); | ||
| this._chatServer.Initialize(settings); | ||
| await this._chatServer.StartAsync(cancellationToken).ConfigureAwait(false); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public Task StoppingAsync(CancellationToken cancellationToken) => Task.CompletedTask; | ||
|
|
||
| /// <inheritdoc/> | ||
| public Task StopAsync(CancellationToken cancellationToken) | ||
| { | ||
| return this._chatServer?.StopAsync(cancellationToken) ?? Task.CompletedTask; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public Task StoppedAsync(CancellationToken cancellationToken) => Task.CompletedTask; | ||
| } |
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
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.
There is a potential issue here with multiple subscriptions to the
PropertyChangedevent. If an exception occurs afterGetRequiredServicebut beforethis._serveris set (e.g., in theServerStateDataconstructor), thecatchblock will cause a retry. In the next iteration,GetRequiredServicewill return the same singletonIManageableServerinstance, and another event handler will be subscribed. This leads to a memory leak and the handler being called multiple times for each event. It's safer to unsubscribe before subscribing to prevent this.