From d69e9d7d270a2efc071a281613acb8430c216d4f Mon Sep 17 00:00:00 2001 From: Shyam Namboodiripad Date: Mon, 9 Oct 2023 09:47:40 -0700 Subject: [PATCH 1/2] Fix bug related to resetting of KernelScheduler._currentlyRunningTopLevelOperation. _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 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 a previous change that fixed a race condition. The regression happened because were were missing a test for the above scenario. I have included some tests for this as part of the current commit. --- ...nteractive_api_is_not_changed.approved.txt | 1 + .../KernelSchedulerTests.cs | 65 +++++++++++++++++++ .../KernelScheduler.cs | 11 ++-- 3 files changed, 72 insertions(+), 5 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 546af66c0e..8c59ced9ec 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..848b650436 100644 --- a/src/Microsoft.DotNet.Interactive.Tests/KernelSchedulerTests.cs +++ b/src/Microsoft.DotNet.Interactive.Tests/KernelSchedulerTests.cs @@ -495,6 +495,71 @@ await scheduler.RunAsync("inner", async _ => "inner 2", "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] 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..d84ed1f5da 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; } From 11b94292fe33a2ac10b8745a2432b2be0e5d9938 Mon Sep 17 00:00:00 2001 From: Shyam Namboodiripad Date: Mon, 9 Oct 2023 11:14:20 -0700 Subject: [PATCH 2/2] Add a skipped test for issue #3236. --- .../KernelSchedulerTests.cs | 44 +++++++++++++++++-- .../KernelScheduler.cs | 2 +- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.DotNet.Interactive.Tests/KernelSchedulerTests.cs b/src/Microsoft.DotNet.Interactive.Tests/KernelSchedulerTests.cs index 848b650436..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; @@ -495,7 +496,7 @@ await scheduler.RunAsync("inner", async _ => "inner 2", "outer 2"); } - + [Fact] public async Task CurrentValue_reflects_the_work_that_is_in_progress() { @@ -561,6 +562,43 @@ await scheduler.RunAsync("inner", async _ => 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 d84ed1f5da..fa8dffd6e5 100644 --- a/src/Microsoft.DotNet.Interactive/KernelScheduler.cs +++ b/src/Microsoft.DotNet.Interactive/KernelScheduler.cs @@ -49,7 +49,7 @@ public void CancelCurrentOperation() } } - public T CurrentValue => + public T CurrentValue => (_currentlyRunningOperation ?? _currentlyRunningTopLevelOperation) is { } currentOperation ? currentOperation.Value : default;