SEBWIN-361: Fixed concurrency issue when terminating external applications and ensured termination operation continues even with failure.
This commit is contained in:
		
							parent
							
								
									07bb78e637
								
							
						
					
					
						commit
						2b7ff4561a
					
				
					 2 changed files with 22 additions and 7 deletions
				
			
		| 
						 | 
					@ -81,6 +81,7 @@ namespace SafeExamBrowser.Applications
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
				logger.Info("Starting application...");
 | 
									logger.Info("Starting application...");
 | 
				
			||||||
				InitializeInstance(processFactory.StartNew(executablePath));
 | 
									InitializeInstance(processFactory.StartNew(executablePath));
 | 
				
			||||||
 | 
									logger.Info("Successfully started application.");
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			catch (Exception e)
 | 
								catch (Exception e)
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
| 
						 | 
					@ -92,19 +93,29 @@ namespace SafeExamBrowser.Applications
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			applicationMonitor.InstanceStarted -= ApplicationMonitor_InstanceStarted;
 | 
								applicationMonitor.InstanceStarted -= ApplicationMonitor_InstanceStarted;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								try
 | 
				
			||||||
 | 
								{
 | 
				
			||||||
				lock (@lock)
 | 
									lock (@lock)
 | 
				
			||||||
				{
 | 
									{
 | 
				
			||||||
					if (instances.Any() && !settings.AllowRunning)
 | 
										if (instances.Any() && !settings.AllowRunning)
 | 
				
			||||||
					{
 | 
										{
 | 
				
			||||||
					logger.Info("Terminating application...");
 | 
											logger.Info($"Terminating application with {instances.Count} instance(s)...");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
						foreach (var instance in instances)
 | 
											foreach (var instance in instances)
 | 
				
			||||||
						{
 | 
											{
 | 
				
			||||||
 | 
												instance.Terminated -= Instance_Terminated;
 | 
				
			||||||
							instance.Terminate();
 | 
												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)
 | 
							private void ApplicationMonitor_InstanceStarted(Guid applicationId, IProcess process)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -66,7 +66,11 @@ namespace SafeExamBrowser.Applications
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			var terminated = process.HasTerminated;
 | 
								var terminated = process.HasTerminated;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if (!terminated)
 | 
								if (terminated)
 | 
				
			||||||
 | 
								{
 | 
				
			||||||
 | 
									logger.Info("Application instance is already terminated.");
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								else
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
				FinalizeEvents();
 | 
									FinalizeEvents();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue