-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Update ComputerUnix.cs to fix Restart- and Stop-Computer on Linux/Mac #19780
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
Conversation
… Mac Fix RunCommand to respect 'command' and 'args' inputs.
Can't use string.empty as a default parameter value. Changed to "".
| /// Run a command. | ||
| /// </summary> | ||
| protected void RunCommand(String command, String args) { | ||
| protected void RunCommand(String command = "/sbin/shutdown", String args = "") { |
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.
Why do we need the defaults?
| protected void RunCommand(String command = "/sbin/shutdown", String args = "") { | |
| protected void RunCommand(string command, string args) { |
Please replace String with string.
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.
Note that the args string for macOS needs changing from "now" to "-h now", but note the concerns raised in #19738 (comment)
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.
I suggest don't complicate the PR and only fix the code with right shutdown for Linux and Mac. Then open new issue to enhance the cmdlet to support Force (and grace shutdown) on Unix.
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 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.
@iSazonov defaults have been removed. I had put them in there just because those were the previous hardcoded values. Changed String to string per your request.
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.
@mark-wilson-spc, note that the restart fix would work for macOS too, but shutdown would still be broken, if "now" isn't changed to "-h now".
My personal preference is to include the latter change, and then deal with the following aspects separately:
- graceful shutdown/reboot on macOS: Stop-Computer, Restart-Computer on macOS: support graceful shutdown by default, implement
-Forcesupport #19791 - inability to programmatically detect failure: Stop-Computer, Restart-Computer on Unix: inability to detect failure. #19802
Given my discussion of the tests above: Is it possible that the macOS test failure you saw was transient, unrelated to your changes? If so, including the "-h now" too shouldn't be a problem.
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.
Thank you for the clarification, @mklement0 . My thought was that we could go ahead and push out the working fix (Restart-Computer for Linux and MacOs) immediately since it is ready to go and has a broad benefit, and then address the thornier shutdown issue separately.
As far as the testing goes, I am new at this and am not in a position to speak to the results of the automated testing (especially where MacOS is concerned). I had assumed all tests were valid and robust, and that it was necessary to pass all tests for merge approval. I'm not sure who can best speak to your question regarding the possible transient or false nature of a test result.
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.
Thanks, @mark-wilson-spc - I'll have to defer to others too with respect to the tests.
As for whether to hold off on the macOS "-h now" fix to also make Stop-Computer work:
-
As noted, even without the fix,
Restart-Computerwill start to work on macOS, because-r nowworks on macOS too. -
If we proceed without the shutdown fix for macOS, we'll have a somewhat working
Restart-Computerthere - one that forces instant shutdown with potential data loss - and a still brokenStop-Computer. -
If we include the shutdown fix for macOS we'll have somewhat working
Restart-ComputerandStop-Computercmdlets on macOS, with the caveats noted.
If either approach gets into a stable release before the graceful approach is implemented on macOS (I'm not holding my breath), we'll have a backward-compatibility problem on our hand: users may by then have come to rely on the forceful and therefore guaranteed shutdown/reboot, even without having specified -Force.
Therefore, the most conservative approach would be: fix the problem for Linux only for now (which already had a working Stop-Computer, but a broken Restart-Computer), which would require deliberately setting the arguments to pass to shutdown to something invalid for Restart-Computer too.
I hope that lays out the options clearly, and I'll leave the decision to others.
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.
In any case we can not really stop/reboot CI system and have to use test mosc/hook. So we must check the cmdlets manually before merge.
As for code, I suggest to implement "-h now" for macOs as @mklement0 proposed. As result after the PR we should get the cmdlets working well on Linux and macOS but without (1) grace scenario, (2) error processing - both are for follow PRs.
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.
I am working on an implementation and should have it ready later today.
iSazonov
left a comment
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.
@mklement0 Could you please check the build on Mac and Linux?
|
@iSazonov, unfortunately, I'm not set up for building on Linux. |
Remove default parameter assignments and change 'String' to 'string'.
Change MacOs 'Stop-Computer' args from 'now' to '-h now' per PR directive.
| /// Run a command. | ||
| /// </summary> | ||
| protected void RunCommand(String command, String args) { | ||
| protected void RunCommand(string command, string args) { |
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.
The way /sbin/shutdown is invoked makes it impossible to detect failure on the PowerShell side, though that's probably for a separate PR; see
Added handling for MacOS and Unix Force and NoForce restarts and shutdowns.
| /// Force the operation to take place if possible. | ||
| /// </summary> | ||
| [Parameter] | ||
| public SwitchParameter Force { get; set; } = false; |
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.
No need to init to default.
| public SwitchParameter Force { get; set; } = false; | |
| public SwitchParameter Force { get; set; } |
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.
I will remove it, but just an fyi I used Computer.cs as my style template and it is defaulted there.
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.
Implemented.
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.
Removing the default assignment false on force params may have caused validation tests to fail. Re-added as a test.
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.
No, it doesn't affect the tests.
| string unixRestartCommand = "/sbin/shutdown"; | ||
| string unixRestartArgs = "-r now"; | ||
|
|
||
| string macOSRestartCommand = "osascript"; | ||
| string macOSRestartArgs = @"-e 'tell application ""System Events"" to restart'"; | ||
|
|
||
| string macOSForceRestartCommand = "/sbin/shutdown"; | ||
| string macOSForceRestartArgs = "-r now"; |
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.
Please use string const for all.
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.
Implemented.
…switch default value Implement requested changes from ISazonov. Change string to string const Remove unnecessary default value of false on Force parameter declarations.
Removing the assignment seems to have caused validation testing to fail.
| /// </summary> | ||
| protected override void BeginProcessing() | ||
| { | ||
| string const unixRestartCommand = "/sbin/shutdown"; |
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.
Build failed on the wrong syntax.
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.
Corrected.
|
@mark-wilson-spc Please sign CLA (see instructions above) if you agree. We can not merge without CLA. |
|
@mklement0 Please review last build. |
|
@microsoft-github-policy-service agree company="SierraSpace.com" |
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
JamesWTruher
left a comment
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.
I like this (I think we could add sleep too - in a separate PR).
I just have 1 question and a concern about more validation.
| command = unixRestartCommand; | ||
| args = unixRestartArgs; | ||
| } | ||
|
|
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.
now that you've changed the way shutdown is being called (and adding the more gentle osascript approach, I would be happier if we boosted the tests here, at least to have validation between force and not-force on mac
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.
In CI, a test hook will have to be needed to verify intended behavior
| const string unixRestartArgs = "-r now"; | ||
|
|
||
| const string macOSRestartCommand = "osascript"; | ||
| const string macOSRestartArgs = @"-e 'tell application ""System Events"" to restart'"; |
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.
if this is the more gentle approach, would it be a good idea to pop UI for confirmation?
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.
ShouldProcess might be appropriate.
|
This pull request has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 15 days. It will be closed if no further activity occurs within 10 days of this comment. |
4 similar comments
|
This pull request has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 15 days. It will be closed if no further activity occurs within 10 days of this comment. |
|
This pull request has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 15 days. It will be closed if no further activity occurs within 10 days of this comment. |
|
This pull request has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 15 days. It will be closed if no further activity occurs within 10 days of this comment. |
|
This pull request has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 15 days. It will be closed if no further activity occurs within 10 days of this comment. |
|
This PR has Quantification details
Why proper sizing of changes matters
Optimal pull request sizes drive a better predictable PR flow as they strike a
What can I do to optimize my changes
How to interpret the change counts in git diff output
Was this comment helpful? 👍 :ok_hand: :thumbsdown: (Email) |
|
WIll see if someone on the team can finish this PR |
SteveL-MSFT
left a comment
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.
I'm ok with the changes as there's an ask to continue to improve this in subsequent PRs
Fix
Restart-Computeron Linux andStop-Computeron MacOS by modifyingRunCommandto respectcommandandargsinputs.PR Summary
Fix
Restart-Computeron Linux systems andStop-Computeron MacOS systems.See issues #14684 and #19738
Made minimal changes necessary to correct behavior.
I am new at contributing to projects and have not done a build or testing. Perhaps someone in the Community or on the PowerShell team can validate functionality.
PR Context
Restart-Computeron Linux has been shutting down systems instead of restarting them..Stop-Computeron MacOS has been incorrectly throwing errors due to missing arguments.PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.(which runs in a different PS Host).