Fix spawned wmic processes not exiting when using pidusage in packaged apps #186
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
With the recent addition of gwmi support on Windows, a
spawn
call was introduced to check for availability of wmic and switch to gwmi on failure.This works fine by default, but introduces a new problem in cases where pidusage is being utilized in a packaged application such as with a tool like pkg or its actively maintained successor fork. In a packaged app, the spawned WMI processes don't get killed, which leads to a situation where each pidusage call creates a new process and they just infinitely stack up until the computer becomes unstable and/or runs out of memory.
I am someone who uses pidusage in packaged applications, so I found a simple solution to address this and created this PR. I modified lib/stats.js to now hold a reference to the spawned child wmic process, and I added a call to kill it in a finally block added to the try/catch. This maintains existing functionality while ensuring the child process is killed when this try/catch check is completed and works in both packaged and non-packaged environments.
For reference, this is what the task manager will look like when this situation occurs, with a new process forming on each pidusage call: