Skip to content

Commit d1dc105

Browse files
committed
change processor count, revert TryReadPSDataFaile
1 parent 5ce9bbb commit d1dc105

3 files changed

Lines changed: 64 additions & 58 deletions

File tree

src/code/FindHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,10 +1179,10 @@ internal void FindDependencyPackagesHelper(ServerApiCall currentServer, Response
11791179
if (currentPkg.Dependencies.Length > 0)
11801180
{
11811181
// If finding more than 5 packages, do so concurrently
1182-
const int PARALLEL_THRESHOLD = 5; // TODO: Trottle limit from user, defaults to 5;
1182+
//const int PARALLEL_THRESHOLD = 5; // TODO: Trottle limit from user, defaults to 5;
11831183
int processorCount = Environment.ProcessorCount;
11841184
int maxDegreeOfParallelism = processorCount * 4;
1185-
if (currentPkg.Dependencies.Length > PARALLEL_THRESHOLD)
1185+
if (currentPkg.Dependencies.Length > processorCount)
11861186
{
11871187
Parallel.ForEach(currentPkg.Dependencies, new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism }, dep =>
11881188
{

src/code/InstallHelper.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ private ConcurrentDictionary<string, Hashtable> InstallParentAndDependencyPackag
924924
// TODO: figure out a good threshold and parallel count
925925
int processorCount = Environment.ProcessorCount;
926926
_cmdletPassedIn.WriteDebug($"parentAndDeps.Count is {parentAndDeps.Count}, processor count is: {processorCount}");
927-
if (parentAndDeps.Count > 0)
927+
if (parentAndDeps.Count > processorCount)
928928
{
929929
_cmdletPassedIn.WriteDebug($"parentAndDeps.Count is greater than processor count");
930930
// Set the maximum degree of parallelism to 32? (Invoke-Command has default of 32, that's where we got this number from)
@@ -938,12 +938,6 @@ private ConcurrentDictionary<string, Hashtable> InstallParentAndDependencyPackag
938938

939939
Stream responseStream = currentServer.InstallPackage(depPkgName, depPkgVersion, true, out ErrorRecord installNameErrRecord);
940940

941-
var nullresp = false;
942-
if (responseStream == null)
943-
{
944-
nullresp = true;
945-
}
946-
947941
// if (currentServer.Repository.ApiVersion == PSRepositoryInfo.APIVersion.V2)
948942
// {
949943
// // See if the network call we're making is already cached, if not, call FindNameAsync() and cache results
@@ -1103,10 +1097,10 @@ private bool TryInstallToTempPath(
11031097
{
11041098
var pathToFile = Path.Combine(tempInstallPath, $"{pkgName}.{normalizedPkgVersion}.zip");
11051099
using var fs = File.Create(pathToFile);
1106-
if (responseStream == null)
1107-
{
1108-
throw new InvalidOperationException("responseStream is null");
1109-
}
1100+
// if (responseStream == null)
1101+
// {
1102+
// throw new InvalidOperationException("responseStream is null");
1103+
// }
11101104
responseStream.Seek(0, System.IO.SeekOrigin.Begin);
11111105
responseStream.CopyTo(fs);
11121106
fs.Close();

src/code/Utils.cs

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,66 +1357,78 @@ private static bool TryReadPSDataFile(
13571357
runspace.Open();
13581358
runspace.SessionStateProxy.LanguageMode = PSLanguageMode.ConstrainedLanguage;
13591359

1360-
// Set the created runspace as the default for the current thread
1361-
//Runspace.DefaultRunspace = runspace;
1362-
1363-
using (System.Management.Automation.PowerShell pwsh = System.Management.Automation.PowerShell.Create())
1360+
// Save and set the default runspace for the current thread to prevent
1361+
// stale DefaultRunspace from a prior operation on this reused thread-pool thread.
1362+
Runspace previousDefaultRunspace = Runspace.DefaultRunspace;
1363+
try
13641364
{
1365-
pwsh.Runspace = runspace;
1366-
1367-
var cmd = new Command(
1368-
command: contents,
1369-
isScript: true,
1370-
useLocalScope: true);
1371-
cmd.MergeMyResults(
1372-
myResult: PipelineResultTypes.Error | PipelineResultTypes.Warning | PipelineResultTypes.Verbose | PipelineResultTypes.Debug | PipelineResultTypes.Information,
1373-
toResult: PipelineResultTypes.Output);
1374-
pwsh.Commands.AddCommand(cmd);
1375-
1365+
Runspace.DefaultRunspace = runspace;
13761366

1377-
try
1367+
using (System.Management.Automation.PowerShell pwsh = System.Management.Automation.PowerShell.Create())
13781368
{
1379-
// Invoke the pipeline and retrieve the results
1380-
var results = pwsh.Invoke();
1369+
pwsh.Runspace = runspace;
1370+
1371+
var cmd = new Command(
1372+
command: contents,
1373+
isScript: true,
1374+
useLocalScope: true);
1375+
cmd.MergeMyResults(
1376+
myResult: PipelineResultTypes.Error | PipelineResultTypes.Warning | PipelineResultTypes.Verbose | PipelineResultTypes.Debug | PipelineResultTypes.Information,
1377+
toResult: PipelineResultTypes.Output);
1378+
pwsh.Commands.AddCommand(cmd);
13811379

1382-
if (results[0] is PSObject pwshObj)
1380+
try
13831381
{
1384-
switch (pwshObj.BaseObject)
1382+
// Invoke the pipeline and retrieve the results
1383+
var results = pwsh.Invoke();
1384+
1385+
if (results[0] is PSObject pwshObj)
13851386
{
1386-
case ErrorRecord err:
1387-
//_cmdletPassedIn.WriteError(error);
1388-
break;
1387+
switch (pwshObj.BaseObject)
1388+
{
1389+
case ErrorRecord err:
1390+
//_cmdletPassedIn.WriteError(error);
1391+
break;
13891392

1390-
case WarningRecord warning:
1391-
//cmdlet.WriteWarning(warning.Message);
1392-
break;
1393+
case WarningRecord warning:
1394+
//cmdlet.WriteWarning(warning.Message);
1395+
break;
13931396

1394-
case VerboseRecord verbose:
1395-
//cmdlet.WriteVerbose(verbose.Message);
1396-
break;
1397+
case VerboseRecord verbose:
1398+
//cmdlet.WriteVerbose(verbose.Message);
1399+
break;
13971400

1398-
case DebugRecord debug:
1399-
//cmdlet.WriteDebug(debug.Message);
1400-
break;
1401+
case DebugRecord debug:
1402+
//cmdlet.WriteDebug(debug.Message);
1403+
break;
14011404

1402-
case InformationRecord info:
1403-
//cmdlet.WriteInformation(info);
1404-
break;
1405+
case InformationRecord info:
1406+
//cmdlet.WriteInformation(info);
1407+
break;
14051408

1406-
case Hashtable result:
1407-
dataFileInfo = result;
1408-
return true;
1409+
case Hashtable result:
1410+
dataFileInfo = result;
1411+
return true;
1412+
}
14091413
}
14101414
}
1415+
catch (Exception ex)
1416+
{
1417+
error = ex;
1418+
}
14111419
}
1412-
catch (Exception ex)
1413-
{
1414-
error = ex;
1415-
}
1420+
// Return false to indicate "we couldn't parse a valid Hashtable from this .psd1 file."
1421+
// The only success path is the 'case Hashtable result' branch above which does return true.
1422+
return false;
1423+
}
1424+
finally
1425+
{
1426+
// Always restore the previous default runspace and close/dispose
1427+
// the per-thread runspace, even on success (return true) or exception paths.
1428+
Runspace.DefaultRunspace = previousDefaultRunspace;
1429+
runspace.Close();
1430+
runspace.Dispose();
14161431
}
1417-
runspace.Close();
1418-
1419-
return false;
14201432
}
14211433
catch (Exception ex)
14221434
{

0 commit comments

Comments
 (0)