Core - ConcurrentMethodRunnerQueue.Enqueue throws InvalidOperationException

When ConcurrentMethodRunnerQueue.Dispose is called from a different thread it appears that
the CancellationTokenSource is cancelled (via Dispose) inbetween creation of the Task
and calling Task.Start(TaskScheduler).

Switch from using Task constructor and Task.Start to the preferred Task.Run method.

Added additional unit tests.

Issue #3639
This commit is contained in:
Alex Maitland
2021-06-25 14:08:50 +10:00
parent aa8a02f5de
commit c7a4fc8e6e
2 changed files with 77 additions and 4 deletions
@@ -2,6 +2,7 @@
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.Threading;
using System.Threading.Tasks;
using CefSharp.Example.JavascriptBinding;
@@ -21,6 +22,74 @@ namespace CefSharp.Test.Framework
this.output = output;
}
/// <summary>
/// Recreate the workflow that would appear to be the root cause of
/// https://github.com/cefsharp/CefSharp/discussions/3638
/// </summary>
[Fact]
public void SimulateStartOnTaskAlreadyCompleted()
{
var cts = new CancellationTokenSource();
//Create a new Task
var task = new Task(async () =>
{
await Task.Delay(100);
}, cts.Token);
//Cancel before started
cts.Cancel();
Assert.Throws<InvalidOperationException>(() => task.Start(TaskScheduler.Default));
}
/// <summary>
/// Proposed fix for
/// https://github.com/cefsharp/CefSharp/discussions/3638
/// It's difficult to recreate the exact threading requirements, so simulating the
/// behaviour to test code executes without exception.
/// </summary>
[Fact]
public void SimulateTaskRunStartOnTaskAlreadyCompleted()
{
var cts = new CancellationTokenSource();
//Cancel before started
cts.Cancel();
var task = Task.Run(async () =>
{
await Task.Delay(100);
}, cts.Token);
Assert.NotNull(task);
Assert.Equal(TaskStatus.Canceled, task.Status);
}
[Fact]
public void DisposeConcurrentMethodRunnerQueueThenEnqueueInvocation()
{
var methodInvocation = new MethodInvocation(1, 1, 1, "Testing", 1);
methodInvocation.Parameters.Add("Echo Me!");
var objectRepository = new JavascriptObjectRepository
{
NameConverter = null
};
var methodRunnerQueue = new ConcurrentMethodRunnerQueue(objectRepository);
//Dispose
methodRunnerQueue.Dispose();
//Enqueue
var ex = Record.Exception(() => methodRunnerQueue.Enqueue(methodInvocation));
//Ensure no exception thrown
Assert.Null(ex);
}
[Fact]
public async Task StopConcurrentMethodRunnerQueueWhenMethodRunning()
{
@@ -34,13 +34,19 @@ namespace CefSharp.Internals
public void Enqueue(MethodInvocation methodInvocation)
{
if(cancellationTokenSource.IsCancellationRequested)
if (cancellationTokenSource.IsCancellationRequested)
{
return;
}
var task = new Task(async () =>
//Enqueue on ThreadPool
Task.Run(async () =>
{
if (cancellationTokenSource.IsCancellationRequested)
{
return;
}
var result = await ExecuteMethodInvocation(methodInvocation).ConfigureAwait(false);
if (cancellationTokenSource.IsCancellationRequested)
@@ -113,8 +119,6 @@ namespace CefSharp.Internals
}
}, cancellationTokenSource.Token);
task.Start(TaskScheduler.Default);
}
private async Task<MethodInvocationResult> ExecuteMethodInvocation(MethodInvocation methodInvocation)