dotnet / reactive

The Reactive Extensions for .NET
http://reactivex.io
MIT License
6.73k stars 751 forks source link

AsyncObservable.DeferAsync calls itselfs recursively. #1976

Closed pwretmo closed 1 year ago

pwretmo commented 1 year ago

Hello and thank you for using dotnet/reactive. Please select a category and detail your issue by answering the questions there:

Bug

Despite our best efforts, bugs can slip into releases or corner cases forgotten about. We will try our best to remedy the situation and/or provide workarounds. Note that certain (odd) behaviors are by design and as such are not considered bugs.

Which library version?

System.Reactive.Async 6.0.0-alpha.18

What are the platform(s), environment(s) and related component version(s)?

Windows 10 x64, .NET 7.0

What is the use case or problem?

The DeferAsync overload taking an CancellationToken calls itself recursively.

public static IAsyncObservable<TSource> AsyncObservable.DeferAsync<TSource>(Func<CancellationToken, ValueTask<IAsyncObservable<TSource>>> observableFactory) => DeferAsync(observableFactory);

What is the expected outcome?

An IAsyncObservable is returned.

What is the actual outcome?

Stackoverflow exception

The program '[36248] testhost.exe: Program Trace' has exited with code 0 (0x0).
The program '[36248] testhost.exe' has exited with code 3221225477 (0xc0000005) 'Access violation'.
The program '[36248] testhost.exe' has exited with code 4294967295 (0xffffffff).

What is the stacktrace of the exception(s) if any?

The active test run was aborted. Reason: Test host process crashed : Stack overflow.
Repeat 24063 times:

   at System.Reactive.Linq.AsyncObservable.DeferAsync[[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Func`2<System.Threading.CancellationToken,System.Threading.Tasks.ValueTask`1<System.IAsyncObservable`1<System.__Canon>>>)

Do you have a code snippet or project that reproduces the problem?

AsyncObservable.DeferAsync(cancellationToken => ValueTask.FromResult(AsyncObservable.Return(Unit.Default)));
idg10 commented 1 year ago

Thanks for this.

This looks like a pretty straightforward mistake. The DeferAsync method is meant to call the equivalent Defer, but calls itself instead.

(As for how on earth something this obvious got missed, this is an experimental project and never had any tests. Our goal is to work out how to take the existing test suite for normal Rx and apply it to AsyncRx.NET. #1900 This would have been picked up immediately if it had ever been tested.)

I've created a PR to fix this. We'll do a new preview release at some point, but in the meantime if you're blocked, you can just invoke Defer instead. That DeferAsync overload is meant to be an alias for a particular overload of Defer. (I don't know the history, but my guess is that in some situations, calling DeferAsync narrows down the number of possibilities the compiler type inference has to consider, which might help avoid explicit type arguments in some scenarios.)