Skip to content

Fix: Process.Parent property is null on Linux if the process name contains a space - #12925

Closed
Rajdeep Biswas (rajdeep-biswas) wants to merge 7 commits into
PowerShell:masterfrom
rajdeep-biswas:bugfix-12908
Closed

Rajdeep Biswas (rajdeep-biswas) wants to merge 7 commits into
PowerShell:masterfrom
rajdeep-biswas:bugfix-12908

Conversation

@rajdeep-biswas

@rajdeep-biswas Rajdeep Biswas (rajdeep-biswas) commented Jun 9, 2020 •

Copy link
Copy Markdown

PR Summary

Changed code in CorePsPlatform.cs so that process names with spaces in them can be handled.

PR Context

Fix for #12908
This was caused by the inconsistency in the index position after split, which has been fixed here by only considering the substring which stats after the last closing parenthesis ')' and hence it will always have a consistent index for process ID after split()ing.
Note: first ever PR, please be lenient with my documentation. I'm open to suggestions for any changes.

PR Checklist

@iSazonov Ilya (iSazonov) left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please add a test. (I guess we could change a process name by changing main thread name.)

@rajdeep-biswas

Copy link
Copy Markdown
Author

Please add a test.

Hi, I went through the testing guide but couldn't find an exact detail of how to add a test to the code, could you please point me to an example?

@iSazonov

Ilya (iSazonov) commented Jun 10, 2020 •

Copy link
Copy Markdown
Collaborator

@therajdeepbiswas

You should add a test to Get-Process.Tests.ps1 file.
The test should be skipped if not $IsLinux.
I guess we can not rename process on the fly so we have to create an executable file with appropriate name (like "T ( E ( S ) T)") and run it. Perhaps we could use https://stackoverflow.com/questions/11130229/start-a-process-with-a-name

Stack Overflow
Basically I want to dynamically start some processes which may create their own children processes, also I want to kill a certain group of processes I just created whenever I want. One way I could ...

@rajdeep-biswas

Rajdeep Biswas (rajdeep-biswas) commented Jun 10, 2020 •

Copy link
Copy Markdown
Author

create an executable file with appropriate name (like "T ( E ( S ) T)") and run it.

Ilya (@iSazonov) I don't think it is possible to assign an alias with special characters from within PowerShell (so as to be executed from a testcase). If I were to do it from bash, that alias wouldn't carry over to PowerShell when I run the latter. And even if I'm going to create a custom executable, that extra file will always be hardcoded for that one test case, isn't it?

I thought it would be better to create a temporary file for one manual test.
I called it "t(e(s)t)" and the contents of it are as follows
#!/bin/bash
pwsh

After executing this from bash, it launched PowerShell as expected and on running Get-Process, you can note that it was indeed listed in the process list as shown below in the screenshot.

image

And indeed the issue seems to have been solved from the one manual test, as shown below.

image

I don't think including this one file to run testcases on it time and again makes any sense. So it will be unnecessary to include the hardcoded testcase, however, I have added a testcase that checks if the parent property returned is not not null when looked up using process name (and not process ID). Adding in next commit.

@ghost

Deleted user (ghost) commented Jun 10, 2020 •

Copy link
Copy Markdown

CLA assistant check
All CLA requirements met.

@rajdeep-biswas

Copy link
Copy Markdown
Author

The CI test for macos is failing on a pester file I did not change anything in. Please suggest me how to fix this.

@iSazonov Ilya (iSazonov) left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@therajdeepbiswas Your test never checks the new code path on Linux. If we remove the cs code the test will pass.

…ocess.Tests.ps1


made changes as suggested

Co-authored-by: Ilya <darpa@yandex.ru>
@Araon

Copy link
Copy Markdown

Lmao

@rajdeep-biswas

Copy link
Copy Markdown
Author

Ilya (@iSazonov) what now?

@iSazonov

Copy link
Copy Markdown
Collaborator

@therajdeepbiswas Please fix the test and make the test positive to cover the new code path.

Your test never checks the new code path on Linux. If we remove the cs code the test will pass.

@rajdeep-biswas

Rajdeep Biswas (rajdeep-biswas) commented Jun 10, 2020 •

Copy link
Copy Markdown
Author

@therajdeepbiswas Please fix the test and make the test positive to cover the new code path.

Ilya (@iSazonov) My test file, in itself is working when executed offline using Start-PSPester -Path test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1, and the new testcase which I wrote seems to be passing as can be inferred from the screenshot below.

image

The CI test for macos is failing on a pester file I did not change anything in.

and, as I mentioned earlier, the build was failing on a test file I hadn't modified at all.
The following results are from the automated tests
Check failure on line 1 in Test-Connection.Error occurred in Describe block
azure-pipelines / PowerShell-CI-macos
Test-Connection.Error occurred in Describe block
Exception calling "GetHostEntry" with "1" argument(s): "nodename nor servname provided, or not known"
Raw Output: At /Users/runner/runners/2.169.1/work/1/s/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1:15 char:9

Please note that Test-Connection.Tests.ps1 is a file I did not modify.

and make the test positive to cover the new code path.

PS: also, I'm not sure what you mean by 'cover the new code path', could you please care to elaborate?

@iSazonov

Ilya (iSazonov) commented Jun 11, 2020 •

Copy link
Copy Markdown
Collaborator

the build was failing on a test file I hadn't modified at all.

It is because there is a bug in .Net. (Process name is "ConsoleHost mai".) After the bug will be fixed your test will pass. You should create a process name so that your new code is executed in the new test.

@rajdeep-biswas

Copy link
Copy Markdown
Author

create a process name

By this do you mean a test that invokes the Get-Process function which I modified?

@iSazonov

Ilya (iSazonov) commented Jun 11, 2020 •

Copy link
Copy Markdown
Collaborator

Yes. You could use Bash sample you mentioned above.

@rajdeep-biswas

Copy link
Copy Markdown
Author

That can only be done through a manual test, right? How can you write a pester test that executes a custom bash file?

@iSazonov

Copy link
Copy Markdown
Collaborator

You can create a bash script file on fly, set exec flag by chmod +x and run the script from pwsh.

PS /home/sie> & './t t.sh'

 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
      0     0.00       4.97       0.40       8   8 bash
      0     0.00      99.42       1.51    2412   8 ConsoleHost mai
      0     0.00      85.93       0.70    2440   8 ConsoleHost mai
      0     0.00       0.54       0.02       1   0 init
      0     0.00       0.08       0.00       6   6 init
      0     0.00       0.08       0.39       7   6 init
      0     0.00       3.11       0.00    2439   8 t t.sh

@rajdeep-biswas

Copy link
Copy Markdown
Author

Ilya (@iSazonov) please check if this is what you are talking about.

@rajdeep-biswas

Copy link
Copy Markdown
Author

Ilya (@iSazonov) Done as you suggested. Please check.

@adityapatwardhan

Copy link
Copy Markdown
Member

@therajdeepbiswas Please have a look at the failures.

@rajdeep-biswas

Rajdeep Biswas (rajdeep-biswas) commented Jun 12, 2020 •

Copy link
Copy Markdown
Author

Hi Aditya Patwardhan (@adityapatwardhan)
I think the macos fails are being caused due to how Ilya (@iSazonov) describe the .net bug.

the build was failing on a test file I hadn't modified at all.

It is because there is a bug in .Net. (Process name is "ConsoleHost mai".) After the bug will be fixed your test will pass. You should create a process name so that your new code is executed in the new test.

And about the linux fails, I think they're because of an unbuilt version of pwsh. (The pwsh in the current branch is still running the version that is not bug free, right?)

@ghost ghost removed the Review - Needed The PR is being reviewed label Jul 28, 2020
@rajdeep-biswas

Rajdeep Biswas (rajdeep-biswas) commented Jul 28, 2020 •

Copy link
Copy Markdown
Author

@therajdeepbiswas I think current test could be removed.

Perhaps Aditya Patwardhan (@adityapatwardhan) has an idea about adding test or agree to merge without test.

Waiting on confirmation about this, Ilya (@iSazonov)

@ghost ghost added the Review - Needed The PR is being reviewed label Aug 4, 2020
@ghost

Copy link
Copy Markdown

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
Copy Markdown
Collaborator

@therajdeepbiswas Since we moved to .Net 6.0 please rebase.

@ghost ghost removed the Review - Needed The PR is being reviewed label Feb 14, 2021
@ghost ghost added the Review - Needed The PR is being reviewed label Feb 22, 2021
@ghost

Copy link
Copy Markdown

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

@adityapatwardhan

Copy link
Copy Markdown
Member

@therajdeepbiswas yes, please rebase.

@ghost ghost removed the Review - Needed The PR is being reviewed label Apr 20, 2021
@ghost ghost added the Review - Needed The PR is being reviewed label Apr 27, 2021
@ghost

Copy link
Copy Markdown

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

@adityapatwardhan

Copy link
Copy Markdown
Member

@therajdeepbiswas The test you have added is failing please have a look.

@ghost ghost removed the Review - Needed The PR is being reviewed label Sep 2, 2021
@ghost ghost added the Review - Needed The PR is being reviewed label Sep 9, 2021
@ghost

Copy link
Copy Markdown

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

@ghost ghost removed the Review - Needed The PR is being reviewed label Sep 15, 2021
@ghost ghost added the Review - Needed The PR is being reviewed label Sep 23, 2021
@ghost

Copy link
Copy Markdown

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

It "Has Parent process property in linux" -Skip:(-not $IsLinux) {
# Added new test to check for bug as described in issue https://github.com/PowerShell/PowerShell/issue/12908
$powershellexe = (Get-Process -Name "$((Get-Process -Id $PID).Name)").mainmodule.filename
& $powershellexe -noprofile -command "Set-Content -Path .\'t ( e ( s ) t ).txt' -Value '#!/bin/bash`r`n./$PSHOME/pwsh`r`nchmod +x ./ " + "'t ( e ( s ) t )\'"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You don't and shouldn't create a new pwsh to run these commands. Here's what I recommend changing this to (you'll need to test this as I'm typing it freehand in this edit box):

$commandName = 't ( e ( s ) t )'

$script = @"
#!/bin/sh
pwsh -noprofile -command 'while ($true) { Start-Sleep -Seconds 10 }'
"@

Set-Content -Path testdrive:/$commandName -Value $script
chmod +x (resolve-path testdrive:/$commandName -Relative)
try {
  $p = start-process -FilePath testdrive:/$commandName -PassThru
  (Get-Process $commandName).Parent | Should -Be $pid
}
finally {
  $p | Stop-Process
}

@ghost ghost 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 Jun 13, 2022
@ghost ghost added the Stale label Jun 28, 2022
@ghost

Copy link
Copy Markdown

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.

@ghost ghost closed this Jul 9, 2022
This pull request was closed.
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 Stale Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants