From 2b7ff4561afdf228369300645e5e687e93a1256d Mon Sep 17 00:00:00 2001 From: dbuechel Date: Wed, 26 Feb 2020 08:49:16 +0100 Subject: [PATCH] SEBWIN-361: Fixed concurrency issue when terminating external applications and ensured termination operation continues even with failure. --- .../ExternalApplication.cs | 23 ++++++++++++++----- .../ExternalApplicationInstance.cs | 6 ++++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/SafeExamBrowser.Applications/ExternalApplication.cs b/SafeExamBrowser.Applications/ExternalApplication.cs index 5b500e2a..a54a43a2 100644 --- a/SafeExamBrowser.Applications/ExternalApplication.cs +++ b/SafeExamBrowser.Applications/ExternalApplication.cs @@ -81,6 +81,7 @@ namespace SafeExamBrowser.Applications { logger.Info("Starting application..."); InitializeInstance(processFactory.StartNew(executablePath)); + logger.Info("Successfully started application."); } catch (Exception e) { @@ -92,18 +93,28 @@ namespace SafeExamBrowser.Applications { applicationMonitor.InstanceStarted -= ApplicationMonitor_InstanceStarted; - lock (@lock) + try { - if (instances.Any() && !settings.AllowRunning) + lock (@lock) { - logger.Info("Terminating application..."); - - foreach (var instance in instances) + if (instances.Any() && !settings.AllowRunning) { - instance.Terminate(); + logger.Info($"Terminating application with {instances.Count} instance(s)..."); + + foreach (var instance in instances) + { + instance.Terminated -= Instance_Terminated; + instance.Terminate(); + } + + logger.Info("Successfully terminated application."); } } } + catch (Exception e) + { + logger.Error($"Failed to terminate application!", e); + } } private void ApplicationMonitor_InstanceStarted(Guid applicationId, IProcess process) diff --git a/SafeExamBrowser.Applications/ExternalApplicationInstance.cs b/SafeExamBrowser.Applications/ExternalApplicationInstance.cs index 0ca0ea46..fbf1c567 100644 --- a/SafeExamBrowser.Applications/ExternalApplicationInstance.cs +++ b/SafeExamBrowser.Applications/ExternalApplicationInstance.cs @@ -66,7 +66,11 @@ namespace SafeExamBrowser.Applications var terminated = process.HasTerminated; - if (!terminated) + if (terminated) + { + logger.Info("Application instance is already terminated."); + } + else { FinalizeEvents();