Powershell: Process.Parent property is null on Linux if the process name contains a space

Created on 5 Jun 2020  路  5Comments  路  Source: PowerShell/PowerShell

Discovered in #12894

https://github.com/PowerShell/PowerShell/blob/5ef69e88bc2a6e500349a8eec8d13bb27e143e3e/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs#L972-L985
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
First-Time-Issue Issue-Bug OS-Linux Up-for-Grabs WG-Engine

All 5 comments

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.

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.

@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?

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.

Thanks for the info! I have raised PR #12925

Was this page helpful?
0 / 5 - 0 ratings

Related issues

manofspirit picture manofspirit  路  3Comments

JohnLBevan picture JohnLBevan  路  3Comments

SteveL-MSFT picture SteveL-MSFT  路  3Comments

rkeithhill picture rkeithhill  路  3Comments

rudolfvesely picture rudolfvesely  路  3Comments