RxJava icon indicating copy to clipboard operation
RxJava copied to clipboard

Need Scheduler Shutdown

Open blesa opened this issue 12 years ago • 26 comments

Use of Schedulers.io() or Schedulers.computation() creates several executors (thread pools) which are never shutted down. Threads stay in memory until the whole JVM finishes. This causes memory leak warnings on Tomcat aplication server (while reloading web app). Maybe it also violates section 4.3 of Rx Design Guidelines (not sure about that). There should be a method to gracefully terminate all schedulers and their executors (thread pools). I'd propose something like Schedulers.reset() (or .shutdown or .shutdownNow). All default schedulers are now created in Schedulers class constructor and referenced from its final member variables. I think it should be changed and sequence like this should be possible: Schedulers.io(); // singleton referencing schedulers is created, threads are running Schedulers.reset(); // all executors are shutted down, threads are gone ... Schedulers.io(); // a fresh new set of executors is initialized

blesa avatar Oct 07 '14 13:10 blesa

A reset of some kind would be okay.

This never comes up in the Netflix environment as we never reload containers.

This is not related to design guideline 4.3. Each Observable is cleaning up after itself (shutting down scheduled work it used, etc). Threadpools are system level, otherwise new threads would be created on every subscription which would be far worse.

benjchristensen avatar Oct 07 '14 16:10 benjchristensen

Hm I wonder, aren't those scheduler threads daemonized anyway? So they should go down silently once the app is reloaded?

daschl avatar Oct 07 '14 21:10 daschl

They are daemon threads, but the pools themselves would not clean themselves up.

https://github.com/ReactiveX/RxJava/blob/1.x/src/main/java/rx/internal/util/RxThreadFactory.java#L34

benjchristensen avatar Oct 08 '14 00:10 benjchristensen

This relates to #1624 inasmuch as the Schedulers.shutdown suggestion above would enable proper isolation of unit tests of asynchronous functionality.

davidmoten avatar Oct 08 '14 06:10 davidmoten

Does Schedulers.reset() work? Or is it awkward to exist on the factory class where all other methods are for creating Schedulers? If not there where should it live?

benjchristensen avatar Oct 14 '14 03:10 benjchristensen

I prefer reset() over shutdown() as reset() implies it can be reused whereas shutdown() suggests it can't.

benjchristensen avatar Oct 14 '14 03:10 benjchristensen

I think reset() is better. I found a few days ago, that Hystrix has Hystrix.reset() method (https://netflix.github.io/Hystrix/javadoc/com/netflix/hystrix/Hystrix.html#reset()), but there is not RxJava (Rx) class in this project. Rx.reset() would look better then Schedulers.reset() but is this good enough reason to introduce a new class for "lifecycle management of Rx".

blesa avatar Oct 14 '14 10:10 blesa

@headinthebox What do you prefer?

rx.schedulers.Schedulers.reset();
rx.RxJava.reset();
rx.Rx.reset();

benjchristensen avatar Oct 15 '14 21:10 benjchristensen

(a)

headinthebox avatar Oct 15 '14 21:10 headinthebox

There is a 3 months old proposal to solve the issue. Is it still relevant? @benjchristensen have you considered the latest update to the PR to be adequate?

akarnokd avatar Jan 20 '15 11:01 akarnokd

This also causes a conflict with tools like https://github.com/carrotsearch/randomizedtesting which do automatic thread leak detection at test time. Because I have no way of closing down the RxComputationThreadPool-* threads at the end of a test suite, randomizedtesting throws an exception.

andrewclegg avatar May 19 '15 09:05 andrewclegg

hi everyone,

reviving this discussion since the issue came up with one of our users. Said user deploys our SDK in a JBoss instance and does hot redeploys (vs restarting the container). This leads to OOM because the RxJava threads (in this case from Schedulers.computation()) are never cleared, and they retain a reference to the whole application code via their contextClassLoader.

so :up: for this feature :)

continuing discussion, should we also expose a public void shutdown() method on the schedulers themselves / the Scheduler interface? or rather attempt to make it package protected so that only Schedulers.reset() can call its Schedulers shutdown method?

simonbasle avatar May 27 '15 07:05 simonbasle

I have container based deployments too and am keen to see this PR succeed also.

davidmoten avatar May 27 '15 08:05 davidmoten

ping @akarnokd @benjchristensen, has this been reviewed since last PR? could this be revived as is or does it need work?

simonbasle avatar Jun 09 '15 16:06 simonbasle

The related PR is quite old and needs reapplication. I don't really have time for this now and decisions regarding RxJava have been few and far between lately.

akarnokd avatar Jun 09 '15 16:06 akarnokd

Why don't we let Schedulers be Subscriptions?

headinthebox avatar Jun 09 '15 16:06 headinthebox

@headinthebox that's an interesting idea, reusing an interface that is already close in semantics to Closeable in java and IDisposable in ReactiveExtensions, but still specific to RxJava.

if I go that way, how do you think the backward compatibility should be maintained? do a default implementation in the Scheduler abstract class?

simonbasle avatar Jun 10 '15 14:06 simonbasle

Why don't we let Schedulers be Subscriptions?

Because it is only half of the solution. It doesn't really matter what the method will be called that shuts down the NewThreadWorkers.

There are a few things to consider:

  • Shutting down schedulers is a terminal state and since Schedulers references them from final fields, there is no way to resurrect them.
  • If one needs resurrection, the entire shutdown mechanism should be implemented on Schedulers and somehow not expose the actual shutdown method on the Scheduler class. Plus, one has to switch to volatile references to the standard schedulers so they can be replaced at will, but @benjchristensen expressed concerns regarding the added volatile reads of those scheduler instances putting overhead on Observable sequence creation.
  • computation() scheduler is trivial to shutdown due to the known fixed set of workers, trampoline(), test() doesn't really matter, newThread() shuts down itself 'automatically' after (proper) use. The remaining trouble is with io() because one needs to track each worker created. This requires some concurrent datastructure and the createWorker()'s overhead will increase significantly.

See #1769 for my original proposal.

akarnokd avatar Jun 10 '15 14:06 akarnokd

@akarnokd @benjchristensen @headinthebox I'd like to bump this again. The inability to adequately unit test concurrent code in an isolated way is a bug, not a potential enhancement.

I can work around this by factoring my all my tests to their own suites but I think the easier and better solution is to improve RxJava. If I reapply your PR, will you be able to take a look and iterate with me if necessary?

MatthewDailey avatar Jun 16 '15 16:06 MatthewDailey

@MatthewDailey I think it's faster if I'm doing it, but it could take weeks until it gets approved and more weeks until it gets released.

akarnokd avatar Jun 16 '15 20:06 akarnokd