CA-379971: Ensure ParallelAction is not waiting on _lock when no actions are running

There is a chance that all actions have completed before we hit the `Wait` call, we need to make sure we don't hit a deadlock.

This can happen if for instance there is only one action, and it is a "dummy" action, such as the one used in the EUA check.

Also I have removed the compound assignment for the `volatile _completedActionsCount` since Visual Studio was flagging it as a "suspicious usage of a volatile variable". I personally don't think it's a problem but better safe than sorry.

Contains minor whitespace fixes, too

Signed-off-by: Danilo Del Busso <danilo.delbusso@cloud.com>
This commit is contained in:
Danilo Del Busso 2023-10-25 09:53:09 +01:00 committed by Konstantina Chremmou
parent 0f4d105518
commit 82ffd50271

View File

@ -57,7 +57,7 @@ namespace XenAdmin.Actions
private readonly int _maxNumberOfParallelActions;
private readonly int _actionsCount;
private readonly object _lock = new object();
private volatile int _completedActionsCount ;
private volatile int _completedActionsCount;
/// <summary>
/// Use this constructor to create a cross connection ParallelAction.
@ -124,7 +124,10 @@ namespace XenAdmin.Actions
lock (_lock)
{
Monitor.Wait(_lock);
// CA-379971: There is a chance that all actions have completed before we
// hit the Wait call, we need to make sure we don't hit a deadlock
if (_completedActionsCount != _actionsCount)
Monitor.Wait(_lock);
}
}
@ -165,7 +168,7 @@ namespace XenAdmin.Actions
foreach (var connection in _actionsByConnection.Keys)
{
foreach (var action in _actionsByConnection[connection])
foreach (var action in _actionsByConnection[connection])
total += action.PercentComplete;
}
@ -180,7 +183,7 @@ namespace XenAdmin.Actions
sender.Completed -= action_Completed;
lock (_lock)
{
_completedActionsCount++;
_completedActionsCount = _completedActionsCount + 1;
if (_completedActionsCount == _actionsCount)
{
Monitor.Pulse(_lock);