From c7a4fc8e6eb179270e6bbdec4503ce5cc8178bd4 Mon Sep 17 00:00:00 2001 From: Alex Maitland Date: Fri, 25 Jun 2021 14:08:50 +1000 Subject: [PATCH] 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 --- .../ConcurrentMethodRunnerQueueFacts.cs | 69 +++++++++++++++++++ .../Internals/ConcurrentMethodRunnerQueue.cs | 12 ++-- 2 files changed, 77 insertions(+), 4 deletions(-) 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)