# Is there is any way to cancel stream immidiatly

**URL:** <https://discuss.akka.io/t/is-there-is-any-way-to-cancel-stream-immidiatly/7740>\
**Category:** Akka Libraries\
**Tags:** streams\
**Created:** [January 19, 2021, 12:59pm UTC](https://discuss.akka.io/t/is-there-is-any-way-to-cancel-stream-immidiatly/7740 "2021-01-19T12:59:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dparamoshkin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/dparamoshkin/32/2053_2.png) [@dparamoshkin](https://discuss.akka.io/u/dparamoshkin)\
**Post date:** [January 19, 2021, 12:59pm UTC](https://discuss.akka.io/t/is-there-is-any-way-to-cancel-stream-immidiatly/7740/1 "2021-01-19T12:59:38Z")

</div>

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?

```auto
    IntStream intStream = IntStream.range(0, 100);
    UniqueKillSwitch run = Source.fromIterator(intStream::iterator)
        .map(item -> {
          System.out.println(item);
          Thread.sleep(100);
          return item;
        })
        .viaMat(KillSwitches.single(), Keep.right())
        .toMat(Sink.foreach(item -> {
        }), Keep.left())
        .run(_actorSystem.getMaterializer());

    Thread.sleep(1000);
    run.shutdown();

```

---

<div class="post-metadata">

**Author:** ![ignasi35](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/ignasi35/32/62_2.png) [@ignasi35](https://discuss.akka.io/u/ignasi35)\
**Post date:** [January 19, 2021, 1:57pm UTC](https://discuss.akka.io/t/is-there-is-any-way-to-cancel-stream-immidiatly/7740/2 "2021-01-19T13:57:19Z")

</div>

Hi @dparamoshkin,

try again replacing the `Thread.sleep(100)` with `throttle`:

```auto
        IntStream intStream = IntStream.range(0, 100);
        UniqueKillSwitch run = Source.fromIterator(intStream::iterator)
                .throttle(10, Duration.of(1, ChronoUnit.SECONDS))
                .map(item -> {
                    System.out.println(item);
                    return item;
                })
                .viaMat(KillSwitches.single(), Keep.right())
                .toMat(Sink.foreach(item -> {
                }), Keep.left())
                .run(_actorSystem);

        Thread.sleep(1000);
        run.shutdown();

```

;-)

---

<div class="post-metadata">

**Author:** ![johanandren](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/johanandren/32/85_2.png) [@johanandren](https://discuss.akka.io/u/johanandren)\
**Post date:** [January 19, 2021, 2:52pm UTC](https://discuss.akka.io/t/is-there-is-any-way-to-cancel-stream-immidiatly/7740/3 "2021-01-19T14:52:07Z")

</div>

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.

```auto
.via(Flow.fromFunction((Integer item) -> {
  System.out.println(item);
  Thread.sleep(100);
  return item;
}).async("akka.stream.blocking-io-dispatcher")
.withAttributes(Attributes.inputBuffer(1, 1)))

```

---

<div class="post-metadata">

**Author:** ![dparamoshkin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/dparamoshkin/32/2053_2.png) [@dparamoshkin](https://discuss.akka.io/u/dparamoshkin)\
**Post date:** [January 19, 2021, 4:07pm UTC](https://discuss.akka.io/t/is-there-is-any-way-to-cancel-stream-immidiatly/7740/4 "2021-01-19T16:07:05Z")

</div>

@ignasi35 I can’t change what inside the map - it could be long DB call, or Rest call, or anything else which is long running

---

<div class="post-metadata">

**Author:** ![dparamoshkin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/dparamoshkin/32/2053_2.png) [@dparamoshkin](https://discuss.akka.io/u/dparamoshkin)\
**Post date:** [January 19, 2021, 4:08pm UTC](https://discuss.akka.io/t/is-there-is-any-way-to-cancel-stream-immidiatly/7740/5 "2021-01-19T16:08:22Z")

</div>

@johanandren Thanks for your answer, is it effectively the same as using `mapAsync(1, ...)` ?

---

<div class="post-metadata">

**Author:** ![johanandren](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/johanandren/32/85_2.png) [@johanandren](https://discuss.akka.io/u/johanandren)\
**Post date:** [January 19, 2021, 4:32pm UTC](https://discuss.akka.io/t/is-there-is-any-way-to-cancel-stream-immidiatly/7740/6 "2021-01-19T16:32:39Z")

</div>

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.
