Skip to content

Commit

Permalink
Merge pull request #6014 from smoogipoo/fix-threadedtaskscheduler-rac…
Browse files Browse the repository at this point in the history
…e-condition

Fix race condition in ThreadedTaskScheduler when adding tasks
  • Loading branch information
peppy authored Oct 4, 2023
2 parents e2c94cd + 4257d72 commit 907e62c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 29 additions & 0 deletions osu.Framework.Tests/Threading/ThreadedTaskSchedulerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,34 @@ public void EnsureScheduledTaskReturnsOnDisposal()
Assert.That(run.Wait(30000));
Assert.That(exited.Wait(30000));
}

[Test]
[Repeat(1000)]
public void QueueAndDisposeStressTest()
{
ThreadedTaskScheduler scheduler = new ThreadedTaskScheduler(1, string.Empty);

Thread disposeThread = new Thread(() =>
{
scheduler.Dispose();
})
{
IsBackground = true
};

Thread queueThread = new Thread(() =>
{
Task.Factory.StartNew(() => { }, CancellationToken.None, TaskCreationOptions.HideScheduler, scheduler);
})
{
IsBackground = true
};

disposeThread.Start();
queueThread.Start();

disposeThread.Join();
queueThread.Join();
}
}
}
2 changes: 1 addition & 1 deletion osu.Framework/Threading/ThreadedTaskScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected override void QueueTask(Task task)
{
tasks.Add(task);
}
catch (ObjectDisposedException)
catch (Exception ex) when (ex is InvalidOperationException or ObjectDisposedException)
{
// tasks may have been disposed. there's no easy way to check on this other than catch for it.
Logger.Log($"Task was queued for execution on a {nameof(ThreadedTaskScheduler)} ({name}) after it was disposed. The task will be executed inline.");
Expand Down

0 comments on commit 907e62c

Please sign in to comment.