Skip to content
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

fix: Fixing a potential race condition and possibly memory leak in "powerShellStart" #696

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 29 additions & 19 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,32 +396,42 @@ function powerShellProceedResults(data) {
}

function powerShellStart() {
// Reuse existing persisted process, if one is available
if (_psPersistent && _psChild && _psChild.pid){
return;
}

// If somehow _psPersistent is not in sync with _psChild,
// just kill it and start a new one.
if (!_psPersistent && _psChild && _psChild.pid) {
powerShellRelease();
}

_psChild = spawn('powershell.exe', ['-NoLogo', '-InputFormat', 'Text', '-NoExit', '-Command', '-'], {
stdio: 'pipe',
windowsHide: true,
maxBuffer: 1024 * 20000,
encoding: 'UTF-8',
env: util._extend({}, process.env, { LANG: 'en_US.UTF-8' })
});
if (_psChild && _psChild.pid) {
_psPersistent = true;
_psChild.stdout.on('data', function (data) {
_psResult = _psResult + data.toString('utf8');
if (data.indexOf(_psCmdSeperator) >= 0) {
powerShellProceedResults(_psResult);
_psResult = '';
}
});
_psChild.stderr.on('data', function () {
powerShellProceedResults(_psResult + _psError);
});
_psChild.on('error', function () {
powerShellProceedResults(_psResult + _psError);
});
_psChild.on('close', function () {
_psChild.kill();
});
}

_psPersistent = true;
_psChild.stdout.on('data', function (data) {
_psResult = _psResult + data.toString('utf8');
if (data.indexOf(_psCmdSeperator) >= 0) {
powerShellProceedResults(_psResult);
_psResult = '';
}
});
_psChild.stderr.on('data', function () {
powerShellProceedResults(_psResult + _psError);
});
_psChild.on('error', function () {
powerShellProceedResults(_psResult + _psError);
});
_psChild.on('close', function () {
_psChild.kill();
});
}

function powerShellRelease() {
Expand Down