KEMBAR78
Leave the input/output/error handles unset when they are not redirected by daxian-dbw · Pull Request #20853 · PowerShell/PowerShell · GitHub
Skip to content

Conversation

@daxian-dbw
Copy link
Member

@daxian-dbw daxian-dbw commented Dec 5, 2023

PR Summary

This PR is to fix Start-Process to address PowerShell/PSReadLine#288.

Basically, when starting pwsh with Start-Process in the following ways, the new console and the existing console get weirdly tied together, which causes Console.ReadKey running in those 2 consoles to interfere with each other (block and unblock each other):

  • Start-Process pwsh -RedirectStandardError .\error.txt: start pwsh with its stderr redirected to the file error.txt
  • Start-Process pwsh -PassThru -Credential (Get-Credential <another-user>): start pwsh as a different user.

Based on the investigation in PowerShell/PSReadLine#288 (comment), we should not explicitly set the standard input/output/error handles when they are not redirected. Instead, just let Windows figure out the default to use when creating the process.

Note that, the change in TaskbarJumpList.cs is related. When running Start-Process pwsh -Credential as a non-admin user, the "TaskbarJumpList" code throws AccessDenied exception, which causes a debug build to crash. The Debug.Fail is not needed. The catch (ThreadStartException) below simply ignore the exception too.

Manual validation

I verified the changes on both Windows 11 and Windows 10, and it works fine for the abovementioned scenarios. I also verified that the "redirecting input/output/error to files" are still working fine, regardless of using -Credential or not.

PR Checklist

@daxian-dbw daxian-dbw added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Dec 5, 2023
@daxian-dbw daxian-dbw self-assigned this Dec 5, 2023
var CLSID_EnumerableObjectCollection = new Guid(@"2d3468c1-36a7-43b6-ac24-d3f02fd9607a");
const uint CLSCTX_INPROC_HANDLER = 2;
const uint CLSCTX_INPROC = CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER;
var ComSvrInterface_GUID = new Guid(@"555E2D2B-EE00-47AA-AB2B-39F953F6B339");
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 variable is assigned but not used anywhere.

@pull-request-quantifier-deprecated

This PR has 23 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Extra Small
Size       : +9 -14
Percentile : 9.2%

Total files changed: 2

Change summary by file extension:
.cs : +9 -14

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

Copy link
Collaborator

@iSazonov iSazonov left a comment

Choose a reason for hiding this comment

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

What if user redirect only output?

Can we cover the scenario we fix by tests?

@daxian-dbw
Copy link
Member Author

What if user redirect only output?

When starting pwsh with only the output redirected, PSReadLine will not be loaded because it doesn't work when output is redirected. So, this scenario is not in our consideration for the bug.

Can we cover the scenario we fix by tests?

Existing tests for Start-Process still work. The particular issue that was fixed was a pure interactive scenario, so I don't think we can cover it by test.

@ltrzesniewski
Copy link

I'm pretty sure this change broke atuinsh/atuin#2543 - looks like letting Windows figure out the default handle to use doesn't always work as well as expected. 😢

In that PR, I want to redirect stderr to a file while keeping stdout on the screen. After updating PowerShell to v7.5.0, nothing appears on screen anymore on Windows.

The following line now misbehaves:

Start-Process -Wait -NoNewWindow -RedirectStandardError $resultFile.FullName -FilePath atuin -ArgumentList $argString

If I understand correctly, there's no easy workaround for this. You can't supply a custom output handle to ProcessStartInfo, you have to implement all the redirection yourself, which is not something I'm particularly happy to do in a PowerShell script. If you know a better solution, I'd be happy to hear.

@iSazonov
Copy link
Collaborator

@ltrzesniewski Please open new issue with simple repro steps.

@ltrzesniewski
Copy link

Sure, sorry. Reported here: #24986

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Extra Small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants