fix: Fixing a potential race condition
and possibly memory leak
in "powerShellStart"
#696
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.
We've been use
powerShellStart
andpowerShellRelease
in our project to minimise the number "PowerShell" processes created by SystemInformation in order to work around #626.However, we've noticed a couple of potential problems, when
powerShellStart
are called multiple times, beforepowerShellRelease
is called.Imagine the following scenario:
si.powerShellStart()
followed bysi.system(cb)
(or the promise version). It'll callsi.powerShellRelease
once it gets the response in the callback.si.powerShellStart()
followed bysi.osInfo(cb)
.(In reality, function 1 & 2 are in two different "context" therefore have no knowledge of each other regarding when which one will make the call first, hence very tricky to synchronise them.)
There are a couple of issues here:
_psChild
is global therefore it's overwritten by the one created by function 2. The callback of function 1 may never be able to release it (hence possible memory leak), where the "PowerShell" process created by function 2 are released twice.This commit changes
powerShellStart
function so that it first checks if apersisted
"PowerShell" process already existed, and just reuse it if one is available.