dotnet / interactive

.NET Interactive combines the power of .NET with many other languages to create notebooks, REPLs, and embedded coding experiences. Share code, explore data, write, and learn across your apps in ways you couldn't before.
MIT License
2.91k stars 389 forks source link

Console output can be lost when cell execution is canceled #3772

Open jonsequitur opened 2 days ago

jonsequitur commented 2 days ago

The console output "Dispose" does not appear when the following code is canceled.


using System.Threading.Tasks;
using Microsoft.DotNet.Interactive;

Console.WriteLine("initialize");

var output = string.Empty.Display("text/plain");
var counter = 1;
while (!KernelInvocationContext.Current.CancellationToken.IsCancellationRequested)
{
    await Task.Delay(500);
    output.Update($"{counter++}");
}

Console.WriteLine("Dispose");

var completed = true;
``` 

This is because the `KernelInvocationContext` stops listening to console output and publishing events when it's completed. The behavior is unintuitive though and could perhaps be changed so that event publishing continues for long enough to capture this output.

More details here:
https://github.com/dotnet/interactive/issues/2987#issuecomment-2489136012