Skip to content

Commit

Permalink
Fix a race condition in a TaskStatusTest (dotnet#36645)
Browse files Browse the repository at this point in the history
  • Loading branch information
VSadov authored and stephentoub committed Apr 6, 2019
1 parent df5bba1 commit d00fa24
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/System.Threading.Tasks/tests/Task/TaskStatusTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ private void TaskRun()
{
try
{
ManualResetEventSlim childMre = new ManualResetEventSlim(initialState: false);

if (_createChildTask)
{
TaskCreationOptions childTCO = (TaskCreationOptions)(int)_childCreationOptions;
Expand All @@ -313,18 +315,21 @@ private void TaskRun()
childTCO = TaskCreationOptions.AttachedToParent;
}

_childTask = new Task(ChildTaskRun, null, _childTaskToken, childTCO);
_childTask = new Task(ChildTaskRun, childMre, _childTaskToken, childTCO);

if (_childTask.Status != TaskStatus.Created)
{
Assert.True(false, string.Format("Expecting Child Task status to be Created while getting {0}", _childTask.Status.ToString()));
}

_childTask.Start();

if (_testAction != TestAction.CancelTask && _testAction != TestAction.CancelTaskAndAcknowledge)
{
//
// if cancel action, start the child task after calling Cancel()
// if cancel action, release the child task after calling Cancel()
//
_childTask.Start();
childMre.Set();
}
}

Expand All @@ -336,19 +341,14 @@ private void TaskRun()
switch (_testAction)
{
case TestAction.CancelTask:
if (_createChildTask)
{
_childTask.Start();
}
_taskCts.Cancel();
childMre.Set();

break;
case TestAction.CancelTaskAndAcknowledge:
if (_createChildTask)
{
_childTask.Start();
}
_taskCts.Cancel();
childMre.Set();

if (_taskCts.Token.IsCancellationRequested)
{
throw new OperationCanceledException(_taskCts.Token);
Expand All @@ -367,6 +367,8 @@ private void TaskRun()

private void ChildTaskRun(object o)
{
(o as ManualResetEventSlim)?.Wait();

if (_childTask.Status != TaskStatus.Running)
{
Assert.True(false, string.Format("Expecting Child Task status to be Running while getting {0}", _childTask.Status.ToString()));
Expand Down

0 comments on commit d00fa24

Please sign in to comment.