Skip to content

Commit

Permalink
Dispose of scripting shell on error. Remove unused dictionary.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Aug 30, 2023
1 parent ff11f18 commit 635c6b4
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions Agent/Services/ExternalScriptingShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public interface IExternalScriptingShell : IDisposable, IScriptingShell

public class ExternalScriptingShell : IExternalScriptingShell
{
private static readonly ConcurrentDictionary<string, IExternalScriptingShell> _sessions = new();
private readonly IConfigService _configService;
private readonly ILogger<ExternalScriptingShell> _logger;
private readonly ManualResetEvent _outputDone = new(false);
Expand Down Expand Up @@ -151,7 +150,7 @@ public async Task<ScriptResultDto> WriteInput(string input, TimeSpan timeout)
_errorOut += Environment.NewLine + ex.Message;

// Something's wrong. Let the next command start a new session.
RemoveSession();
Dispose();
}
finally
{
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 635c6b4

Please sign in to comment.