reactive: Unexpected blocking behavior with Repeat

This is actually distilled from a much more complicated debugging session, which boils down to the following query:

using System;
using System.Linq;
using System.Reactive.Linq;

namespace Recursive
{
    class Program
    {
        static void Main(string[] args)
        {
            var timer = Observable
                .Timer(TimeSpan.Zero, TimeSpan.FromSeconds(1))
                .SelectMany(x => Observable.
                    Return(x)
                    .Repeat()
                    .Do(k => Console.WriteLine(k))
                    .TakeWhile((k, i) => i <= k));
            timer.Subscribe();
            Console.ReadLine();
        }
    }
}

In my understanding, this query should repeat the value N as many times as N, and then stop, but for some reason it loops forever on the Repeat, even though TakeWhile is clearly terminating, as can be verified by moving the WriteLine right after the TakeWhile operator (it will return zero once and then nothing, as opposed to an infinite stream of zeroes when it’s right after Repeat).

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 16 (3 by maintainers)

Commits related to this issue

Most upvoted comments

It’s not just Return, any synchronous generator will face this issue. Sometimes this is not a problem because of short sequences and forcing everything to async is a performance overkill. The whole internals have to be redesigned to resolve these situations for good (#494).