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

[BUG] ReactiveCommand's IsExecuting is not set to true if it's executed again after throwing an exception #2894

Closed
n-ski opened this issue Aug 21, 2021 · 4 comments

Comments

@n-ski
Copy link

n-ski commented Aug 21, 2021

Describe the bug
If a command throws an exception and it gets executed again later, IsExecuting will remain false.

Steps To Reproduce
Basically added 2 lines of code to an existing test.

[Fact]
public async Task CommandIsExecutingTest()
{
    var subj = new Subject<Unit>();
    bool isExecuting = false;
    Exception? fail = null;
    var fixture = ReactiveCommand.CreateFromTask(async () =>
    {
        await subj.Take(1);
        throw new Exception();
    });
    fixture.IsExecuting.Subscribe(x => isExecuting = x);
    fixture.ThrownExceptions.Subscribe(ex => fail = ex);

    Assert.False(isExecuting);
    Assert.Null(fail);

    fixture.Execute().Subscribe();
    Assert.True(isExecuting);
    Assert.Null(fail);

    subj.OnNext(Unit.Default);

    // Wait 1 ms to allow execution to complete
    await Task.Delay(1).ConfigureAwait(false);

    Assert.False(isExecuting);
    Assert.NotNull(fail);

    fixture.Execute().Subscribe();
    Assert.True(isExecuting); // Fails here.
}

Expected behaviour
IsExecuting should be true when the command is executing.

Environment

  • OS: Windows 10, .NET SDK 5.0.206, Runtime 5.0.9
  • ReactiveUI Version: 15.1.1
@n-ski n-ski added the bug label Aug 21, 2021
@ChrisPulman
Copy link
Member

We are trying to come up with a resolve for this but the root cause is in the System.Reactive code
dotnet/reactive#1256
This issue has been outstanding for quite some time.

@sijk
Copy link

sijk commented Oct 28, 2021

For what it's worth, if anyone else is trying to work around this issue, the following approach of explicitly catching the exception within the command observable worked for me:

Subject<Exception> _exceptions = new();

CancelableCommand = ReactiveCommand.CreateFromObservable(
    () => Observable
        .StartAsync(DoSomethingAsync)
        .TakeUntil(CancelCommand)
        .ObserveOn(RxApp.MainThreadScheduler)
        .Catch((Exception ex) =>
        {
            _exceptions.OnNext(ex);
            return Observable.Return(Unit.Default);
        }),
    canExecute);

Then rather than subscribing to CancelableCommand.ThrownExceptions you subscribe to _exceptions.

@glennawatson
Copy link
Contributor

Closing this issue due to it being related to the Rx team not RxUI.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants