-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Terminate the loop in PsUtils.GetMainModule() which was infinite in case there was no main module. #6358
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
Terminate the loop in PsUtils.GetMainModule() which was infinite in case there was no main module. #6358
Changes from all commits
d71760a
b457c01
5a250bb
80bff4f
5990167
16fd838
9483699
33cacc1
f9a2d56
3f5556d
494e0f0
65fcf99
fd351d8
9182513
f07dd26
3f6b1bd
982a314
4e350e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,13 +59,12 @@ internal static class PsUtils | |
| internal static ProcessModule GetMainModule(Process targetProcess) | ||
| { | ||
| int caughtCount = 0; | ||
| ProcessModule mainModule = null; | ||
|
|
||
| while (mainModule == null) | ||
| while (true) | ||
| { | ||
| try | ||
| { | ||
| mainModule = targetProcess.MainModule; | ||
| return targetProcess.MainModule; | ||
| } | ||
| catch (System.ComponentModel.Win32Exception e) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we catch
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @daxian-dbw what do you think?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should catch
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why we should wait if process already exit? I'd re-throw immediately.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for clarify! |
||
| { | ||
|
|
@@ -81,8 +80,6 @@ internal static ProcessModule GetMainModule(Process targetProcess) | |
| throw; | ||
| } | ||
| } | ||
|
|
||
| return mainModule; | ||
| } | ||
|
|
||
| // Cache of the current process' parentId | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,9 +13,25 @@ Describe "Get-Process for admin" -Tags @('CI', 'RequireAdminOnWindows') { | |
| } | ||
| } | ||
|
|
||
| It "Should support -FileVersionInfo" -Skip:(!$IsWindows) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is wrong for non-Windows. If you run the It "Should support -FileVersionInfo" {
$pwshVersion = ...
if ($IsWindows) {
$pwshVersion | Should -BeExactly $PSVersionTable.PSVersion
} else {
$pwshVersion | Should -BeNullOrEmpty
}
}
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we know that process' FileVersion always would be null or non windows platform?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the FileVersion information is stored as resources in PE file format on Windows whereas Linux uses ELF format which is not compatible with this extended file information. |
||
| It "Should support -FileVersionInfo" { | ||
| $pwshVersion = Get-Process -Id $pid -FileVersionInfo | ||
| $PSVersionTable.PSVersion | Should -Match $pwshVersion.FileVersion | ||
| if ($IsWindows) { | ||
| $PSVersionTable.PSVersion | Should -MatchExactly $pwshVersion.FileVersion | ||
| $gitCommitId = $PSVersionTable.GitCommitId | ||
| if ($gitCommitId.StartsWith("v")) { $gitCommitId = $gitCommitId.Substring(1) } | ||
| $pwshVersion.ProductVersion.Replace("-dirty","") | Should -BeExactly $gitCommitId | ||
| } else { | ||
| $pwshVersion.FileVersion | Should -BeNullOrEmpty | ||
| } | ||
| } | ||
|
|
||
| It "Run with parameter -FileVersionInfo should not hang on non Windows platform also when process' main module is null." -Skip:$IsWindows { | ||
| # Main module for idle process can be null on non-Windows platforms | ||
| { $pwshVersion = Get-Process -Id 0 -FileVersionInfo -ErrorAction Stop } | Should -Not -Throw | ||
| } | ||
|
|
||
| It "Run with parameter -FileVersionInfo for idle process should throw on Windows." -Skip:(!$IsWindows) { | ||
| { $pwshVersion = Get-Process -Id 0 -FileVersionInfo -ErrorAction Stop } | Should -Throw -ErrorId "CouldNotEnumerateFileVer,Microsoft.PowerShell.Commands.GetProcessCommand" | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
mainModule == null, is the expected behavior to be writing out nothing or throwing an error?On windows, the current behavior is to throw error when the
FileInfoVersionis not accessible.@JamesWTruher Your insight would be helpful here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related comment from CoreFX https://github.com/dotnet/corefx/blob/f90aa6665a59dc6caaff995a79fb852d21fe6f68/src/System.Diagnostics.Process/src/System/Diagnostics/Process.Windows.cs#L176
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's also possible on non-windows platforms that mainmodule could be null due to the underlying corefx implementation (ex: process id 0). Throwing an error doesn't seem reasonable.