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

The ToObservable operator can throw unhandled exception #1677

Open
theodorzoulias opened this issue Jan 2, 2022 · 0 comments · May be fixed by #2172
Open

The ToObservable operator can throw unhandled exception #1677

theodorzoulias opened this issue Jan 2, 2022 · 0 comments · May be fixed by #2172

Comments

@theodorzoulias
Copy link
Contributor

theodorzoulias commented Jan 2, 2022

Hi, and happy new year! I think that I have found a bug in the ToObservable extension method for async-enumerable sequences. In case an exception is thrown by the GetAsyncEnumerator invocation, the exception is not propagated by the OnError mechanism of the produced observable, and instead it is rethrown as an unhandled exception that crashes the process. Here is a minimal demonstration of this behavior:

using System;
using System.Linq;
using System.Threading;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        var observable = new BadAsyncEnumerable().ToObservable();
        observable.Subscribe(item => { }, error => Console.WriteLine(error.Message));
        Thread.Sleep(1000);
    }

    class BadAsyncEnumerable : IAsyncEnumerable<int>
    {
        public IAsyncEnumerator<int> GetAsyncEnumerator(
            CancellationToken cancellationToken = default)
        {
            throw new Exception("Oops!");
        }
    }
}

Expected behavior: The error message is printed in the console.

Actual behavior: Unhandled exception.

Try it on Fiddle.

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

Successfully merging a pull request may close this issue.

1 participant