In our application, we have an issue with using KillSwitch, even after shutdown() called we can see mapping operations (they can have blocking calls to DB) continue to execute for a long time.
I was able to reproduce the same behavior in a small code snippet. I would expect to see 10-20 outputs but I see all 100. We can’t change the implementation of functions called in the map operators.
Is there is any way to cancel stream faster?
The reason is that all operations are fused into one actor running the stream, which is blocked on your Thread.sleep (or blocking operation), the killswitch depends on being able to interact with the stream, but cannot because it is blocked. If you move the map operation to a separate dispatcher for blocking operations, an async stream island has a buffer for performance reasons, that can be tweaked with the inputBuffer attribute, however, making it less undeterministic by making the buffer small can have a negative effect on the throughput of the stream, other operators often introduce buffers anyway, which you may not have control over, so better just live with that.
Yes, what is observable will be pretty much the same.
It’s important to run that async task/CompletableFuture on a threadpool specific for blocking tasks, just like using the “blocking-io-dispatcher” for the blocking section of the stream, so that it does not starve other parts of the system.