MOST ANSWER: (Below is the answer that Samte wrote, the author of the question. However, he sent the answer as part of the question. I will go to a separate answer, marking it as a wiki community, since the author did not move it himself.)
I found refactoring that this is a problem with planners. Return uses an Immediate scheduler, and Repeat uses CurrentThread. The fixed code is below.
public static IObservable<byte[]> AsyncRead(this NetworkStream stream, int bufferSize) { return Observable.Defer(() => { try { return stream.DataAvailable ? AsyncReadChunk(stream, bufferSize) : Observable.Return(new byte[0], Scheduler.CurrentThread); } catch (Exception) { return Observable.Return(new byte[0], Scheduler.CurrentThread); } }) .Repeat() .TakeWhile((dataChunk, index) => dataChunk.Length > 0); }
source share