Use null-coalescing assignment in /remoting and /hostifaces (#17728)

This commit is contained in:
Dmitry Volodin
2022-07-25 11:26:01 -07:00
committed by GitHub
parent f1595c4b31
commit e8c66a3282
21 changed files with 44 additions and 126 deletions
@@ -85,10 +85,7 @@ namespace System.Management.Automation.Runspaces
{
lock (SyncObject)
{
if (_completedWaitHandle == null)
{
_completedWaitHandle = new ManualResetEvent(IsCompleted);
}
_completedWaitHandle ??= new ManualResetEvent(IsCompleted);
}
}
@@ -84,10 +84,7 @@ namespace System.Management.Automation.Runspaces
{
lock (this.SyncRoot)
{
if (_applicationPrivateData == null)
{
_applicationPrivateData = new PSPrimitiveDictionary();
}
_applicationPrivateData ??= new PSPrimitiveDictionary();
}
}
@@ -768,10 +765,7 @@ namespace System.Management.Automation.Runspaces
/// </remarks>
internal PipelineThread GetPipelineThread()
{
if (_pipelineThread == null)
{
_pipelineThread = new PipelineThread(this.ApartmentState);
}
_pipelineThread ??= new PipelineThread(this.ApartmentState);
return _pipelineThread;
}
@@ -1090,10 +1090,7 @@ namespace System.Management.Automation.Host
// This way, multiple runspaces opened by the same process will share the same transcript.
lock (s_systemTranscriptLock)
{
if (systemTranscript == null)
{
systemTranscript = PSHostUserInterface.GetTranscriptOptionFromSettings(transcription, currentTranscript);
}
systemTranscript ??= PSHostUserInterface.GetTranscriptOptionFromSettings(transcription, currentTranscript);
}
}
@@ -1322,12 +1322,9 @@ namespace System.Management.Automation
{
lock (SyncObject)
{
if (_readWaitHandle == null)
{
// Create the handle signaled if there are objects in the buffer
// or the buffer has been closed.
_readWaitHandle = new ManualResetEvent(_data.Count > 0 || !_isOpen);
}
// Create the handle signaled if there are objects in the buffer
// or the buffer has been closed.
_readWaitHandle ??= new ManualResetEvent(_data.Count > 0 || !_isOpen);
}
}
@@ -1536,12 +1536,9 @@ namespace System.Management.Automation.PSTasks
{
get
{
if (_jobDebuggerWrapper == null)
{
_jobDebuggerWrapper = new PSTaskChildDebugger(
_task.Debugger,
this.Name);
}
_jobDebuggerWrapper ??= new PSTaskChildDebugger(
_task.Debugger,
this.Name);
return _jobDebuggerWrapper;
}
@@ -762,10 +762,7 @@ namespace System.Management.Automation
// create the client remote powershell for remoting
// communications
if (RemotePowerShell == null)
{
RemotePowerShell = new ClientRemotePowerShell(this, ((RunspacePool)_rsConnection).RemoteRunspacePoolInternal);
}
RemotePowerShell ??= new ClientRemotePowerShell(this, ((RunspacePool)_rsConnection).RemoteRunspacePoolInternal);
// If we get here, we don't call 'Invoke' or any of it's friends on 'this', instead we serialize 'this' in PowerShell.ToPSObjectForRemoting.
// Without the following two steps, we'll be missing the 'ExtraCommands' on the serialized instance of 'this'.
@@ -804,10 +801,7 @@ namespace System.Management.Automation
RedirectShellErrorOutputPipe = redirectShellErrorOutputPipe;
if (RemotePowerShell == null)
{
RemotePowerShell = new ClientRemotePowerShell(this, ((RunspacePool)_rsConnection).RemoteRunspacePoolInternal);
}
RemotePowerShell ??= new ClientRemotePowerShell(this, ((RunspacePool)_rsConnection).RemoteRunspacePoolInternal);
if (!RemotePowerShell.Initialized)
{
@@ -2236,10 +2230,7 @@ namespace System.Management.Automation
{
if (addToHistory)
{
if (settings == null)
{
settings = new PSInvocationSettings();
}
settings ??= new PSInvocationSettings();
settings.AddToHistory = true;
}
@@ -3544,10 +3535,7 @@ namespace System.Management.Automation
break;
}
if (objs == null)
{
objs = _batchAsyncResult.Output;
}
objs ??= _batchAsyncResult.Output;
DoRemainingBatchCommands(objs);
}
@@ -231,10 +231,7 @@ namespace System.Management.Automation.Runspaces.Internal
{
lock (this.syncObject)
{
if (_applicationPrivateData == null)
{
_applicationPrivateData = new PSPrimitiveDictionary();
}
_applicationPrivateData ??= new PSPrimitiveDictionary();
}
}
@@ -633,10 +633,7 @@ namespace System.Management.Automation
{
lock (syncObject)
{
if (_childJobs == null)
{
_childJobs = new List<Job>();
}
_childJobs ??= new List<Job>();
}
}
@@ -3371,13 +3368,10 @@ namespace System.Management.Automation
}
}
if (failureException == null)
{
failureException = new RuntimeException(
PSRemotingErrorInvariants.FormatResourceString(
RemotingErrorIdStrings.RemoteRunspaceOpenUnknownState,
runspace.RunspaceStateInfo.State));
}
failureException ??= new RuntimeException(
PSRemotingErrorInvariants.FormatResourceString(
RemotingErrorIdStrings.RemoteRunspaceOpenUnknownState,
runspace.RunspaceStateInfo.State));
failureErrorRecord = new ErrorRecord(failureException, targetObject,
fullyQualifiedErrorId, ErrorCategory.OpenError,
@@ -93,8 +93,7 @@ namespace System.Management.Automation
{
lock (_syncobject)
{
if (_parameters == null)
_parameters = new List<CommandParameterCollection>();
_parameters ??= new List<CommandParameterCollection>();
}
}
@@ -208,12 +208,9 @@ namespace System.Management.Automation.Remoting
}
// If that didn't work out fall-back to the traditional approach.
if (pipeline == null)
{
pipeline = useNestedPipelines ?
_runspaceRef.Value.CreateNestedPipeline(line, addToHistory) :
_runspaceRef.Value.CreatePipeline(line, addToHistory);
}
pipeline ??= useNestedPipelines ?
_runspaceRef.Value.CreateNestedPipeline(line, addToHistory) :
_runspaceRef.Value.CreatePipeline(line, addToHistory);
// Add robust connection callback if this is a pushed runspace.
RemotePipeline remotePipeline = pipeline as RemotePipeline;
@@ -2911,10 +2911,7 @@ namespace System.Management.Automation
private void CheckRemoteBreakpointManagementSupport(string breakpointCommandNameToCheck)
{
if (_remoteBreakpointManagementIsSupported == null)
{
_remoteBreakpointManagementIsSupported = _remoteDebuggingCapability.IsCommandSupported(breakpointCommandNameToCheck);
}
_remoteBreakpointManagementIsSupported ??= _remoteDebuggingCapability.IsCommandSupported(breakpointCommandNameToCheck);
if (!_remoteBreakpointManagementIsSupported.Value)
{
@@ -4233,10 +4233,7 @@ namespace Microsoft.PowerShell.Commands
{
lock (s_SyncObject)
{
if (s_TypeTable == null)
{
s_TypeTable = TypeTable.LoadDefaultTypeFiles();
}
s_TypeTable ??= TypeTable.LoadDefaultTypeFiles();
}
}
@@ -1065,10 +1065,7 @@ namespace Microsoft.PowerShell.Commands
{
lock (_syncObject)
{
if (_outputProcessingNotification == null)
{
_outputProcessingNotification = new OutputProcessingState();
}
_outputProcessingNotification ??= new OutputProcessingState();
}
}
@@ -343,10 +343,7 @@ namespace Microsoft.PowerShell.Commands
{
base.BeginProcessing();
if (ConfigurationName == null)
{
ConfigurationName = string.Empty;
}
ConfigurationName ??= string.Empty;
}
/// <summary>
@@ -524,10 +524,7 @@ namespace Microsoft.PowerShell.Commands
}
}
if (reason == null)
{
reason = new RuntimeException(this.GetMessage(RemotingErrorIdStrings.RemoteRunspaceOpenUnknownState, state));
}
reason ??= new RuntimeException(this.GetMessage(RemotingErrorIdStrings.RemoteRunspaceOpenUnknownState, state));
string fullyQualifiedErrorId = WSManTransportManagerUtils.GetFQEIDFromTransportError(
transErrorCode,
@@ -499,7 +499,7 @@ namespace System.Management.Automation.Remoting.Internal
internal static ErrorRecord AddSourceTagToError(ErrorRecord errorRecord, Guid sourceId)
{
if (errorRecord == null) return null;
if (errorRecord.ErrorDetails == null) errorRecord.ErrorDetails = new ErrorDetails(string.Empty);
errorRecord.ErrorDetails ??= new ErrorDetails(string.Empty);
errorRecord.ErrorDetails.RecommendedAction = CreateInformationalMessage(sourceId, errorRecord.ErrorDetails.RecommendedAction);
return errorRecord;
}
@@ -1359,10 +1359,7 @@ namespace System.Management.Automation.Remoting.Server
data.Data);
if (_isSerializing)
{
if (_dataToBeSentQueue == null)
{
_dataToBeSentQueue = new Queue<Tuple<RemoteDataObject, bool, bool>>();
}
_dataToBeSentQueue ??= new Queue<Tuple<RemoteDataObject, bool, bool>>();
_dataToBeSentQueue.Enqueue(new Tuple<RemoteDataObject, bool, bool>(dataToBeSent, flush, reportPending));
return;
@@ -286,15 +286,9 @@ namespace System.Management.Automation.Remoting
}
// assign defaults after parsing the xml content.
if (result.MaxReceivedObjectSizeMB == null)
{
result.MaxReceivedObjectSizeMB = BaseTransportManager.MaximumReceivedObjectSize;
}
result.MaxReceivedObjectSizeMB ??= BaseTransportManager.MaximumReceivedObjectSize;
if (result.MaxReceivedCommandSizeMB == null)
{
result.MaxReceivedCommandSizeMB = BaseTransportManager.MaximumReceivedDataSize;
}
result.MaxReceivedCommandSizeMB ??= BaseTransportManager.MaximumReceivedDataSize;
return result;
}
@@ -1740,10 +1734,7 @@ namespace System.Management.Automation.Remoting
bool validateFile = false)
{
_configFile = configFile;
if (roleVerifier == null)
{
roleVerifier = static (role) => false;
}
roleVerifier ??= static (role) => false;
Runspace backupRunspace = Runspace.DefaultRunspace;
@@ -224,8 +224,8 @@ namespace System.Management.Automation.Remoting
private void CreateCollectionIfNecessary()
{
if (_modulesToImport == null) _modulesToImport = new List<string>();
if (_modulesToImportInternal == null) _modulesToImportInternal = new List<object>();
_modulesToImport ??= new List<string>();
_modulesToImportInternal ??= new List<object>();
}
private const string SessionConfigToken = "SessionConfigurationData";
@@ -358,14 +358,11 @@ namespace System.Management.Automation.Remoting.Server
string data = originalStdIn.ReadLine();
lock (_syncObject)
{
if (sessionTM == null)
{
sessionTM = CreateSessionTransportManager(
configurationName: configurationName,
configurationFile: configurationFile,
cryptoHelper: cryptoHelper,
workingDirectory: workingDirectory);
}
sessionTM ??= CreateSessionTransportManager(
configurationName: configurationName,
configurationFile: configurationFile,
cryptoHelper: cryptoHelper,
workingDirectory: workingDirectory);
}
if (string.IsNullOrEmpty(data))
@@ -271,10 +271,7 @@ namespace System.Management.Automation
internal void SendApplicationPrivateDataToClient()
{
// Include Debug mode information.
if (_applicationPrivateData == null)
{
_applicationPrivateData = new PSPrimitiveDictionary();
}
_applicationPrivateData ??= new PSPrimitiveDictionary();
if (_serverRemoteDebugger != null)
{
@@ -2004,10 +2001,7 @@ namespace System.Management.Automation
StringUtil.Format(DebuggerStrings.CannotProcessDebuggerCommandNotStopped));
}
if (_processCommandCompleteEvent == null)
{
_processCommandCompleteEvent = new ManualResetEventSlim(false);
}
_processCommandCompleteEvent ??= new ManualResetEventSlim(false);
_threadCommandProcessing = new ThreadCommandProcessing(command, output, _wrappedDebugger.Value, _processCommandCompleteEvent);
try
@@ -2527,10 +2521,7 @@ namespace System.Management.Automation
{
// Blocking call for nested debugger execution (Debug-Runspace) stop events.
// The root debugger never makes two EnterDebugMode calls without an ExitDebugMode.
if (_nestedDebugStopCompleteEvent == null)
{
_nestedDebugStopCompleteEvent = new ManualResetEventSlim(false);
}
_nestedDebugStopCompleteEvent ??= new ManualResetEventSlim(false);
_nestedDebugging = true;
OnEnterDebugMode(_nestedDebugStopCompleteEvent);