dotnet / dotnet/reactive

ToTask() blocks indefinitely

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

Description

In the latest version of Rx.NET, ToTask is not working as expected anymore.

#### Bug

> Which library version?

5.0.0

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

iOS, Android, .NET6 windows unit test

> What is the use case or problem?

Getting the value of a BehaviorSubject transformed by a Select.

> What is the expected outcome?

The current value of the behavior subject

> What is the actual outcome?

Waits indefinitely

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

none

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

```c#
var bs = new BehaviorSubject(null);
var test = bs.Select(_ => 1).ToTask():
```

#### Workaround

A modern, simple and readable version of ToTask could be this one (tested ok).
await is required to prevent the dispose from being disposed too early or too late. And make the code simpler to read.

```c#
static async Task ToTask2(IObservable o)
{
var tcs = new TaskCompletionSource();

using var d = o
.Catch((Exception e) =>
{
tcs.TrySetException(e);
return Observable.Return(default!);
})
.Subscribe(v => tcs.TrySetResult(v));

return await tcs.Task.ConfigureAwait(false);
}
```

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.