From 001bea65e4966eec54fd538a15e6166a5bce8a96 Mon Sep 17 00:00:00 2001 From: Shyam N Date: Mon, 9 Oct 2023 11:54:16 -0700 Subject: [PATCH] Fix bug related to resetting of KernelScheduler._currentlyRunningTopLevelOperation (#3235) `_currentlyRunningTopLevelOperation` was not being reset in the case where a top-level operation would schedule child operations. In this case, `_currentlyRunningOperation` was correctly being reset when each child operation was completed. However, when the parent operation was completed, we would fail to reset `_currentlyRunningTopLevelOperation` since we were comparing `_currentlyRunningTopLevelOperation` with the value of `_currentlyRunningOperation` (and `_currentlyRunningOperation` would have a value null at this point because it was reset to null when the child operation was completed above). The above bug was breaking parenting (and command token assignment) for subsequent commands. Subsequent commands were being incorrectly parented to the stale value reflected in `_currentlyRunningTopLevelOperation`. And this in turn was leading to hangs. This was an unfortunate regression triggered by the changes in #3222 that fixed a race condition. The regression happened because were missing tests that exercised the above scenario. I have included some tests for this as part of the current commit. Also add a skipped test for related issue #3236. --- ...nteractive_api_is_not_changed.approved.txt | 1 + .../KernelSchedulerTests.cs | 107 +++++++++++++++++- .../KernelScheduler.cs | 11 +- 3 files changed, 112 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.DotNet.Interactive.ApiCompatibility.Tests/ApiCompatibilityTests.Interactive_api_is_not_changed.approved.txt b/src/Microsoft.DotNet.Interactive.ApiCompatibility.Tests/ApiCompatibilityTests.Interactive_api_is_not_changed.approved.txt index 150eb14ff6..29795d2cfb 100644 --- a/src/Microsoft.DotNet.Interactive.ApiCompatibility.Tests/ApiCompatibilityTests.Interactive_api_is_not_changed.approved.txt +++ b/src/Microsoft.DotNet.Interactive.ApiCompatibility.Tests/ApiCompatibilityTests.Interactive_api_is_not_changed.approved.txt @@ -267,6 +267,7 @@ Microsoft.DotNet.Interactive public System.Threading.Tasks.Task Invoke(Microsoft.DotNet.Interactive.Commands.KernelCommand command, KernelInvocationContext context) public class KernelScheduler, IKernelScheduler, System.IDisposable .ctor() + public T CurrentValue { get;} public System.Void CancelCurrentOperation() public System.Void Dispose() protected System.Boolean IsChildOperation(T current, T incoming) diff --git a/src/Microsoft.DotNet.Interactive.Tests/KernelSchedulerTests.cs b/src/Microsoft.DotNet.Interactive.Tests/KernelSchedulerTests.cs index 82e5d08418..92013bc961 100644 --- a/src/Microsoft.DotNet.Interactive.Tests/KernelSchedulerTests.cs +++ b/src/Microsoft.DotNet.Interactive.Tests/KernelSchedulerTests.cs @@ -4,10 +4,11 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using FluentAssertions; using System.Linq; using System.Threading; using System.Threading.Tasks; +using FluentAssertions; +using FluentAssertions.Execution; using Microsoft.DotNet.Interactive.Tests.Utility; using Microsoft.DotNet.Interactive.Utility; using Pocket; @@ -90,7 +91,7 @@ public async Task top_level_scheduled_work_does_not_execute_in_parallel() maxObservedParallelism.Should().Be(1); } - + public class TestKernelScheduler : KernelScheduler { private readonly Func _isChildOperation; @@ -496,6 +497,108 @@ await scheduler.RunAsync("inner", async _ => "outer 2"); } + [Fact] + public async Task CurrentValue_reflects_the_work_that_is_in_progress() + { + using var scheduler = new KernelScheduler(); + + var work = 1000; + await scheduler.RunAsync(work, async _ => + { + scheduler.CurrentValue.Should().Be(work); + return 2000; + }); + } + + [Fact] + public async Task CurrentValue_reflects_the_child_operation_when_called_from_within_a_child_operation() + { + using var scheduler = new TestKernelScheduler((o, i) => o == "outer" && i == "inner"); + + await scheduler.RunAsync("outer", async _ => + { + await scheduler.RunAsync("inner", async _ => + { + scheduler.CurrentValue.Should().Be("inner"); + return default; + }); + + return default; + }); + } + + [Fact] + public async Task CurrentValue_reflects_the_parent_operation_when_called_from_within_a_parent_operation() + { + using var scheduler = new TestKernelScheduler((o, i) => o == "outer" && i == "inner"); + + await scheduler.RunAsync("outer", async _ => + { + await scheduler.RunAsync("inner", async _ => + { + return default; + }); + + scheduler.CurrentValue.Should().Be("outer"); + return default; + }); + } + + [Fact] + public async Task CurrentValue_is_reset_once_all_work_is_complete() + { + using var scheduler = new TestKernelScheduler((o, i) => o == "outer" && i == "inner"); + + await scheduler.RunAsync("outer", async _ => + { + await scheduler.RunAsync("inner", async _ => + { + return default; + }); + + return default; + }); + + scheduler.CurrentValue.Should().BeNull(); + } + + [Fact(Skip = "Disabled pending https://github.com/dotnet/interactive/issues/3236")] + public async Task CurrentValue_reflects_correct_value_within_parent_child_as_well_as_grand_child_operations() + { + using var scheduler = + new TestKernelScheduler( + (o, i) => o == "parent" && i == "child" || o == "child" && i == "grandchild" || o == "parent" && i == "grandchild"); + + using var _ = new AssertionScope(); + + scheduler.CurrentValue.Should().BeNull(); + + await scheduler.RunAsync("parent", async _ => + { + scheduler.CurrentValue.Should().Be("parent"); + + await scheduler.RunAsync("child", async _ => + { + scheduler.CurrentValue.Should().Be("child"); + + await scheduler.RunAsync("grandchild", async _ => + { + scheduler.CurrentValue.Should().Be("grandchild"); + return default; + }); + + // See https://github.com/dotnet/interactive/issues/3236 + scheduler.CurrentValue.Should().Be("child"); + return default; + }); + + scheduler.CurrentValue.Should().Be("parent"); + return default; + }); + + scheduler.CurrentValue.Should().BeNull(); + } + [Fact] public async Task concurrent_schedulers_do_not_interfere_with_one_another() { diff --git a/src/Microsoft.DotNet.Interactive/KernelScheduler.cs b/src/Microsoft.DotNet.Interactive/KernelScheduler.cs index 9f3378eae9..fa8dffd6e5 100644 --- a/src/Microsoft.DotNet.Interactive/KernelScheduler.cs +++ b/src/Microsoft.DotNet.Interactive/KernelScheduler.cs @@ -44,14 +44,15 @@ public void CancelCurrentOperation() { if (_currentlyRunningTopLevelOperation is { } operation) { - operation.TaskCompletionSource.TrySetCanceled(_schedulerDisposalSource.Token); _currentlyRunningTopLevelOperation = null; + operation.TaskCompletionSource.TrySetCanceled(_schedulerDisposalSource.Token); } } - internal T CurrentValue => (_currentlyRunningOperation ?? _currentlyRunningTopLevelOperation) is { } currentOperation - ? currentOperation.Value - : default; + public T CurrentValue => + (_currentlyRunningOperation ?? _currentlyRunningTopLevelOperation) is { } currentOperation + ? currentOperation.Value + : default; public Task RunAsync( T value, @@ -182,7 +183,7 @@ void CompleteWithException(Exception exception) void ResetCurrentlyRunning() { - if (ReferenceEquals(_currentlyRunningTopLevelOperation, _currentlyRunningOperation)) + if (ReferenceEquals(_currentlyRunningTopLevelOperation, operation)) { _currentlyRunningTopLevelOperation = null; }