Infinite Catch in Reactive Extenions
<p>My question relates to Rx and Catch operator. Lets say I have Timeout on my observable and each time timeout occurs I would like to recreate underlying observable (Catch) and do the same thing (add timeout and catch).</p>

<p>Below I pasted example code. For purpose of this example Timeout occurs all the time every 2 seconds. From my observation this code can't work infinitely, somehow after recreation something is holding reference to old observable leftovers. Those leftovers accumulate all the time when Catch is called.</p>

<p>Most suspicious line is the last one, were some kind of self-reference is present. But I can't actually visualise myself why it might be wrong? Also is there any way to create observable with similar logic that will work forever?</p>

<pre><code>    public static IObservable&lt;string&gt; CreateReliableStream(this IObservable&lt;string&gt; targetObservable, Func&lt;IObservable&lt;string&gt;&gt; recreateObservable)
    {
        return targetObservable
            .Timeout(TimeSpan.FromSeconds(2))
            .Catch&lt;string, Exception&gt;(exception =&gt; ReconnectOnError(exception, recreateObservable));
    }

    private static IObservable&lt;string&gt; ReconnectOnError(Exception exception, Func&lt;IObservable&lt;string&gt;&gt; recreateObservable)
    {
        GC.Collect(); // For debug - make sure all unreferenced object are removed

        return recreateObservable()
            .Timeout(TimeSpan.FromSeconds(2))
            .Catch&lt;string, Exception&gt;(ex =&gt; ReconnectOnError(ex, recreateObservable));
    }
</code></pre>