Skip to content

Commit 24a3e72

Browse files
authored
Merge pull request #3416 from typelevel/bugfix/3415
Fix #3415 - fromIterator looping infinitely
2 parents 8fa4a9a + b4a42da commit 24a3e72

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

core/shared/src/main/scala/fs2/Stream.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3646,9 +3646,13 @@ object Stream extends StreamLowPriority {
36463646

36473647
def getNextChunk(i: Iterator[A]): F[Option[(Chunk[A], Iterator[A])]] =
36483648
F.suspend(hint) {
3649-
i.take(chunkSize).toVector
3650-
}.map { s =>
3651-
if (s.isEmpty) None else Some((Chunk.from(s), i))
3649+
val bldr = Vector.newBuilder[A]
3650+
var cnt = 0
3651+
while (cnt < chunkSize && i.hasNext) {
3652+
bldr += i.next()
3653+
cnt += 1
3654+
}
3655+
if (cnt == 0) None else Some((Chunk.from(bldr.result()), i))
36523656
}
36533657

36543658
Stream.unfoldChunkEval(iterator)(getNextChunk)

core/shared/src/test/scala/fs2/StreamCombinatorsSuite.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -704,16 +704,18 @@ class StreamCombinatorsSuite extends Fs2Suite {
704704
}
705705

706706
test("fromIterator") {
707-
forAllF { (x: List[Int], cs: Int) =>
707+
// Note: important to use Vector here and not List in order to prevent https://github.com/typelevel/fs2/issues/3415
708+
forAllF { (x: Vector[Int], cs: Int) =>
708709
val chunkSize = (cs % 4096).abs + 1
709-
Stream.fromIterator[IO](x.iterator, chunkSize).assertEmits(x)
710+
Stream.fromIterator[IO](x.iterator, chunkSize).assertEmits(x.toList)
710711
}
711712
}
712713

713714
test("fromBlockingIterator") {
714-
forAllF { (x: List[Int], cs: Int) =>
715+
// Note: important to use Vector here and not List in order to prevent https://github.com/typelevel/fs2/issues/3415
716+
forAllF { (x: Vector[Int], cs: Int) =>
715717
val chunkSize = (cs % 4096).abs + 1
716-
Stream.fromBlockingIterator[IO](x.iterator, chunkSize).assertEmits(x)
718+
Stream.fromBlockingIterator[IO](x.iterator, chunkSize).assertEmits(x.toList)
717719
}
718720
}
719721

0 commit comments

Comments
 (0)