Skip to content

Commit d00fa24

Browse files
VSadovstephentoub
authored andcommitted
Fix a race condition in a TaskStatusTest (dotnet#36645)
See:https://github.com/dotnet/coreclr/issues/20706
1 parent df5bba1 commit d00fa24

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/System.Threading.Tasks/tests/Task/TaskStatusTest.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ private void TaskRun()
300300
{
301301
try
302302
{
303+
ManualResetEventSlim childMre = new ManualResetEventSlim(initialState: false);
304+
303305
if (_createChildTask)
304306
{
305307
TaskCreationOptions childTCO = (TaskCreationOptions)(int)_childCreationOptions;
@@ -313,18 +315,21 @@ private void TaskRun()
313315
childTCO = TaskCreationOptions.AttachedToParent;
314316
}
315317

316-
_childTask = new Task(ChildTaskRun, null, _childTaskToken, childTCO);
318+
_childTask = new Task(ChildTaskRun, childMre, _childTaskToken, childTCO);
317319

318320
if (_childTask.Status != TaskStatus.Created)
319321
{
320322
Assert.True(false, string.Format("Expecting Child Task status to be Created while getting {0}", _childTask.Status.ToString()));
321323
}
324+
325+
_childTask.Start();
326+
322327
if (_testAction != TestAction.CancelTask && _testAction != TestAction.CancelTaskAndAcknowledge)
323328
{
324329
//
325-
// if cancel action, start the child task after calling Cancel()
330+
// if cancel action, release the child task after calling Cancel()
326331
//
327-
_childTask.Start();
332+
childMre.Set();
328333
}
329334
}
330335

@@ -336,19 +341,14 @@ private void TaskRun()
336341
switch (_testAction)
337342
{
338343
case TestAction.CancelTask:
339-
if (_createChildTask)
340-
{
341-
_childTask.Start();
342-
}
343344
_taskCts.Cancel();
345+
childMre.Set();
344346

345347
break;
346348
case TestAction.CancelTaskAndAcknowledge:
347-
if (_createChildTask)
348-
{
349-
_childTask.Start();
350-
}
351349
_taskCts.Cancel();
350+
childMre.Set();
351+
352352
if (_taskCts.Token.IsCancellationRequested)
353353
{
354354
throw new OperationCanceledException(_taskCts.Token);
@@ -367,6 +367,8 @@ private void TaskRun()
367367

368368
private void ChildTaskRun(object o)
369369
{
370+
(o as ManualResetEventSlim)?.Wait();
371+
370372
if (_childTask.Status != TaskStatus.Running)
371373
{
372374
Assert.True(false, string.Format("Expecting Child Task status to be Running while getting {0}", _childTask.Status.ToString()));

0 commit comments

Comments
 (0)