Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tricks for debugging with Reactive Extensions?

I'm looking for ideas on how to make RX more easily debuggable. It can be so very difficult to find the point of failure when a source is passed through combiners and throttles and publishes and such.

So far I've been doing similar things to what I do with complicated Enumerable chains - inserting Do() for tracing, adding a "name" field to an anonymous type part way through, grabbing stack traces sometimes.. But we have perhaps hundreds of producers and maybe thousands of consumers in our system and it's getting very hard to isolate problems.

What kinds of tricks do you have for debugging your RX usage?

like image 924
scobi Avatar asked Nov 22 '11 02:11

scobi


2 Answers

I think a constructive discussion on this topic has been had on the Rx Forums in 2009.

Instead of adding adhoc Do operators into your queries, you would add a custom Log/Trace operator. This operator would capture the Subscription, Disposal, OnNext, OnError and OnCompleted events. Depending on your implementation it could just write to the console, use your favorite Logger library or even create ETW events for OS and Visual Studio integration.

public static class ObservableTrace
{
    public static IObservable<TSource> Trace<TSource>(this IObservable<TSource> source, string name)
    {
        int id = 0;
        return Observable.Create<TSource>(observer => 
        {
            int id1 = ++id;
            Action<string, object> trace = (m, v) => Debug.WriteLine("{0}{1}: {2}({3})", name, id1, m, v);
            trace("Subscribe", "");
            IDisposable disposable = source.Subscribe(
                v => { trace("OnNext", v); observer.OnNext(v); },
                e => { trace("OnError", ""); observer.OnError(e); },
                () => { trace("OnCompleted", ""); observer.OnCompleted(); });
            return () => { trace("Dispose", ""); disposable.Dispose(); };
        });
    }
}
like image 51
Lee Campbell Avatar answered Sep 29 '22 11:09

Lee Campbell


One important trick for catching Rx bugs is to retry debugging with first-chance exceptions enabled, this makes it way more likely that you'll get a real exception message instead of a rethrown one:

like image 24
Ana Betts Avatar answered Sep 29 '22 12:09

Ana Betts