# About NoMoreElementsNeeded exception

**URL:** <https://discuss.akka.io/t/about-nomoreelementsneeded-exception/8599>\
**Category:** Akka Libraries\
**Created:** [August 5, 2021, 7:28am UTC](https://discuss.akka.io/t/about-nomoreelementsneeded-exception/8599 "2021-08-05T07:28:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![qaemma](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/qaemma/32/2498_2.png) [@qaemma](https://discuss.akka.io/u/qaemma)\
**Post date:** [August 5, 2021, 7:28am UTC](https://discuss.akka.io/t/about-nomoreelementsneeded-exception/8599/1 "2021-08-05T07:28:27Z")

</div>

Hi, after recent akka version update, I start seeing this error `akka.stream.SubscriptionWithCancelException$NoMoreElementsNeeded$` .

Looking at [Akka 2.9.0 - akka.stream.Attributes.CancellationStrategy](https://doc.akka.io/api/akka/current/akka/stream/Attributes$$CancellationStrategy$.html), it seems to be a new behavior for 2.6.x. The default is `PropagateFailure` , which is not configurable, has this documentation:

> Strategy that treats cancelStage in different ways depending on the cause that was given to the cancellation.  
> If the cause was a regular, active cancellation (SubscriptionWithCancelException.NoMoreElementsNeeded), the stage receiving this cancellation is completed regularly.  
> If another cause was given, this is treated as an error and the behavior is the same as with failStage.

So it seems it shouldn’t raise this error to user level. What am I missing? I have tried with 2.6.10, 2.6.15 etc after seeing this [akka/akka#30071](https://github.com/akka/akka/issues/30071) but it doesn’t help in my case.

My code look like this:

```auto
      S3.download(bucket = bucket, key = key)
      .collect {
        case Some((byteStringSource, _)) => byteStringSource
      }
      .flatMapConcat(identity)
      .via(flow)
      .grouped(10000)
      .mapAsyncUnordered(parallelism = 2) { items =>
        doSomething(items)
      }
      .runWith(Sink.ignore)

```

It always runs fine on my local but fails with the above exception on production (in AWS + k8s). With the same input, it fails at different time. Sometime it fails at 90% stream completion, sometimes 99%.

Am I missing something? How should I handle this error?

---

<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:** [August 24, 2021, 9:12am UTC](https://discuss.akka.io/t/about-nomoreelementsneeded-exception/8599/2 "2021-08-24T09:12:12Z")

</div>

That stream itself shouldn’t lead to a cancellation, `Sink.ignore` consumes until it sees the end of the stream, and none of the other used operators shown cancels upstream. This should mean it is either something in your `flow` or an issue in the `S3.download` internals.

Where/how are you observing the cancellation exception, logged by internals or by your own code (a custom graph stage for example)?

---

<div class="post-metadata">

**Author:** ![thebiftek](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/thebiftek/32/2567_2.png) [@thebiftek](https://discuss.akka.io/u/thebiftek)\
**Post date:** [September 21, 2021, 10:54am UTC](https://discuss.akka.io/t/about-nomoreelementsneeded-exception/8599/3 "2021-09-21T10:54:26Z")

</div>

Hello,  
I have same issue. File I’m downloading is new line delimited JSON of about 100Mb size.

```auto
  implicit val ec: ExecutionContext = ExecutionContext.fromExecutorService(Executors.newCachedThreadPool())
  implicit val typedSystem: ActorSystem[_] = ActorSystem(Behaviors.empty[Any], "Service")

  def upsertSleep(suggestions: Seq[ByteString]): Future[Done] = {
    Future {
      Thread.sleep(1000)
    }.map(_ => Done)
      .andThen {
        case Success(res) => logger.info(s"Inserted batch $res")
        case Failure(exception) => logger.error("Could not insert batch", exception)
      }
  }

  S3.download("bucketName", "fileName")
    .collect {
      case Some((stream, metadata)) =>
        logger.info(s"Downloading file of size ${metadata.contentLength}")
        stream.log("collect")
    }
    .flatMapConcat(identity)
    .via(Framing.delimiter(ByteString("\n"), 100000, allowTruncation = true))
    .grouped(1000)
    .mapAsync[Done](1)(upsertSleep)
    .runWith(Sink.ignore)

```

Logs are:

```auto
2021-09-21 12:24:20.326 [Service-akka.actor.default-dispatcher-5] INFO c.a.c.m.a.Runner$ - Downloading file of size 113906841
...
...
...
2021-09-21 12:26:59.404 [pool-1-thread-1] INFO c.a.c.m.a.Runner$ - Inserted batch Done
2021-09-21 12:27:00.407 [pool-1-thread-1] INFO c.a.c.m.a.Runner$ - Inserted batch Done
2021-09-21 12:27:01.409 [pool-1-thread-1] INFO c.a.c.m.a.Runner$ - Inserted batch Done
2021-09-21 12:27:02.414 [pool-1-thread-1] INFO c.a.c.m.a.Runner$ - Inserted batch Done
2021-09-21 12:27:03.417 [pool-1-thread-1] INFO c.a.c.m.a.Runner$ - Inserted batch Done
2021-09-21 12:27:04.131 [Service-akka.actor.default-dispatcher-16] ERROR a.s.Materializer - [collect] Upstream failed.
akka.stream.SubscriptionWithCancelException$NoMoreElementsNeeded$: null

```

Lib versions:

```auto
val alpakkaS3Lib = "com.lightbend.akka" %% "akka-stream-alpakka-s3" % "3.0.3"
val akkaHttpLib = "com.typesafe.akka" %% "akka-http" % "10.2.6"
val akkaActorLib = "com.typesafe.akka" %% "akka-actor" % "2.6.16"
val akkaActorTypedLib = "com.typesafe.akka" %% "akka-actor-typed" % "2.6.16"
```

---

<div class="post-metadata">

**Author:** ![thebiftek](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/thebiftek/32/2567_2.png) [@thebiftek](https://discuss.akka.io/u/thebiftek)\
**Post date:** [September 29, 2021, 12:51pm UTC](https://discuss.akka.io/t/about-nomoreelementsneeded-exception/8599/4 "2021-09-29T12:51:26Z")

</div>

We have noticed that this bug is is much more noticable on fast connections (~1Gbps). On slower it almost never happens.

---

<div class="post-metadata">

**Author:** ![joroKr21](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/jorokr21/32/2594_2.png) [@joroKr21](https://discuss.akka.io/u/joroKr21)\
**Post date:** [October 13, 2021, 8:20am UTC](https://discuss.akka.io/t/about-nomoreelementsneeded-exception/8599/5 "2021-10-13T08:20:12Z")

</div>

@qaemma @thebiftek did you find a workaround?

---

<div class="post-metadata">

**Author:** ![thebiftek](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/thebiftek/32/2567_2.png) [@thebiftek](https://discuss.akka.io/u/thebiftek)\
**Post date:** [October 13, 2021, 8:45am UTC](https://discuss.akka.io/t/about-nomoreelementsneeded-exception/8599/6 "2021-10-13T08:45:13Z")

</div>

@joroKr21 Hi, yes i did. Now i download large file split to small chunks (1mb) and download those one by one. This small substream is now materialized, which “fixed” the error entirely. I tried not materializing them and retry downloading, but it failed too much. So materializing it was best option.

---

<div class="post-metadata">

**Author:** ![joroKr21](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/jorokr21/32/2594_2.png) [@joroKr21](https://discuss.akka.io/u/joroKr21)\
**Post date:** [October 13, 2021, 4:39pm UTC](https://discuss.akka.io/t/about-nomoreelementsneeded-exception/8599/7 "2021-10-13T16:39:38Z")

</div>

I found another solution. As suggested in [akka/akka-http#3201](https://github.com/akka/akka-http/issues/3201) increasing `akka.http.client.stream-cancellation-delay` to 1 second worked for me.

---

<div class="post-metadata">

**Author:** ![thebiftek](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/thebiftek/32/2567_2.png) [@thebiftek](https://discuss.akka.io/u/thebiftek)\
**Post date:** [October 17, 2021, 6:49pm UTC](https://discuss.akka.io/t/about-nomoreelementsneeded-exception/8599/8 "2021-10-17T18:49:22Z")

</div>

I tried that, but it didnt help at all no matter what delay i put in.

---

<div class="post-metadata">

**Author:** ![mdedetrich](https://avatars.discourse-cdn.com/v4/letter/m/e47c2d/32.png) [@mdedetrich](https://discuss.akka.io/u/mdedetrich)\
**Post date:** [February 9, 2022, 11:23pm UTC](https://discuss.akka.io/t/about-nomoreelementsneeded-exception/8599/10 "2022-02-09T23:23:21Z")

</div>

Getting this error and it only occurs in github actions which is likely due to it having a much faster network connection. As you said increasing it to 1 second help.

Does it make sense to increase the default `akka.http.client.stream-cancellation-delay` to 1 second since that appears to be a lot more reasonable?
