Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix removal of scripting shell after error/timeout. #721

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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