The problem isn't anything to do with Retry
- it's Observable.Throw
because it defaults to scheduling its OnError
on the immediate scheduler. Change your first line to this and you are golden:
var retry = Observable.Throw<int>(new Exception(), Scheduler.Default).Retry();
In general Rx tries to use the most immediate scheduler it can. Scheduler.Default
will use the current platform's preferred scheduler that introduces concurrency.
Retry
will initially subscribe on the current thread (which is fine) and then subscribe for each retry on whatever thread Throw
terminates on.
EDIT
Although the fix above works - it's probably not the best way. It's actually good that Throw
and Retry
try to use the immediate scheduler as this is the most computationally efficient way. The code above will cause every iteration of Throw
to run on a different thread.
A better and more idiomatic Rx solution would be to edit the third line instead:
var subscription = retry.SubscribeOn(Scheduler.Default).Subscribe();
This causes only the initial subscription to Retry
to happen asynchronously on a different Thread - and the rest of the processing will stay on that one thread.