diff --git a/CefSharp.Test/Framework/ConcurrentMethodRunnerQueueFacts.cs b/CefSharp.Test/Framework/ConcurrentMethodRunnerQueueFacts.cs index 469edfcae..67c4341ff 100644 --- a/CefSharp.Test/Framework/ConcurrentMethodRunnerQueueFacts.cs +++ b/CefSharp.Test/Framework/ConcurrentMethodRunnerQueueFacts.cs @@ -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; } + /// + /// Recreate the workflow that would appear to be the root cause of + /// https://github.com/cefsharp/CefSharp/discussions/3638 + /// + [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(() => task.Start(TaskScheduler.Default)); + } + + /// + /// 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. + /// + [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() { diff --git a/CefSharp/Internals/ConcurrentMethodRunnerQueue.cs b/CefSharp/Internals/ConcurrentMethodRunnerQueue.cs index 902a67cf8..3f87b951d 100644 --- a/CefSharp/Internals/ConcurrentMethodRunnerQueue.cs +++ b/CefSharp/Internals/ConcurrentMethodRunnerQueue.cs @@ -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 ExecuteMethodInvocation(MethodInvocation methodInvocation)