Need Scheduler Shutdown
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
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.
Hm I wonder, aren't those scheduler threads daemonized anyway? So they should go down silently once the app is reloaded?
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
This relates to #1624 inasmuch as the Schedulers.shutdown suggestion above would enable proper isolation of unit tests of asynchronous functionality.
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?
I prefer reset() over shutdown() as reset() implies it can be reused whereas shutdown() suggests it can't.
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".
@headinthebox What do you prefer?
rx.schedulers.Schedulers.reset();
rx.RxJava.reset();
rx.Rx.reset();
(a)
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?
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.
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?
I have container based deployments too and am keen to see this PR succeed also.
ping @akarnokd @benjchristensen, has this been reviewed since last PR? could this be revived as is or does it need work?
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.
Why don't we let Schedulers be Subscriptions?
@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?
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
Schedulersreferences them from final fields, there is no way to resurrect them. - If one needs resurrection, the entire shutdown mechanism should be implemented on
Schedulersand somehow not expose the actual shutdown method on theSchedulerclass. Plus, one has to switch tovolatilereferences to the standard schedulers so they can be replaced at will, but @benjchristensen expressed concerns regarding the addedvolatilereads 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 withio()because one needs to track each worker created. This requires some concurrent datastructure and thecreateWorker()'s overhead will increase significantly.
See #1769 for my original proposal.
@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 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.