Observable.Using does not propagate exceptions from resource .Dispose method to stream
- 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.