title: Rxjava retryWhen called instantly
body: <p>I'm having a very specific problem or misunderstanding with rxjava that someone hopefully can help with. </p>
  
  <p>I'm running rxjava 2.1.5 and have the following code snippet:</p>
  
  <pre><code>public static void main(String[] args) {
  
      final Observable&lt;Object&gt; observable = Observable.create(emitter -&gt; {
          // Code ... 
      });
  
      observable.subscribeOn(Schedulers.io())
              .retryWhen(error -&gt; {
                  System.out.println("retryWhen");
                  return error.retry();
              }).subscribe(next -&gt; System.out.println("subscribeNext"),
                           error -&gt; System.out.println("subscribeError"));
  
  }
  </code></pre>
  
  <p>After executing this, the program prints:</p>
  
  <pre><code>retryWhen
  
  Process finished with exit code 0
  </code></pre>
  
  <p>My question, and what I don't understand is: Why is retryWhen called instantly upon subscribing to an Observable? The observable does nothing. </p>
  
  <p>What I want is retryWhen to be called when onError is called on the emitter. Am I misunderstanding how rx works?</p>
  
  <p>Thanks!</p>
  
  <p>Adding new snippet:</p>
  
  <pre><code>public static void main(String[] args) throws InterruptedException {
  
      final Observable&lt;Object&gt; observable = Observable.create(emitter -&gt; {
          emitter.onNext("next");
          emitter.onComplete();
      });
  
      final CountDownLatch latch = new CountDownLatch(1);
      observable.subscribeOn(Schedulers.io())
              .doOnError(error -&gt; System.out.println("doOnError: " + error.getMessage()))
              .retryWhen(error -&gt; {
                  System.out.println("retryWhen: " + error.toString());
                  return error.retry();
              }).subscribe(next -&gt; System.out.println("subscribeNext"),
                           error -&gt; System.out.println("subscribeError"),
                           () -&gt; latch.countDown());
  
      latch.await();
  }
  </code></pre>
  
  <p>Emitter onNext and complete is called. DoOnError is never called. Output is:</p>
  
  <p>retryWhen: io.reactivex.subjects.SerializedSubject@35fb3008
  subscribeNext</p>
  
  <p>Process finished with exit code 0</p>