-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Improve error handling in OpenProcess method : Windows32Exception - The parameter is incorrect #1700 #1701
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
base: master
Are you sure you want to change the base?
Conversation
…he parameter is incorrect java-native-access#1700 I found the Windows code path that throws in getProcessFilePath(HWND) and implemented a defensive fix so your logs won’t fill with Win32Exceptions anymore. Now, when OpenProcess fails with ERROR_INVALID_PARAMETER (87), it is treated like ACCESS_DENIED: we either retry with PROCESS_QUERY_LIMITED_INFORMATION, or return an empty path if access still fails. This covers the race where a PID disappears between enumeration and open, or other transient/unsupported cases.
|
Hi, Is your email valid? From: helloJetBase-tech 178346048+marktech0813@users.noreply.github.com That seems to be a frequent comment from @matthiasblaesing. What about using a switch instead of adding a local variable? |
|
hi, |
I added a bug-fix entry under “Next Release (5.19.0) → Bug Fixes” in CHANGES.md referencing java-native-access#1700.
I replaced the local variable with a switch on GetLastError in getProcessFilePath(HWND) so we branch on ACCESS_DENIED and INVALID_PARAMETER without storing the error.
|
I replaced the local variable with a switch on GetLastError in getProcessFilePath(HWND) so we branch on ACCESS_DENIED and INVALID_PARAMETER without storing the error. |
marktech0813
left a comment
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.
Let me know if you’d prefer we still cache the error once per branch to be extra defensive, but functionally it’s now switch-based and CHANGES is updated.
|
While we're finessing, how about an early break and fall-through to reduce duplicate code? Within the first switch, rather than checking for null, just break out early if process is assigned. Then the nested switch can just check for the 2 cases, else fall through to the outer switch's default. Maybe better if I just show the code: |
Yes. Functionally identical, but cleaner: Less duplication and shallower nesting via early break and fall-through. No extraneous local state; logic is driven directly by GetLastError. Readability improved while preserving exact semantics (retry on ACCESS_DENIED/INVALID_PARAMETER, otherwise throw; return "" on second failure). Contribution by Gittensor, learn more at https://gittensor.io/
|
Yes. Functionally identical, but cleaner:
|
matthiasblaesing
left a comment
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.
The problem is real and this stack overflow post gives some possible reasons: https://stackoverflow.com/questions/4988082/openprocess-error-87-invalid-parameter
The use the correct parameter to fetch the PID, but the other to reasons (window from system process or different user/terminal services) sounds reasonable.
So I agree with the general idea here.
Nitpicks:
- see inline comments
- please ensure that the commits have valid author information. Valid means a realname and email (this was already brought up by @JaredDavis22 but ignored). You can see the "real" author information in the patch view for a PR. In this case: https://patch-diff.githubusercontent.com/raw/java-native-access/jna/pull/1701.patch
Please squash the commits into a single commit.
|
okay. |
Co-authored-by: Matthias Bläsing <mblaesing@doppel-helix.eu>
Co-authored-by: Matthias Bläsing <mblaesing@doppel-helix.eu>
|
Please squash and update the author information. |
I found the Windows code path that throws in getProcessFilePath(HWND) and implemented a defensive fix so your logs won’t fill with Win32Exceptions anymore. Now, when OpenProcess fails with ERROR_INVALID_PARAMETER (87), it is treated like ACCESS_DENIED: we either retry with PROCESS_QUERY_LIMITED_INFORMATION, or return an empty path if access still fails. This covers the race where a PID disappears between enumeration and open, or other transient/unsupported cases.
In contrib/platform/src/com/sun/jna/platform/WindowUtils.java, updated W32WindowUtils.getProcessFilePath:
On first OpenProcess failure, if GetLastError() is ACCESS_DENIED or INVALID_PARAMETER, we attempt PROCESS_QUERY_LIMITED_INFORMATION.
If the second OpenProcess also fails with ACCESS_DENIED or INVALID_PARAMETER, we return an empty string instead of throwing.
Only other error codes still throw a Win32Exception.