Skip to content
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

offers the synchronous part of start and stop in IHostedService to run concurrently #85191

Merged
merged 3 commits into from
May 3, 2023

Conversation

pedrobsaila
Copy link
Contributor

Contributes to #68036

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Apr 22, 2023
@ghost
Copy link

ghost commented Apr 22, 2023

Tagging subscribers to this area: @dotnet/area-extensions-hosting
See info in area-owners.md if you want to be subscribed.

Issue Details

Contributes to #68036

Author: pedrobsaila
Assignees: -
Labels:

area-Extensions-Hosting, community-contribution

Milestone: -

_ = TryExecuteBackgroundServiceAsync(backgroundService);
}
}));
Task tasks = Task.WhenAll(_hostedServices.Select(service => Task.Run(() => StartAndTryToExecuteAsync(service, combinedCancellationToken))));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be passing token into Task.Run as well?

Suggested change
Task tasks = Task.WhenAll(_hostedServices.Select(service => Task.Run(() => StartAndTryToExecuteAsync(service, combinedCancellationToken))));
Task tasks = Task.WhenAll(_hostedServices.Select(service => Task.Run(() => StartAndTryToExecuteAsync(service, combinedCancellationToken), combinedCancellationToken)));

_ = TryExecuteBackgroundServiceAsync(backgroundService);
}
}));
Task tasks = Task.WhenAll(_hostedServices.Select(service => Task.Run(() => StartAndTryToExecuteAsync(service, combinedCancellationToken), cancellationToken)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this use cancellationToken rather than combinedCancellationToken as the Task.Run argument?

Also, since this isn't a hot code path I assume we're ok with the extra allocation that's incurred here for the closures and whatnot?

Copy link
Contributor Author

@pedrobsaila pedrobsaila Apr 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this use cancellationToken rather than combinedCancellationToken as the Task.Run argument?

I fixed the combined cancellation tokens, that was an error from me.

Also, since this isn't a hot code path I assume we're ok with the extra allocation that's incurred here for the closures and whatnot?

However I don't know if eliminating the closure in Task.Run is good. I know that Task has constructor that accepts Action<object?> action, object? state, CancellationToken cancellationToken. and that Task.Factory.StartNew has the same call. However I am unable to weight the gains of reducing a closure vs the loss from a (back and forth)cast from object to CancellationToken.

@@ -185,7 +182,7 @@ public async Task StopAsync(CancellationToken cancellationToken = default)

if (_options.ServicesStopConcurrently)
{
Task tasks = Task.WhenAll(hostedServices.Select(async service => await service.StopAsync(token).ConfigureAwait(false)));
Task tasks = Task.WhenAll(hostedServices.Select(service => Task.Run(() => service.StopAsync(token), cancellationToken)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question here about why two different tokens are being used?

Copy link
Member

@buyaa-n buyaa-n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @pedrobsaila LGTM.

@eerhardt @stephentoub do you have any other feedback?

@buyaa-n
Copy link
Member

buyaa-n commented May 1, 2023

Failures unrelated and looks all known issues

@buyaa-n
Copy link
Member

buyaa-n commented May 3, 2023

All failures are known issues, merging

@buyaa-n buyaa-n merged commit 6379ecb into dotnet:main May 3, 2023
@pedrobsaila pedrobsaila deleted the 68036-fix-concurrency branch May 3, 2023 06:26
@ghost ghost locked as resolved and limited conversation to collaborators Jun 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Extensions-Hosting community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants