From 635c6b41d662d8a0b240ac65f336af2ddd746cfd Mon Sep 17 00:00:00 2001 From: Jared Goodwin Date: Wed, 30 Aug 2023 06:58:29 -0700 Subject: [PATCH] Dispose of scripting shell on error. Remove unused dictionary. --- Agent/Services/ExternalScriptingShell.cs | 30 ++++++++++++------------ 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Agent/Services/ExternalScriptingShell.cs b/Agent/Services/ExternalScriptingShell.cs index d65f4da78..02a1474c0 100644 --- a/Agent/Services/ExternalScriptingShell.cs +++ b/Agent/Services/ExternalScriptingShell.cs @@ -21,7 +21,6 @@ public interface IExternalScriptingShell : IDisposable, IScriptingShell public class ExternalScriptingShell : IExternalScriptingShell { - private static readonly ConcurrentDictionary _sessions = new(); private readonly IConfigService _configService; private readonly ILogger _logger; private readonly ManualResetEvent _outputDone = new(false); @@ -151,7 +150,7 @@ public async Task WriteInput(string input, TimeSpan timeout) _errorOut += Environment.NewLine + ex.Message; // Something's wrong. Let the next command start a new session. - RemoveSession(); + Dispose(); } finally { @@ -167,7 +166,18 @@ protected virtual void Dispose(bool disposing) { if (disposing) { - ShellProcess?.Dispose(); + try + { + if (ShellProcess?.HasExited == false) + { + ShellProcess.Kill(); + ShellProcess.Dispose(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Error while disposing scripting shell process."); + } } _disposedValue = true; @@ -208,22 +218,12 @@ private ScriptResultDto GeneratePartialResult(string input, TimeSpan runtime) HadErrors = !string.IsNullOrWhiteSpace(_errorOut) || (ShellProcess?.HasExited == true && ShellProcess.ExitCode != 0) }; - RemoveSession(); + Dispose(); return partialResult; } private void ProcessIdleTimeout_Elapsed(object? sender, ElapsedEventArgs e) { - RemoveSession(); - } - - private void RemoveSession() - { - ShellProcess?.Kill(); - if (_senderConnectionId is null) - { - return; - } - _sessions.TryRemove(_senderConnectionId, out _); + Dispose(); } private void ShellProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)