Skip to content

Question about interrupted threads in RxJava2 #5024

Closed
@ahmedre

Description

@ahmedre

hi,
i have a question that's similar to #4863, but a bit simpler. i am running into a strange issue that i have been able to simplify to a simple test case - what’s happening is, sometimes, by the time my most downstream method is called, the thread is interrupted (which causes issues because the code i call there bails if the thread is interrupted).

i've been able to simplify the code and repro using this:

  private Single<ArrayList<Integer>> fakeDataPiece() {
    return Single.fromCallable(() -> {
      Thread.sleep(500);
      return new ArrayList<Integer>();
    }).subscribeOn(Schedulers.io());
  }

  private Single<BookmarkData> fakeGetData() {
    return Single.zip(fakeDataPiece(), fakeDataPiece(), fakeDataPiece(),
        (integers, integers2, integers3) ->
            new BookmarkData(new ArrayList<>(), new ArrayList<>(), new ArrayList<>()))
        .subscribeOn(Schedulers.io());
  }

  public Single<Uri> exportBookmarksObservable() {
        return fakeGetData()
        .flatMap(bookmarkData -> Single.just(exportBookmarks(bookmarkData)))
        .subscribeOn(Schedulers.io());
  }

by the time exportBookmarks is called, in many cases, Thread.currentThread().isInterrupted() returns true.

my question is: why is the thread interrupted? (i am curious as to whether this is expected or if there is something i am misunderstanding or doing wrong).

observations:

  1. if i explicitly remove all the subscribeOns except for the one in exportBookmarksObservable, then i don’t see the issue (though it does change the behavior a bit in the sense that, with this, all workers are run on the same thread as opposed to each piece of work happening on a potentially different thread).
  2. if i add a doOnSuccess in fakeGetData(), i find the thread is interrupted (and is the same thread that exportBookmarks gets called on, while also being interrupted). this also is the same as one of the fakeDataPiece threads, which, at the time of its completion, is not interrupted.
  3. calling Thread.interrupted() as suggested in Get RxCachedThreadScheduler-n when calling Disposable.dispose() #4863 also fixes the problem.

i am using RxJava 2.0.4 - thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions