Skip to content

Process.Parent property is null on Linux if the process name contains a space #12908

Closed

Description

@iSazonov

Discovered in #12894

// read /proc/<pid>/stat
// 4th column will contain the ppid, 92 in the example below
// ex: 93 (bash) S 92 93 2 4294967295 ...
var path = $"/proc/{pid}/stat";
try
{
var stat = System.IO.File.ReadAllText(path);
var parts = stat.Split(' ', 5);
if (parts.Length < 5)
{
return invalidPid;
}
return Int32.Parse(parts[3]);

If a process name contains a space (like (ConsoleHost mai) vs (bash)) the split returns additional extra element and we get wrong value with parts[3].

The fix could be - search ) to skip the process name.

/proc/[pid]/stat format for reference https://man7.org/linux/man-pages/man5/proc.5.html

Steps to reproduce

(Get-Process -Name 'process name').Parent

Expected behavior

# Returns parent process object

Actual behavior

# Returns null

Environment data

pwsh 7.1 Preview3

Activity

  1. added
    Issue-Bug
    WG-Engine
    Up-for-Grabs
    First-Time-Issue
    on Jun 5, 2020
  2. rajdeep-biswas commented on Jun 8, 2020

    @rajdeep-biswas

    I'm willing to contribute assuming help is still wanted. I have found a fix by modifying CorePsPlatform.cs, solely. The fix involves skipping the process name, as suggested. I will open a PR once I have approval. :)

    note: this is going to be my first ever contribution to open source, so please help me out if I'm missing out any documentation conventions as is considered standard for PRs to this repository.

  3. NoMoreFood commented on Jun 9, 2020

    @NoMoreFood
    Contributor

    Keep in mind that you could have a process name itself with one or more parentheses. Something like:

    17718 (really weird process( ) name) T 15873 17718 15873 34816 ....

    Since, per standard, the rest of the line should all be numeric fields, it might be valuable to look for the ')' in reverse and then parse from there. You could also do a regular expression, but you have to anchor it to the start and end to ensure proper parsing:

    '^\d+ \(.+\) [A-Z] (?<parent>\d+)(?: \d+){48}$'

    In the above, the 'parent' named capture group would be the parent PID. The regular expression, while computationally expensive, is probably the most robust way in my opinion.

  4. rajdeep-biswas commented on Jun 9, 2020

    @rajdeep-biswas

    Bryan Berns (@NoMoreFood) I have accounted for the last occurring ')' using Substring and LastIndexOf, I had a feeling that will be less expensive than a regex but still work everytime, without fail.

    I have a question regarding your standard process of raising a PR; will I have to fork the repo and submit a branch from there or will I be added as a collaborator and directly set an upstream branch to this repo?

  5. NoMoreFood commented on Jun 9, 2020

    @NoMoreFood
    Contributor

    Standard practice is to fork the repository, create a branch in your repository, and then
    submit a pull request to merge changes in that branch into the upstream. One the pull is submitted, this project has a bunch of test that will kick in for coding style and regression testing so watch your PR to make sure those all pass. You'll also likely be asked to create a regression test for this problem.

  6. rajdeep-biswas commented on Jun 9, 2020

    @rajdeep-biswas

    Thanks for the info! I have raised PR #12925

  7. ghost added
    In-PR
    on Jun 20, 2022
  8. ghost removed
    In-PR
    on Sep 29, 2022
  9. ghost added
    Resolution-Fixed
    on Sep 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    First-Time-IssueIssue-BugOS-LinuxResolution-FixedUp-for-GrabsWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions