question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question about interrupted threads in RxJava2

See original GitHub issue

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 #4863 also fixes the problem.

i am using RxJava 2.0.4 - thanks!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
akarnokdcommented, Jan 27, 2017

Thanks, I see the problem now: Single.zip delegates to Flowable.zip which cancels when a source completes before the others complete.

This has to be fixed on the operator level so I can’t give you any workaround other than the clearing of the interrupted flag or a custom operator that suppresses cancellation if it comes after an onsuccess.

0reactions
ahmedrecommented, Jan 30, 2017

thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to implement an interrupted thread in rxjava?
I have the following code that I want to turn Reactive: class ReadThread extends Thread { @Override public void run() { super.run(); ...
Read more >
Reactive Multi-Threading with RxJava - Pitfalls and Solutions
I recently had a rough time refactoring a multi-threaded, reactive message processor. It just didn't seem to be working the way I expected....
Read more >
Java Interrupting a Thread - javatpoint
If any thread is in sleeping or waiting state (i.e. sleep() or wait() is invoked), calling the interrupt() method on the thread, breaks...
Read more >
Get rid of Thread.sleep() from your Java code - Medium
Wherever you want to delay execution, you just put in something like this: try { Thread.sleep(1000); } catch (InterruptedException e) { }
Read more >
Dealing with Backpressure with RxJava - Baeldung
We will look at the different solutions to the problem of growing buffer ... + v); Thread.sleep(1000); } catch (InterruptedException e) { e....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found