Skip to content

SOLR-17717 Use Powershell instead of WMIC to get cmdline on Windows #3291

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

Merged
merged 10 commits into from
Apr 17, 2025

Conversation

janhoy
Copy link
Contributor

@janhoy janhoy commented Mar 27, 2025

https://issues.apache.org/jira/browse/SOLR-17717

The same information as we fetch with WMIC comamnd can be fetched with powershell.exe. It is slower, but more universal. Alternative would be native code via the OSHI library, but too heavy to include for just this one use.

@janhoy janhoy requested a review from Copilot March 27, 2025 10:56
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR addresses the issue with starting Solr on newer Windows 11 Home editions by introducing a fallback mechanism to fetch process command line information using PowerShell when WMIC is not available.

  • Added a static flag (wmicAvailable) to check WMIC availability on Windows.
  • Implemented a fallback in commandLine(...) to use PowerShell when WMIC is missing.
  • Introduced helper methods: isWmicAvailable, commandLineViaWmic, and commandLineViaPowershell.
Files not reviewed (1)
  • solr/CHANGES.txt: Language not supported
Comments suppressed due to low confidence (2)

solr/core/src/java/org/apache/solr/cli/SolrProcessManager.java:182

  • In the commandLineViaWmic method, consider adding a process.waitFor() call after starting the process to ensure that WMIC has completed execution and produced all output before reading from its input stream.
Process process = new ProcessBuilder(

solr/core/src/java/org/apache/solr/cli/SolrProcessManager.java:182

  • In isWmicAvailable, consider re-setting the thread's interrupt status when catching InterruptedException (e.g., by calling Thread.currentThread().interrupt()) to avoid unintentionally swallowing the interrupt flag.
Process process = new ProcessBuilder("wmic", "--version").start();

Copy link
Contributor

@epugh epugh left a comment

Choose a reason for hiding this comment

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

LGTM.

One question about the approach... Could we JUST use powershell and not keep wmic support? Eventually it will be gone... Powershell has been around for quite a while now....

@janhoy
Copy link
Contributor Author

janhoy commented Mar 27, 2025

I'm not fluent enough in Windows world to know whether powershell.exe is available on all home, pro and server versions, so due to lack of integration tests on windows, it feels safer to handle it as a fallback for the existing approach. I wish Java implemented this for us in ProcessHandle on Windows too. ph.info().commandLine() is so much nicer than these workarounds.

@janhoy
Copy link
Contributor Author

janhoy commented Mar 27, 2025

It is tempting to just defer this complexity to https://github.com/oshi/oshi as we discussed in the original feature issue.

@uschindler
Copy link
Contributor

uschindler commented Mar 27, 2025

Powershell is available in Windows 10 and 11 for all variants. Servers have it, too. Windows 8 also has Powershell but its out of date. Windows 7 can install it. If anybody complains about Windows XP, I'm out of arguments.

@epugh
Copy link
Contributor

epugh commented Mar 27, 2025

It is tempting to just defer this complexity to https://github.com/oshi/oshi as we discussed in the original feature issue.

I thought you found a limitation that prevented oshi being viable? It looks like an active maintained library ;-).

@uschindler
Copy link
Contributor

It is tempting to just defer this complexity to https://github.com/oshi/oshi as we discussed in the original feature issue.

I thought you found a limitation that prevented oshi being viable? It looks like an active maintained library ;-).

Please don't add new libraries (especially those with native code) just for a single function call. I'd remove the wmic support completely. It might also no longer be installed on freshly installed Windows 11 Pro or Servers. If you add libraries with native code, you need to whitelist them in later Java versions, because by default next LTS will disable native code via JNI and Panama. So it is now mandatory to add the command line option to enable it for Lucene Core (see recent upgrades to build scripts).

Uwe

@janhoy
Copy link
Contributor Author

janhoy commented Mar 27, 2025

Thanks for the review. I removed RMIC support. I have tested the PowerShell option on my UTM Win11 ARM VM and it works. However, my user is administrator, so I'd need to test with an unprivileged user as well. Feel free to give it a spin if you are on Win..

@janhoy
Copy link
Contributor Author

janhoy commented Mar 28, 2025

Did some more testing and a weakness of the solution is that the Powershell process is spawned once for every Java process running on the system, and each invocation takes some 3-5 seconds on the Win11 VM on my MacBook. Don't know if such slow invocation is representative for a real windows system, but it is an overhead. I have not done a comparison for the old wmic invocation. Manual command invocation in a PS terminal takes <1s. While manually invoking powershell.exe -Command "foo" from a CMD terminal takes 2+ secs, perhaps there's a startup cost in starting powershell process.

So I refactored the code to instead of calling PS once per PID, we do one full PID list on startup with

"Get-CimInstance -ClassName Win32_Process | Where-Object { $_.Name -like '*java*' } | Select-Object ProcessId, CommandLine | ConvertTo-Json -Depth 1"

and stuffs it into a Map<Long, String> for later lookup.

The OSHI library, using jna would probably be far more performant, but I agree that adding a 1Mb jar just for this is too much.

@github-actions github-actions bot added the tests label Mar 28, 2025
@uschindler
Copy link
Contributor

Don't know if such slow invocation is representative for a real windows system, but it is an overhead.

Powershell is slow to start. Yes. Also on real machines.

janhoy added 2 commits March 28, 2025 12:27
Simplify stream parsing
Check for invocation success
@janhoy janhoy changed the title SOLR-17717 Starting solr on newer Windows 11 Home complained about WMIC SOLR-17717 Use Powershell instead of WMIC to get cmdline on Windows Mar 29, 2025
# Conflicts:
#	solr/CHANGES.txt
@janhoy
Copy link
Contributor Author

janhoy commented Apr 17, 2025

Coming back to this. Lacking test suite on multiple windows versions makes such changes brittle. I hope the iterations we have done here and the reviews is enough to merge. And then hope the fix is not worse than what it replaces, except for perhaps added latency launching powershell. I'll go ahead and merge to main for now.

@janhoy janhoy merged commit 6315d3f into apache:main Apr 17, 2025
4 checks passed
@janhoy janhoy deleted the bugfix/SOLR-17717-wmic branch April 17, 2025 21:28
janhoy added a commit to janhoy/solr that referenced this pull request Apr 17, 2025
janhoy added a commit to janhoy/solr that referenced this pull request Apr 17, 2025
janhoy added a commit to janhoy/solr that referenced this pull request Apr 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants