-
Notifications
You must be signed in to change notification settings - Fork 2.7k
On SIGTERM default to a non-zero exit code #21300
Conversation
@dotnet-bot test OSX10.12 x64 Checked Innerloop Build and Test please |
Btw, the OSX build has failed with a real issue:
SIGTERM is used on OSX too. |
Important note: |
I'm thinking that instead of setting the exitcode, we should flag that this is a 'terminationExit'. |
@tmds I am not sure I understand what would the change to use a |
Yes, those comments were unrelated. I was thinking a user may want to add something in
That would allow different ProcessExit event handlers to work together. |
@tmds ah, got it. |
Thinking about it more: on SIGTERM: set 'terminateBySigterm' That will make the exit non-zero when there are no ProcessExit handlers and keep the existing exit code when there are such handlers. |
I am fine with the final state of the code. Let's see what @jkotas thinks about the breaking aspect of this change before merging it. |
Thank you for reviewing @janvorli. |
There is no way to distinguish between these two cases today, in backward compatible way. Basically, there is no API that the app can use to say "I want to shutdown cleanly even though the process got terminated by a signal". Your change is changing the
This means that this change needs to be coordinated with Kestrel. BTW: What do you mean by |
To shutdown cleanly, some action needs to be performed on exit. That's why we could use the presence of ProcessExit handlers as an assumption for clean termination. I think that is as backwards compatible as possible.
The underlying parts of Kestrel can be used for building hosting apps that process non-http request. |
That is wrong assumption to make. Presence of ProcessExit handlers does not imply clean termination. For example, logging libraries often subscribe to ProcessExit handlers to flush their buffers - but that does not make the process exit cleanly. |
That's why I call it an assumption :)
For those programs it keeps the behaviour as-is and requires someone to explicitly add a handler that sets the exit code to non-zero. I'm suggesting it as an option to improve backwards compat. |
@davidfowl ptal at this comment |
I'm happy with the current state of the PR. Another suggestion for change. We can change the coreclr/src/pal/src/thread/process.cpp Lines 1447 to 1449 in 1c18b32
As a value, we could for example use: 128 + 256 + 15 (SIGTERM) = 393. The implementation could be extended further (at a later time):
|
This delta looks fine to me as well, however there are multiple follow ups necessary:
|
@davidfowl @Tratcher can you please have a look?
Looking a bit into Windows behavior: When closing an app with the with the close button, the For Windows services, this can be controlled via I haven't found an API on Windows to request an app to terminate (cfr |
I think the closest one is https://docs.microsoft.com/en-us/windows/console/generateconsolectrlevent |
For the open actions on this issue.
|
I think no more review changes are needed for this PR. So we are waiting for an ACK from @davidfowl / @Tratcher on corresponding Kestrel/Host changes. |
AspNetCore's hosts attempt to shut down gracefully when triggered by ProcessExit or CancelKeyPress: We don't do anything with the exit code. The event threads don't even know if the app ends up shutting down gracefully or non-gracefully. Setting any exit code would need to happen on the Main thread. |
When the app is terminated by SIGTERM, the system Before this PR, the |
Go ahead and merge. We'll file an issue to follow up on our end. |
@dotnet-bot test this please |
Yes, I'll add tests for this in corefx. |
@jkotas Yes, it does |
@tmds Could you please submit a PR to CoreFX with the (failing) tests? I would like to make it easy to see that the test are failing without this change, and they will start passing once this change reaches CoreFX. |
// We set a non-zero exit code to indicate the process didn't terminate cleanly. | ||
// This value can be changed by the user by setting Environment.ExitCode in the | ||
// ProcessExit event. We only start termination on the first SIGTERM signal | ||
// to ensure we don't overwrite an exit code already set in ProcessExit. |
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.
What happens if the SIGTERM causes an int-returning Program.Main to exit gracefully and return a non-zero exit code?
I'm aware of dotnet/aspnetcore#6526 which tracks using Environment.ExitCode in the ProcessExit event to set the process exit code to zero when the ASP.NET host exits gracefully.
I'm more concerned about apps that return custom exit codes from Program.Main. Today this works as long as you avoid dotnet run
as demonstrated in dotnet/corefx#32749. Now that dotnet/cli#10544 has been merged, dotnet run
shouldn't even be an issue going forward.
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.
What happens if the SIGTERM causes an int-returning Program.Main to exit gracefully and return a non-zero exit code?
The program observing the exit code will assume the application didn't exit gracefully.
I'm more concerned about apps that return custom exit codes from Program.Main.
The ProcessExit handler must be aware of those. It can behave different based on the Environment.ExitCode value.
This test verifies the default ExitCode on SIGTERM is not zero. Verifies dotnet/coreclr#21300.
@tmds Thank you! |
* Unix: Add test for ExitCode on SIGTERM This test verifies the default ExitCode on SIGTERM is not zero. Verifies dotnet/coreclr#21300. * PR feedback * Fix BuildConfigurations
* On SIGTERM default to a non-zero exit code * Fix Windows builds * Improve SIG_DFL/SIG_IGN handling * Remove PAL_GetTerminationExitCode * Use sa_handler/sa_sigaction based on SA_SIGINFO; remove HAVE_SIGINFO_T. * configure.cmake: remove siginfo_t check * Move restore_signal_and_resend so OSX can use it; add function documentation * Fix OSX build: include pal/process.h for gPID * Check SIG_IGN and SIG_DFL against sa_handler * Don't use sa_handler when SA_SIGINFO is set * Fix equality check * Swap order of checking SA_SIGINFO and SIG_IGN/SIG_DFL Commit migrated from dotnet/coreclr@3f09035
* Unix: Add test for ExitCode on SIGTERM This test verifies the default ExitCode on SIGTERM is not zero. Verifies dotnet/coreclr#21300. * PR feedback * Fix BuildConfigurations Commit migrated from dotnet/corefx@e3ab2b0
Fixes https://github.com/dotnet/coreclr/issues/21243
CC @janvorli @adityamandaleeka