@@ -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