dotnet / dotnet/reactive

Observable.Using does not propagate exceptions from resource .Dispose method to stream

Open
#1,632 10 comments 1 reaction 0 assignees View on GitHub
[area] Rx
Dominant language
C#
Stars
7.2k
Forks
798
PR merge metrics
No merged PRs in 30d

Description

#### Bug

System.Reactive 5.0.0, Windows .NET 5.0

There are two error-handling cases with `Observable.Using`:

1. Exception is thrown when **creating** the resource (i.e. in provided resource factory method). In this case, exception is propagated to the resulting stream and can be handled in reactive way by using methods like `.Catch`. **Works as expected**.
2. Exception is thrown when calling `Dispose` method of the resource. If this one throws, exception will be **unhandled, crashing the application regardless of onError handlers**.

Reproduction snippet:
```
static async Task Main(string[] args)
{
var source = Observable.Interval(TimeSpan.FromMilliseconds(50));
var withResource = Observable.Using(
() => Disposable.Create(() =>
{
Console.WriteLine("Disposing");
throw new InvalidOperationException("HA");
}),
_ => source.Take(10));

withResource.Subscribe(Console.WriteLine, ex => Console.WriteLine("Caught error"));
await Task.Delay(10000);

Console.WriteLine("Exiting application");
}
```

Expected `onError` handler to work here but exception is thrown as unhandled, crashing the application.

Is this intended behavior? Seems a bit inconsistent given that clearly measures were taken to ensure proper exception handling in resource creation (see `System.Reactive.Linq.ObservableImpl.Run` method).

I understand that allowing Dispose method to throw is bad design and will of course fix problem in my code, but still, accidents happen.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.