Skip to content

Commit

Permalink
fixed potential deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
tallesl committed Mar 8, 2017
1 parent bcbade0 commit af54d4c
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions Library/JobManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,22 @@ public static void Stop()
/// </summary>
public static void StopAndBlock()
{
Stop();

lock (_running)
Stop();

var tasks = new Task[0];

// Even though Stop() was just called, a scheduling may be happening right now, that's why the loop.
// Simply waiting for the tasks inside the lock causes a deadlock (a task may try to remove itself from
// running, but it can't access the collection, it's blocked by the wait).
do
{
Task.WaitAll(_running.Select(t => t.Item2).ToArray());
}
lock (_running)
{
tasks = _running.Select(t => t.Item2).ToArray();
}

Task.WaitAll(tasks);
} while (tasks.Any());
}

#endregion
Expand Down

0 comments on commit af54d4c

Please sign in to comment.