Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application doesn't terminate when unhandled exception is thrown from ReactiveCommand handler #16017

Open
yudindm opened this issue Jun 13, 2024 · 3 comments
Labels

Comments

@yudindm
Copy link

yudindm commented Jun 13, 2024

Describe the bug

As I understand the Avalonia and Reactive UI documentation when unhandled exception is thrown from ReactiveCommand an application should terminate. But this does not happen.

To Reproduce

  1. Create application from Avalonia MVVM template.

  2. Add ReactiveCommand to MainWindowViewModel which just throws exception.

     // in MainWindowViewModel class
         public ReactiveCommand<Unit, Unit> ThrowExceptionCommand { get; }
    
     // in MainWindowViewModel constructor
         ThrowExceptionCommand = ReactiveCommand.Create(() =>
         {
             throw new Exception("Exception should terminate app.");
         });
    
  3. Add Button to invoke ReactiveCommand added in step 2 in MainWindow.axaml.

    // in MainWindow.axaml
     <StackPanel>
         <TextBlock Text="{Binding Greeting}"/>
         <Button Content="Throw Exception Command" Command="{Binding ThrowExceptionCommand}"/>
     </StackPanel>
    
  4. Start application and press the Throw Exception Command Button

  5. See that nothing happens. Application stays open.

Expected behavior

Applcation should terminate.

Avalonia version

11.0.6

OS

No response

Additional context

No response

@yudindm yudindm added the bug label Jun 13, 2024
@maxkatz6
Copy link
Member

Override RxApp.DefaultExceptionHandler if you want a proper control over unhandled exceptions in Rx pipeline.

@maxkatz6
Copy link
Member

About this issue, it seems Reactive doesn't behave quite right, when scheduler has an optimization for the current thread.
See reactiveui/ReactiveUI#1859 and reactiveui/ReactiveUI#2043.

cc @worldbeater @kekekeks

@yudindm
Copy link
Author

yudindm commented Jun 15, 2024

Override RxApp.DefaultExceptionHandler if you want a proper control over unhandled exceptions in Rx pipeline.

From my point of view the proper handling of an unhandled exception is to terminate an application.
As a workaround I added RxApp.DefaultExceptionHandler to bypass AvaloniaScheduler current thread optimization like that:

RxApp.DefaultExceptionHandler = Observer.Create<Exception>(exception =>
        {
            Dispatcher.UIThread.Post(() => throw new UnhandledErrorException("Unhandled exception was thrown.", exception),
                DispatcherPriority.Send);
        });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants