CommunityToolkit icon indicating copy to clipboard operation
CommunityToolkit copied to clipboard

FlowExceptionsToTaskScheduler not bubbling up to TaskScheduler.UnobservedTaskException

Open Qythyx opened this issue 2 years ago • 4 comments

I'm trying to use FlowExceptionsToTaskScheduler within RelayCommand, but it doesn't seem to be working.

Early in my app I have this:

		...stuff...
		TaskScheduler.UnobservedTaskException += HandleUnobservedTaskException;
	}

	private void HandleUnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
	{
		ExceptionHandler.LogException(e.Exception);
	}

Then I have in a view model:

	[RelayCommand(FlowExceptionsToTaskScheduler = true)]
	private async Task Refresh()
	{
		_ = await DataManager.RefreshOrderHistoryAsync();
		IsRefreshing = false;
	}

For my testing I'm intentionally throwing an exception within DataManager.RefreshOrderHistoryAsync, but that seems to be silently swallowed somewhere and HandleUnobservedTaskException is never called.

Should this scenario work, or am I doing something wrong?

Qythyx avatar May 10 '23 12:05 Qythyx

FYI @Sergio0694

michael-hawker avatar Aug 25 '23 23:08 michael-hawker

@Qythyx that handler is not called immediately when the exception is thrown, but rather it's invoked by the Task finalizer. It's possible you're not seeing that just because a GC hasn't triggered just yet and that Task is still alive. Try forcing a GC collect and check whether that does work? We're using that option in several cases and I've never had any issues with it 🤔

Sergio0694 avatar Aug 28 '23 11:08 Sergio0694

@Sergio0694 , thanks for the suggestion, but I tried forcing a GC and still didn't see my handler get called.

Qythyx avatar Aug 28 '23 12:08 Qythyx

Would you be able to provide a minimal repro for this in a standalone .NET console app? Basically a unit test we can also use to check. Because we do have some and they're passing 🤔

Sergio0694 avatar Aug 28 '23 13:08 Sergio0694