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

Consume PosixSignal in Hosting's ConsoleLifetime #56057

Merged
merged 5 commits into from
Jul 22, 2021

Conversation

eerhardt
Copy link
Member

This fixes quite a few Hosting exit issues related to ConsoleLifetime.

It is probably best to review the first commit by itself - Add NetCoreAppCurrent target to Microsoft.Extensions.Hosting. I needed to add a net6.0 target so I could use the new Signal APIs from #50527.

The rest of the commits fix the individual bugs.

Fix #55417
Fix #44086
Fix #50397
Fix #42224
Fix #35990

cc @stephentoub @jkotas - FYI in case you want to take a look

@dotnet-issue-labeler
Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@ghost
Copy link

ghost commented Jul 20, 2021

Tagging subscribers to this area: @eerhardt, @maryamariyan
See info in area-owners.md if you want to be subscribed.

Issue Details

This fixes quite a few Hosting exit issues related to ConsoleLifetime.

It is probably best to review the first commit by itself - Add NetCoreAppCurrent target to Microsoft.Extensions.Hosting. I needed to add a net6.0 target so I could use the new Signal APIs from #50527.

The rest of the commits fix the individual bugs.

Fix #55417
Fix #44086
Fix #50397
Fix #42224
Fix #35990

cc @stephentoub @jkotas - FYI in case you want to take a look

Author: eerhardt
Assignees: -
Labels:

area-Extensions-Hosting, new-api-needs-documentation

Milestone: -

Don't listen to ProcessExit on net6.0+ in Hosting anymore. This allows for Environment.Exit to not hang the app.
Don't clobber ExitCode during ProcessExit now that SIGTERM is handled separately.

For non-net6.0 targets, only wait for the shutdown timeout, so the process doesn't hang forever.

Fix dotnet#55417
Fix dotnet#44086
Fix dotnet#50397
Fix dotnet#42224
Fix dotnet#35990
@eerhardt eerhardt force-pushed the HostingPosixSignal branch from a4574cc to 280e63e Compare July 20, 2021 23:31
</ItemGroup>

<ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">
<Compile Remove="Internal\ConsoleLifetime.notnetcoreapp.cs" />
Copy link
Member

Choose a reason for hiding this comment

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

This naming 🤣

Copy link
Member Author

Choose a reason for hiding this comment

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

Suggestion for a better name?

Copy link
Member

Choose a reason for hiding this comment

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

.netstandard.cs ?

Copy link
Member Author

Choose a reason for hiding this comment

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

This library also builds for net461 and that TFM will compile this file. Is netstandard.cs used elsewhere when targeting net461? I can use that name if it makes sense to people.

Aside: I plan on proposing that in .NET 7 we change Microsoft.Extensions.Hosting to only target .NET 6+ going forward. Microsoft.Extensions.Hosting is an app model that should only be referenced by executables, libraries shouldn't reference it. So just like winforms/wpf/aspnetcore/maui/etc, we should only need to target .NET 6+ in Hosting.

@eerhardt
Copy link
Member Author

/azp run runtime

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

* Remove _shutdownBlock on netcoreappcurrent, as this is no longer waited on
* Change Console.CancelKeyPress to use PosixSignalRegistration SIGINT and SIGQUIT
* Use OperatingSystem.IsWindows
* Use a no-op lifetime on mobile platforms
Copy link
Member

@Tratcher Tratcher left a comment

Choose a reason for hiding this comment

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

🥳


namespace Microsoft.Extensions.Hosting.Internal
{
public partial class ConsoleLifetime : IHostLifetime
Copy link
Member

Choose a reason for hiding this comment

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

So clean

`Host.Run` in the Main method.

This caused other issues because SIGTERM wasn't the only way `ProcessExit` was raised. It is also raised by code
in the application calling `Environment.Exit`. `Environment.Exit` isn't a graceful way of shutting down a process.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
in the application calling `Environment.Exit`. `Environment.Exit` isn't a graceful way of shutting down a process.
in the application calling `Environment.Exit`. `Environment.Exit` isn't a graceful way of shutting down a process in the `Microsoft.Extensions.Hosting` app model.

Copy link
Member Author

Choose a reason for hiding this comment

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

Is it a graceful way of shutting down a process in any app model?

Copy link
Member

Choose a reason for hiding this comment

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

Environment.Exit is a graceful program termination from the runtime point of view.

App models typically want to wrap exit with their own logic. I agree that it is probably not considered the right way to exit the process in any app model, except the "no app model" simple apps.

Copy link
Member Author

Choose a reason for hiding this comment

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

I’ll fix this up in a follow up PR tomorrow so I don’t need to reset CI.

@eerhardt eerhardt merged commit 36f3f97 into dotnet:main Jul 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
5 participants