X Tutup
The Wayback Machine - https://web.archive.org/web/20221221054536/https://github.com/PowerShell/PowerShell/pull/17545
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

Fix Parent property on processes with complex name #17545

Merged
merged 6 commits into from Sep 29, 2022

Conversation

jborean93
Copy link
Collaborator

@jborean93 jborean93 commented Jun 20, 2022

PR Summary

Fix the Parent property to retrieve the parent process on Linux where
the process name has complex characters like a space or parenthesis.

PR Context

Reads from /proc/[pid]/status rather than /proc/[pid]/stat on Linux to get the parent process id. The status file is nicer as each entry is on a newline and has a label making it easier to select the data required. This is an alternative approach to deal with process' with a space and parenthesis in the name avoiding the last index of logic or trying to correctly select the proper column in the stat file.

Fixes #12908
Fixes #17541

Alternative to #12925

PR Checklist

return invalidPid;
Match ppidMatch = Regex.Match(
line,
@"^PPid:\s+(\d+)$",
Copy link
Collaborator

@iSazonov iSazonov Jun 21, 2022

Choose a reason for hiding this comment

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

The proposal looks very expensive. I'd prefer #12925.

Copy link
Collaborator Author

@jborean93 jborean93 Jun 21, 2022

Choose a reason for hiding this comment

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

Is the concern around the regex, the reading of multiple lines or something else? If it's the regex this could be changes to just doing line.StartsWith("PPid: ") and extract it from there but I thought regex gives some nicer validation.

My only concern about #12925 is if any extra entries are added to the stat file in the future that could mess with the LastIndexOf(')') check. I have no idea what the chances are of that happening but the file certainly has had additions in the past so I can't rule it out.

Copy link
Member

@JamesWTruher JamesWTruher Jul 18, 2022

Choose a reason for hiding this comment

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

the additional cost of the regex is probably not worth it as the StartsWith("PPid: ") will do the trick. If you're really stuck on using regex, i'd be much happier if this was compiled once and continually reused.

Copy link
Member

@SteveL-MSFT SteveL-MSFT Aug 22, 2022

Choose a reason for hiding this comment

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

@jborean93 is there any concern using StartsWith() and then splitting out the pid trimming the whitespace instead of a regex?

Copy link
Collaborator Author

@jborean93 jborean93 Aug 22, 2022

Choose a reason for hiding this comment

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

The regex just ensure it's PPid: digit but considering this is probably mandated in the spec it's probably not something to be concerned about. I can change it to StartsWith("PPid: ") but didn't think regex would be that bad of a performance problem and is somewhat simpler to extract the match.

Copy link
Member

@daxian-dbw daxian-dbw Sep 12, 2022

Choose a reason for hiding this comment

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

@jborean93 Any chance to get the proposed changes done? Thanks!

Copy link
Collaborator Author

@jborean93 jborean93 Sep 12, 2022

Choose a reason for hiding this comment

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

Sorry will get to this next week, just on holidays at the moment.

Copy link
Collaborator Author

@jborean93 jborean93 Sep 19, 2022

Choose a reason for hiding this comment

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

Has been updated to use StartsWith instead as per the suggestion.

@msftbot msftbot bot added the Review - Needed The PR is being reviewed label Jun 28, 2022
@msftbot
Copy link

msftbot bot commented Jun 28, 2022

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@daxian-dbw daxian-dbw requested a review from JamesWTruher Jun 28, 2022
@daxian-dbw
Copy link
Member

daxian-dbw commented Jun 28, 2022

@JamesWTruher can you please review this PR?

@msftbot msftbot bot removed the Review - Needed The PR is being reviewed label Jun 28, 2022
@msftbot msftbot bot added the Review - Needed The PR is being reviewed label Jul 6, 2022
@msftbot
Copy link

msftbot bot commented Jul 6, 2022

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@daxian-dbw daxian-dbw added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Review - Needed The PR is being reviewed labels Sep 12, 2022
@msftbot msftbot bot removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Sep 12, 2022
jborean93 added 2 commits Sep 19, 2022
Fix the `Parent` property to retrieve the parent process on Linux where
the process name has complex characters like a space or parenthesis.
@jborean93
Copy link
Collaborator Author

jborean93 commented Sep 19, 2022

Looks like there might be a problem with using testdrive: in Pester. The permissions don't allow an unauthenticated users to set the executable bit breaking the tests for that scenario. Any suggestions on what to do there. I don't fully know how the tests are actually set up here.

@pull-request-quantifier
Copy link

pull-request-quantifier bot commented Sep 20, 2022

This PR has 47 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       : +34 -13
Percentile : 18.8%

Total files changed: 2

Change summary by file extension:
.cs : +13 -6
.ps1 : +21 -7

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? 👍  👌  👎 (Email)
Customize PullRequestQuantifier for this repository.

@jborean93
Copy link
Collaborator Author

jborean93 commented Sep 20, 2022

Looks like there might be a problem with using testdrive: in Pester

Using /tmp as the path was enough to get it passing. Because it only runs on non-Windows that should be fine.

Copy link
Collaborator

@iSazonov iSazonov left a comment

LGTM in common.

If we don't want extra allocations, we could replace Split() by ReadonlySpan.TrimStart()

while true; do sleep 1; done
'@

# Can't use testdrive: as unelevated user doesn't have perms in test in CI
Copy link
Collaborator

@iSazonov iSazonov Sep 21, 2022

Choose a reason for hiding this comment

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

Please reword:

Suggested change
# Can't use testdrive: as unelevated user doesn't have perms in test in CI
# Can't use 'Testdrive:' as unelevated user doesn't have permissions to do 'chmod'.

I guess it is because .Net 7 RC1 has a breaking change in Directory.Create() where chmod mask was changed. If so, it might still break something in PowerShell or Pester.
/cc @nohwnd

Copy link
Collaborator Author

@jborean93 jborean93 Sep 21, 2022

Choose a reason for hiding this comment

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

I assumed this was just a byproduct of how PowerShell CI is set up. I've not looked into it but I believe the unelevated user may potentially be inheriting the same tmpdir as the elevated account and thus cannot set the execute bit on files in that dir. This is just a guess so your assumption could also be true.

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Sep 21, 2022
Copy link
Member

@daxian-dbw daxian-dbw left a comment

LGTM. Thanks @jborean93!

@msftbot msftbot bot added the Review - Needed The PR is being reviewed label Sep 29, 2022
@msftbot
Copy link

msftbot bot commented Sep 29, 2022

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@iSazonov
Copy link
Collaborator

iSazonov commented Sep 29, 2022

@daxian-dbw Ready to merge?

@msftbot msftbot bot removed the Review - Needed The PR is being reviewed label Sep 29, 2022
@daxian-dbw daxian-dbw merged commit d66b2a5 into PowerShell:master Sep 29, 2022
39 checks passed
@jborean93 jborean93 deleted the linux-ppid branch Sep 29, 2022
@jborean93
Copy link
Collaborator Author

jborean93 commented Sep 29, 2022

Thanks everyone for your reviews and suggestions.

@iSazonov
Copy link
Collaborator

iSazonov commented Sep 29, 2022

@jborean93 Thanks for your contribution!

@TravisEz13 TravisEz13 mentioned this pull request Sep 30, 2022
22 tasks
@jborean93 jborean93 mentioned this pull request Nov 18, 2022
22 tasks
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
5 participants
X Tutup