Use null propagation operator in remoting code (#17790)

This commit is contained in:
Frode Flaten
2022-08-01 09:21:16 -07:00
committed by GitHub
parent 2d4a6bf475
commit 7c624902e1
33 changed files with 106 additions and 383 deletions
@@ -159,10 +159,7 @@ namespace System.Management.Automation.Remoting
{
try
{
if (_clientHost.UI != null)
{
_clientHost.UI.WriteErrorLine(errorRecord.ToString());
}
_clientHost.UI?.WriteErrorLine(errorRecord.ToString());
}
catch (Exception)
{
@@ -166,10 +166,7 @@ namespace System.Management.Automation.Runspaces.Internal
outputstream.Close();
errorstream.Close();
if (inputstream != null)
{
inputstream.Close();
}
inputstream?.Close();
}
/// <summary>
@@ -1448,10 +1448,7 @@ namespace System.Management.Automation
{
lock (syncObject)
{
if (_finished != null)
{
_finished.Set();
}
_finished?.Set();
}
}
#pragma warning restore 56500
@@ -4074,10 +4071,7 @@ namespace System.Management.Automation
internal void CheckStateAndRaiseStopEvent()
{
RemoteDebugger remoteDebugger = _wrappedDebugger as RemoteDebugger;
if (remoteDebugger != null)
{
remoteDebugger.CheckStateAndRaiseStopEvent();
}
remoteDebugger?.CheckStateAndRaiseStopEvent();
}
/// <summary>
@@ -4125,13 +4119,7 @@ namespace System.Management.Automation
return null;
}
private static void RestoreRemoteOutput(Pipeline runningCmd)
{
if (runningCmd != null)
{
runningCmd.ResumeIncomingData();
}
}
private static void RestoreRemoteOutput(Pipeline runningCmd) => runningCmd?.ResumeIncomingData();
private void HandleBreakpointUpdated(object sender, BreakpointUpdatedEventArgs e)
{
@@ -2021,11 +2021,8 @@ namespace System.Management.Automation
job.Dispose();
}
if (_jobRunning != null)
_jobRunning.Dispose();
if (_jobSuspendedOrAborted != null)
_jobSuspendedOrAborted.Dispose();
_jobRunning?.Dispose();
_jobSuspendedOrAborted?.Dispose();
}
finally
{
@@ -1826,20 +1826,14 @@ namespace System.Management.Automation.Runspaces.Internal
{
// Reset DisconnectedOn/ExpiresOn
WSManConnectionInfo wsManConnectionInfo = _connectionInfo as WSManConnectionInfo;
if (wsManConnectionInfo != null)
{
wsManConnectionInfo.NullDisconnectedExpiresOn();
}
wsManConnectionInfo?.NullDisconnectedExpiresOn();
}
private void UpdateDisconnectedExpiresOn()
{
// Set DisconnectedOn/ExpiresOn for disconnected session.
WSManConnectionInfo wsManConnectionInfo = _connectionInfo as WSManConnectionInfo;
if (wsManConnectionInfo != null)
{
wsManConnectionInfo.SetDisconnectedExpiresOnToNow();
}
wsManConnectionInfo?.SetDisconnectedExpiresOnToNow();
}
/// <summary>
@@ -275,10 +275,7 @@ namespace System.Management.Automation.Internal
// if a data structure handler does not exist it means
// the association has been removed -
// discard messages
if (dsHandler != null)
{
dsHandler.ProcessReceivedData(rcvdData);
}
dsHandler?.ProcessReceivedData(rcvdData);
}
/// <summary>
@@ -53,11 +53,7 @@ namespace System.Management.Automation
childJob.Dispose();
}
if (_jobResultsThrottlingSemaphore != null)
{
_jobResultsThrottlingSemaphore.Dispose();
}
_jobResultsThrottlingSemaphore?.Dispose();
_cancellationTokenSource.Dispose();
}
}
@@ -541,10 +537,7 @@ namespace System.Management.Automation
} while (false);
}
if (readyToRunChildJob != null)
{
readyToRunChildJob.StartJob();
}
readyToRunChildJob?.StartJob();
}
private void EnqueueReadyToRunChildJob(StartableJob childJob)
@@ -1227,10 +1220,7 @@ namespace System.Management.Automation
}
finally
{
if (cancellationTokenRegistration != null)
{
cancellationTokenRegistration.Dispose();
}
cancellationTokenRegistration?.Dispose();
}
}
finally
@@ -254,10 +254,7 @@ namespace System.Management.Automation.Remoting
if (_state == RemoteSessionState.EstablishedAndKeySent)
{
Timer tmp = Interlocked.Exchange(ref _keyExchangeTimer, null);
if (tmp != null)
{
tmp.Dispose();
}
tmp?.Dispose();
_keyExchanged = true;
SetState(RemoteSessionState.Established, eventArgs.Reason);
@@ -339,10 +336,7 @@ namespace System.Management.Automation.Remoting
Dbg.Assert(_state == RemoteSessionState.EstablishedAndKeySent, "timeout should only happen when waiting for a key");
Timer tmp = Interlocked.Exchange(ref _keyExchangeTimer, null);
if (tmp != null)
{
tmp.Dispose();
}
tmp?.Dispose();
PSRemotingDataStructureException exception =
new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientKeyExchangeFailed);
@@ -634,11 +634,8 @@ namespace System.Management.Automation
//
}
if (_remoteDebugger != null)
{
// Release RunspacePool event forwarding handlers.
_remoteDebugger.Dispose();
}
// Release RunspacePool event forwarding handlers.
_remoteDebugger?.Dispose();
try
{
@@ -1732,10 +1729,7 @@ namespace System.Management.Automation
System.Management.Automation.Remoting.Client.NamedPipeClientSessionTransportManager transportManager =
RunspacePool.RemoteRunspacePoolInternal.DataStructureHandler.TransportManager as System.Management.Automation.Remoting.Client.NamedPipeClientSessionTransportManager;
if (transportManager != null)
{
transportManager.AbortConnect();
}
transportManager?.AbortConnect();
}
#endregion Internal Methods
@@ -384,10 +384,7 @@ namespace System.Management.Automation.Runspaces
_transportName = transportName
};
if (psCmdlet != null)
{
psCmdlet.RunspaceRepository.Add(psSession);
}
psCmdlet?.RunspaceRepository.Add(psSession);
return psSession;
}
@@ -203,10 +203,7 @@ namespace Microsoft.PowerShell.Commands
// Unblock the data collection.
PSDataCollection<PSStreamObject> debugCollection = _debugCollection;
if (debugCollection != null)
{
debugCollection.Complete();
}
debugCollection?.Complete();
}
#endregion
@@ -260,10 +257,7 @@ namespace Microsoft.PowerShell.Commands
// or this command is cancelled.
foreach (var streamItem in _debugCollection)
{
if (streamItem != null)
{
streamItem.WriteStreamObject(this);
}
streamItem?.WriteStreamObject(this);
}
}
catch (Exception)
@@ -212,10 +212,7 @@ namespace Microsoft.PowerShell.Commands
protected override void StopProcessing()
{
RemoteRunspace connectingRunspace = _connectingRemoteRunspace;
if (connectingRunspace != null)
{
connectingRunspace.AbortOpen();
}
connectingRunspace?.AbortOpen();
}
#endregion
@@ -1559,10 +1559,7 @@ namespace Microsoft.PowerShell.Commands
operation.RunspaceDebugStop -= HandleRunspaceDebugStop;
var hostDebugger = GetHostDebugger();
if (hostDebugger != null)
{
hostDebugger.QueueRunspaceForDebug(args.Runspace);
}
hostDebugger?.QueueRunspaceForDebug(args.Runspace);
}
private void HandleJobStateChanged(object sender, JobStateEventArgs e)
@@ -1579,10 +1576,7 @@ namespace Microsoft.PowerShell.Commands
// Signal that this job has been disconnected, or has ended.
lock (_jobSyncObject)
{
if (_disconnectComplete != null)
{
_disconnectComplete.Set();
}
_disconnectComplete?.Set();
}
}
}
@@ -2186,11 +2180,8 @@ namespace Microsoft.PowerShell.Commands
if (!_asjob)
{
if (_job != null)
{
// job will be null in the "InProcess" case
_job.Dispose();
}
// job will be null in the "InProcess" case
_job?.Dispose();
_throttleManager.ThrottleComplete -= HandleThrottleComplete;
_throttleManager.Dispose();
@@ -1960,11 +1960,7 @@ namespace Microsoft.PowerShell.Commands
/// <summary>
/// Adds forwarded events to the local queue.
/// </summary>
internal void OnRunspacePSEventReceived(object sender, PSEventArgs e)
{
if (this.Events != null)
this.Events.AddForwardedEvent(e);
}
internal void OnRunspacePSEventReceived(object sender, PSEventArgs e) => this.Events?.AddForwardedEvent(e);
#endregion Private Methods
@@ -3481,10 +3477,7 @@ namespace Microsoft.PowerShell.Commands
OperationState.StopComplete;
operationStateEventArgs.BaseEvent = baseEventArgs;
if (OperationComplete != null)
{
OperationComplete.SafeInvoke(this, operationStateEventArgs);
}
OperationComplete?.SafeInvoke(this, operationStateEventArgs);
}
}
@@ -3679,11 +3672,7 @@ namespace Microsoft.PowerShell.Commands
case PipelineState.Completed:
case PipelineState.Stopped:
case PipelineState.Failed:
if (RemoteRunspace != null)
{
RemoteRunspace.CloseAsync();
}
RemoteRunspace?.CloseAsync();
break;
}
}
@@ -823,10 +823,7 @@ namespace Microsoft.PowerShell.Commands
{
if (v == null) continue;
MshCommandRuntime mshCommandRuntime = CommandRuntime as MshCommandRuntime;
if (mshCommandRuntime != null)
{
mshCommandRuntime.WriteVerbose(v, true);
}
mshCommandRuntime?.WriteVerbose(v, true);
}
Collection<DebugRecord> debugRecords = ReadAll(job.Debug);
@@ -835,10 +832,7 @@ namespace Microsoft.PowerShell.Commands
{
if (d == null) continue;
MshCommandRuntime mshCommandRuntime = CommandRuntime as MshCommandRuntime;
if (mshCommandRuntime != null)
{
mshCommandRuntime.WriteDebug(d, true);
}
mshCommandRuntime?.WriteDebug(d, true);
}
Collection<WarningRecord> warningRecords = ReadAll(job.Warning);
@@ -847,10 +841,7 @@ namespace Microsoft.PowerShell.Commands
{
if (w == null) continue;
MshCommandRuntime mshCommandRuntime = CommandRuntime as MshCommandRuntime;
if (mshCommandRuntime != null)
{
mshCommandRuntime.WriteWarning(w, true);
}
mshCommandRuntime?.WriteWarning(w, true);
}
Collection<ProgressRecord> progressRecords = ReadAll(job.Progress);
@@ -859,10 +850,7 @@ namespace Microsoft.PowerShell.Commands
{
if (p == null) continue;
MshCommandRuntime mshCommandRuntime = CommandRuntime as MshCommandRuntime;
if (mshCommandRuntime != null)
{
mshCommandRuntime.WriteProgress(p, true);
}
mshCommandRuntime?.WriteProgress(p, true);
}
Collection<InformationRecord> informationRecords = ReadAll(job.Information);
@@ -871,10 +859,7 @@ namespace Microsoft.PowerShell.Commands
{
if (p == null) continue;
MshCommandRuntime mshCommandRuntime = CommandRuntime as MshCommandRuntime;
if (mshCommandRuntime != null)
{
mshCommandRuntime.WriteInformation(p, true);
}
mshCommandRuntime?.WriteInformation(p, true);
}
}
@@ -385,11 +385,7 @@ namespace Microsoft.PowerShell.Commands
/// <summary>
/// Adds forwarded events to the local queue.
/// </summary>
private void OnRunspacePSEventReceived(object sender, PSEventArgs e)
{
if (this.Events != null)
this.Events.AddForwardedEvent(e);
}
private void OnRunspacePSEventReceived(object sender, PSEventArgs e) => this.Events?.AddForwardedEvent(e);
/// <summary>
/// When the client remote session reports a URI redirection, this method will report the
@@ -1099,13 +1099,7 @@ namespace System.Management.Automation.Remoting
/// <summary>
/// Closes the named pipe.
/// </summary>
public void Close()
{
if (_clientPipeStream != null)
{
_clientPipeStream.Dispose();
}
}
public void Close() => _clientPipeStream?.Dispose();
/// <summary>
/// Abort connection attempt.
@@ -188,10 +188,7 @@ namespace System.Management.Automation.Remoting
}
finally
{
if (remoteRunspaceToClose != null)
{
remoteRunspaceToClose.Close();
}
remoteRunspaceToClose?.Close();
}
}
@@ -120,10 +120,7 @@ namespace System.Management.Automation.Remoting.Internal
ErrorRecord errorRecord = (ErrorRecord)this.Value;
errorRecord.PreserveInvocationInfoOnce = true;
MshCommandRuntime mshCommandRuntime = cmdlet.CommandRuntime as MshCommandRuntime;
if (mshCommandRuntime != null)
{
mshCommandRuntime.WriteError(errorRecord, overrideInquire);
}
mshCommandRuntime?.WriteError(errorRecord, overrideInquire);
}
break;
@@ -133,10 +130,7 @@ namespace System.Management.Automation.Remoting.Internal
string debug = (string)Value;
DebugRecord debugRecord = new DebugRecord(debug);
MshCommandRuntime mshCommandRuntime = cmdlet.CommandRuntime as MshCommandRuntime;
if (mshCommandRuntime != null)
{
mshCommandRuntime.WriteDebug(debugRecord, overrideInquire);
}
mshCommandRuntime?.WriteDebug(debugRecord, overrideInquire);
}
break;
@@ -146,10 +140,7 @@ namespace System.Management.Automation.Remoting.Internal
string warning = (string)Value;
WarningRecord warningRecord = new WarningRecord(warning);
MshCommandRuntime mshCommandRuntime = cmdlet.CommandRuntime as MshCommandRuntime;
if (mshCommandRuntime != null)
{
mshCommandRuntime.WriteWarning(warningRecord, overrideInquire);
}
mshCommandRuntime?.WriteWarning(warningRecord, overrideInquire);
}
break;
@@ -159,10 +150,7 @@ namespace System.Management.Automation.Remoting.Internal
string verbose = (string)Value;
VerboseRecord verboseRecord = new VerboseRecord(verbose);
MshCommandRuntime mshCommandRuntime = cmdlet.CommandRuntime as MshCommandRuntime;
if (mshCommandRuntime != null)
{
mshCommandRuntime.WriteVerbose(verboseRecord, overrideInquire);
}
mshCommandRuntime?.WriteVerbose(verboseRecord, overrideInquire);
}
break;
@@ -170,10 +158,7 @@ namespace System.Management.Automation.Remoting.Internal
case PSStreamObjectType.Progress:
{
MshCommandRuntime mshCommandRuntime = cmdlet.CommandRuntime as MshCommandRuntime;
if (mshCommandRuntime != null)
{
mshCommandRuntime.WriteProgress((ProgressRecord)Value, overrideInquire);
}
mshCommandRuntime?.WriteProgress((ProgressRecord)Value, overrideInquire);
}
break;
@@ -181,10 +166,7 @@ namespace System.Management.Automation.Remoting.Internal
case PSStreamObjectType.Information:
{
MshCommandRuntime mshCommandRuntime = cmdlet.CommandRuntime as MshCommandRuntime;
if (mshCommandRuntime != null)
{
mshCommandRuntime.WriteInformation((InformationRecord)Value, overrideInquire);
}
mshCommandRuntime?.WriteInformation((InformationRecord)Value, overrideInquire);
}
break;
@@ -193,10 +175,7 @@ namespace System.Management.Automation.Remoting.Internal
{
WarningRecord warningRecord = (WarningRecord)Value;
MshCommandRuntime mshCommandRuntime = cmdlet.CommandRuntime as MshCommandRuntime;
if (mshCommandRuntime != null)
{
mshCommandRuntime.AppendWarningVarList(warningRecord);
}
mshCommandRuntime?.AppendWarningVarList(warningRecord);
}
break;
@@ -311,10 +290,7 @@ namespace System.Management.Automation.Remoting.Internal
errorRecord.PreserveInvocationInfoOnce = true;
MshCommandRuntime mshCommandRuntime = cmdlet.CommandRuntime as MshCommandRuntime;
if (mshCommandRuntime != null)
{
mshCommandRuntime.WriteError(errorRecord, overrideInquire);
}
mshCommandRuntime?.WriteError(errorRecord, overrideInquire);
}
break;
@@ -324,10 +300,7 @@ namespace System.Management.Automation.Remoting.Internal
string warning = (string)Value;
WarningRecord warningRecord = new WarningRecord(warning);
MshCommandRuntime mshCommandRuntime = cmdlet.CommandRuntime as MshCommandRuntime;
if (mshCommandRuntime != null)
{
mshCommandRuntime.WriteWarning(warningRecord, overrideInquire);
}
mshCommandRuntime?.WriteWarning(warningRecord, overrideInquire);
}
break;
@@ -337,10 +310,7 @@ namespace System.Management.Automation.Remoting.Internal
string verbose = (string)Value;
VerboseRecord verboseRecord = new VerboseRecord(verbose);
MshCommandRuntime mshCommandRuntime = cmdlet.CommandRuntime as MshCommandRuntime;
if (mshCommandRuntime != null)
{
mshCommandRuntime.WriteVerbose(verboseRecord, overrideInquire);
}
mshCommandRuntime?.WriteVerbose(verboseRecord, overrideInquire);
}
break;
@@ -365,10 +335,7 @@ namespace System.Management.Automation.Remoting.Internal
}
MshCommandRuntime mshCommandRuntime = cmdlet.CommandRuntime as MshCommandRuntime;
if (mshCommandRuntime != null)
{
mshCommandRuntime.WriteProgress(progressRecord, overrideInquire);
}
mshCommandRuntime?.WriteProgress(progressRecord, overrideInquire);
}
break;
@@ -378,10 +345,7 @@ namespace System.Management.Automation.Remoting.Internal
string debug = (string)Value;
DebugRecord debugRecord = new DebugRecord(debug);
MshCommandRuntime mshCommandRuntime = cmdlet.CommandRuntime as MshCommandRuntime;
if (mshCommandRuntime != null)
{
mshCommandRuntime.WriteDebug(debugRecord, overrideInquire);
}
mshCommandRuntime?.WriteDebug(debugRecord, overrideInquire);
}
break;
@@ -411,10 +375,7 @@ namespace System.Management.Automation.Remoting.Internal
}
MshCommandRuntime mshCommandRuntime = cmdlet.CommandRuntime as MshCommandRuntime;
if (mshCommandRuntime != null)
{
mshCommandRuntime.WriteInformation(informationRecord, overrideInquire);
}
mshCommandRuntime?.WriteInformation(informationRecord, overrideInquire);
}
break;
@@ -470,10 +431,7 @@ namespace System.Management.Automation.Remoting.Internal
}
finally
{
if (cmdletMethodInvoker.Finished != null)
{
cmdletMethodInvoker.Finished.Set();
}
cmdletMethodInvoker.Finished?.Set();
}
}
@@ -532,10 +532,7 @@ namespace System.Management.Automation.Remoting
}
}
if (operation != null)
{
operation.StartOperation();
}
operation?.StartOperation();
}
/// <summary>
@@ -895,13 +895,11 @@ namespace System.Management.Automation.Remoting.Client
{
// this is for a command
OutOfProcessClientCommandTransportManager cmdTM = GetCommandTransportManager(psGuid);
if (cmdTM != null)
{
// not throwing the exception in null case as the command might have already
// closed. The RS data structure handler does not wait for the close ack before
// it clears the command transport manager..so this might happen.
cmdTM.OnRemoteCmdDataReceived(rawData, streamTemp);
}
// not throwing the exception in null case as the command might have already
// closed. The RS data structure handler does not wait for the close ack before
// it clears the command transport manager..so this might happen.
cmdTM?.OnRemoteCmdDataReceived(rawData, streamTemp);
}
}
@@ -916,13 +914,11 @@ namespace System.Management.Automation.Remoting.Client
{
// this is for a command
OutOfProcessClientCommandTransportManager cmdTM = GetCommandTransportManager(psGuid);
if (cmdTM != null)
{
// not throwing the exception in null case as the command might have already
// closed. The RS data structure handler does not wait for the close ack before
// it clears the command transport manager..so this might happen.
cmdTM.OnRemoteCmdSendCompleted();
}
// not throwing the exception in null case as the command might have already
// closed. The RS data structure handler does not wait for the close ack before
// it clears the command transport manager..so this might happen.
cmdTM?.OnRemoteCmdSendCompleted();
}
}
@@ -968,10 +964,7 @@ namespace System.Management.Automation.Remoting.Client
else
{
OutOfProcessClientCommandTransportManager cmdTM = GetCommandTransportManager(psGuid);
if (cmdTM != null)
{
cmdTM.OnRemoteCmdSignalCompleted();
}
cmdTM?.OnRemoteCmdSignalCompleted();
}
}
@@ -1001,12 +994,10 @@ namespace System.Management.Automation.Remoting.Client
_tracer.WriteMessage("OutOfProcessClientSessionTransportManager.OnCloseAckReceived, in progress command count should be greater than zero: " + commandCount + ", RunSpacePool Id : " + this.RunspacePoolInstanceId + ", psGuid : " + psGuid.ToString());
OutOfProcessClientCommandTransportManager cmdTM = GetCommandTransportManager(psGuid);
if (cmdTM != null)
{
// this might legitimately happen if cmd is already closed before we get an
// ACK back from server.
cmdTM.OnCloseCmdCompleted();
}
// this might legitimately happen if cmd is already closed before we get an
// ACK back from server.
cmdTM?.OnCloseCmdCompleted();
}
}
@@ -1414,10 +1405,7 @@ namespace System.Management.Automation.Remoting.Client
if (isDisposing)
{
if (_client != null)
{
_client.Dispose();
}
_client?.Dispose();
}
}
@@ -2055,10 +2043,7 @@ namespace System.Management.Automation.Remoting.Client
if (isDisposing)
{
if (_clientPipe != null)
{
_clientPipe.Dispose();
}
_clientPipe?.Dispose();
}
}
@@ -2190,13 +2175,7 @@ namespace System.Management.Automation.Remoting.Client
/// <summary>
/// Aborts an existing connection attempt.
/// </summary>
public void AbortConnect()
{
if (_clientPipe != null)
{
_clientPipe.AbortConnect();
}
}
public void AbortConnect() => _clientPipe?.AbortConnect();
#endregion
}
@@ -684,10 +684,7 @@ namespace System.Management.Automation.Remoting
private void ResetReceiveData()
{
// reset resources used to store incoming data (for a single object)
if (_dataToProcessStream != null)
{
_dataToProcessStream.Dispose();
}
_dataToProcessStream?.Dispose();
_currentObjectId = 0;
_currentFrgId = 0;
@@ -446,11 +446,7 @@ namespace System.Management.Automation.Remoting
IntPtr pluginContext)
{
WSManPluginInstance.PerformShutdown(pluginContext);
if (workerPtrs != null)
{
workerPtrs.Dispose();
}
workerPtrs?.Dispose();
}
/// <summary>
@@ -1492,20 +1492,9 @@ namespace System.Management.Automation.Remoting.Client
finally
{
// release resources
if (proxyAuthCredentials != null)
{
proxyAuthCredentials.Dispose();
}
if (proxyInfo != null)
{
proxyInfo.Dispose();
}
if (authCredentials != null)
{
authCredentials.Dispose();
}
proxyAuthCredentials?.Dispose();
proxyInfo?.Dispose();
authCredentials?.Dispose();
}
if (result != 0)
@@ -200,10 +200,7 @@ namespace System.Management.Automation.Remoting.Server
}
// dont throw if there is no cmdTM as it might have legitimately closed
if (cmdTM != null)
{
cmdTM.Close(null);
}
cmdTM?.Close(null);
}
finally
{
@@ -244,12 +241,9 @@ namespace System.Management.Automation.Remoting.Server
{
tracer.WriteMessage("OnClosePacketReceived, in progress commands count should be zero : " + _inProgressCommandsCount + ", psGuid : " + psGuid.ToString());
if (sessionTM != null)
{
// it appears that when closing PowerShell ISE, therefore closing OutOfProcServerMediator, there are 2 Close command requests
// changing PSRP/IPC at this point is too risky, therefore protecting about this duplication
sessionTM.Close(null);
}
// it appears that when closing PowerShell ISE, therefore closing OutOfProcServerMediator, there are 2 Close command requests
// changing PSRP/IPC at this point is too risky, therefore protecting about this duplication
sessionTM?.Close(null);
tracer.WriteMessage("END calling close on session transport manager");
sessionTM = null;
@@ -268,10 +262,7 @@ namespace System.Management.Automation.Remoting.Server
}
// dont throw if there is no cmdTM as it might have legitimately closed
if (cmdTM != null)
{
cmdTM.Close(null);
}
cmdTM?.Close(null);
lock (_syncObject)
{
@@ -798,11 +798,9 @@ namespace System.Management.Automation
{
// Close input if its active. no need to synchronize as input stream would have already been processed
// when connect call came into PS plugin
if (InputCollection != null)
{
// TODO: Post an ETW event
InputCollection.Complete();
}
// TODO: Post an ETW event
InputCollection?.Complete();
}
/// <summary>
@@ -352,10 +352,7 @@ namespace System.Management.Automation.Remoting
{
if (_pushedRunspace != null)
{
if (_debugger != null)
{
_debugger.PopDebugger();
}
_debugger?.PopDebugger();
if (_hostSupportsPSEdit)
{
@@ -266,10 +266,7 @@ namespace System.Management.Automation
// if data structure handler is not found, then association has already been
// removed, discard message
if (dsHandler != null)
{
dsHandler.ProcessReceivedData(rcvdData);
}
dsHandler?.ProcessReceivedData(rcvdData);
}
/// <summary>
@@ -347,10 +347,7 @@ namespace System.Management.Automation
{
Runspace runspaceToDispose = _remoteHost.PushedRunspace;
_remoteHost.PopRunspace();
if (runspaceToDispose != null)
{
runspaceToDispose.Dispose();
}
runspaceToDispose?.Dispose();
}
DisposeRemoteDebugger();
@@ -483,13 +480,7 @@ namespace System.Management.Automation
}
}
private void DisposeRemoteDebugger()
{
if (_serverRemoteDebugger != null)
{
_serverRemoteDebugger.Dispose();
}
}
private void DisposeRemoteDebugger() => _serverRemoteDebugger?.Dispose();
/// <summary>
/// Invokes a script.
@@ -809,10 +800,7 @@ namespace System.Management.Automation
{
// If we have a pushed runspace then execute there.
// Ensure debugger is enabled to the original mode it was set to.
if (_serverRemoteDebugger != null)
{
_serverRemoteDebugger.CheckDebuggerState();
}
_serverRemoteDebugger?.CheckDebuggerState();
StartPowerShellCommandOnPushedRunspace(
powershell,
@@ -920,10 +908,7 @@ namespace System.Management.Automation
// Invoke command normally. Ensure debugger is enabled to the
// original mode it was set to.
if (_serverRemoteDebugger != null)
{
_serverRemoteDebugger.CheckDebuggerState();
}
_serverRemoteDebugger?.CheckDebuggerState();
// Invoke PowerShell on driver runspace pool.
ServerPowerShellDriver driver = new ServerPowerShellDriver(
@@ -2025,10 +2010,7 @@ namespace System.Management.Automation
}
ThreadCommandProcessing threadCommandProcessing = _threadCommandProcessing;
if (threadCommandProcessing != null)
{
threadCommandProcessing.Stop();
}
threadCommandProcessing?.Stop();
}
/// <summary>
@@ -2226,15 +2208,8 @@ namespace System.Management.Automation
ExitDebugMode(DebuggerResumeAction.Stop);
}
if (_nestedDebugStopCompleteEvent != null)
{
_nestedDebugStopCompleteEvent.Dispose();
}
if (_processCommandCompleteEvent != null)
{
_processCommandCompleteEvent.Dispose();
}
_nestedDebugStopCompleteEvent?.Dispose();
_processCommandCompleteEvent?.Dispose();
}
#endregion
@@ -2306,10 +2281,7 @@ namespace System.Management.Automation
public void Stop()
{
Debugger debugger = _wrappedDebugger;
if (debugger != null)
{
debugger.StopProcessCommand();
}
debugger?.StopProcessCommand();
}
internal void DoInvoke()
@@ -241,10 +241,7 @@ namespace System.Management.Automation
_eventSubscriber.FireStartSteppablePipeline(this);
if (_powershellInput != null)
{
_powershellInput.Pulse();
}
_powershellInput?.Pulse();
}
#endregion Internal Methods
@@ -262,20 +259,14 @@ namespace System.Management.Automation
CheckAndPulseForProcessing(true);
if (_powershellInput != null)
{
_powershellInput.Pulse();
}
_powershellInput?.Pulse();
}
private void HandleSessionConnected(object sender, EventArgs eventArgs)
{
// Close input if its active. no need to synchronize as input stream would have already been processed
// when connect call came into PS plugin
if (Input != null)
{
Input.Complete();
}
Input?.Complete();
}
/// <summary>
@@ -302,10 +293,7 @@ namespace System.Management.Automation
PerformStop();
if (_powershellInput != null)
{
_powershellInput.Pulse();
}
_powershellInput?.Pulse();
}
/// <summary>
@@ -326,10 +314,7 @@ namespace System.Management.Automation
CheckAndPulseForProcessing(false);
if (_powershellInput != null)
{
_powershellInput.Pulse();
}
_powershellInput?.Pulse();
}
}
@@ -274,11 +274,8 @@ namespace System.Management.Automation
{
lock (_syncObject)
{
if (_eventManager != null)
{
_eventManager.GenerateEvent(_startSubscriber.SourceIdentifier, this,
new object[1] { new ServerSteppablePipelineDriverEventArg(driver) }, null, true, false);
}
_eventManager?.GenerateEvent(_startSubscriber.SourceIdentifier, this,
new object[1] { new ServerSteppablePipelineDriverEventArg(driver) }, null, true, false);
}
}
@@ -290,11 +287,8 @@ namespace System.Management.Automation
{
lock (_syncObject)
{
if (_eventManager != null)
{
_eventManager.GenerateEvent(_processSubscriber.SourceIdentifier, this,
new object[1] { new ServerSteppablePipelineDriverEventArg(driver) }, null, true, false);
}
_eventManager?.GenerateEvent(_processSubscriber.SourceIdentifier, this,
new object[1] { new ServerSteppablePipelineDriverEventArg(driver) }, null, true, false);
}
}
@@ -706,13 +706,7 @@ namespace System.Management.Automation.Remoting
}
// pass on application private data when session is connected from new client
internal void HandlePostConnect()
{
if (_runspacePoolDriver != null)
{
_runspacePoolDriver.SendApplicationPrivateDataToClient();
}
}
internal void HandlePostConnect() => _runspacePoolDriver?.SendApplicationPrivateDataToClient();
/// <summary>
/// </summary>
@@ -945,10 +939,7 @@ namespace System.Management.Automation.Remoting
/// <param name="eventArgs"></param>
private void HandleSessionDSHandlerClosing(object sender, EventArgs eventArgs)
{
if (_runspacePoolDriver != null)
{
_runspacePoolDriver.Close();
}
_runspacePoolDriver?.Close();
// dispose the session configuration object..this will let them
// clean their resources.
@@ -913,10 +913,7 @@ namespace System.Management.Automation.Remoting
{
// reset the timer
Timer tmp = Interlocked.Exchange(ref _keyExchangeTimer, null);
if (tmp != null)
{
tmp.Dispose();
}
tmp?.Dispose();
}
// the key import would have been done
@@ -984,10 +981,7 @@ namespace System.Management.Automation.Remoting
Dbg.Assert(_state == RemoteSessionState.EstablishedAndKeyRequested, "timeout should only happen when waiting for a key");
Timer tmp = Interlocked.Exchange(ref _keyExchangeTimer, null);
if (tmp != null)
{
tmp.Dispose();
}
tmp?.Dispose();
PSRemotingDataStructureException exception =
new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerKeyExchangeFailed);