Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix writeUtf8Lines evaluates leading effects in the input stream twice #3489

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions io/shared/src/main/scala/fs2/io/file/Files.scala
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,13 @@ sealed trait Files[F[_]] extends FilesPlatform[F] {
def writeUtf8Lines(path: Path, flags: Flags): Pipe[F, String, Nothing] = in =>
in.pull.uncons
.flatMap {
case Some(_) =>
in.intersperse(lineSeparator).append(Stream[F, String](lineSeparator)).underlying
case Some((next, rest)) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just remove the in.pull.uncons.flatMap entirely?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we keep the “don't add lineSeparator if the stream is empty” specification, I don't think we can remove in.pull.uncons.flatMap since we cannot determine if the stream is empty until we evaluate the first element.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, ok... can we test a bit with scopes? I worry, perhaps unnecessarily, that the proposed fix might mess with resource finalization -- i.e. some resources might get finalized early.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like any resource that was to be released at the end of the input stream also gets released at the end of the unconsed-and-flatMaped stream.

I was chatting with @kory33 about this potential corner case. His opinion was that uncons is not specified enough, and that we may want to have additional test-cases on uncons to ensure such early finalizations do not occur.

Code

val program = Stream
  .bracketWeak(IO { println("Resource 1 acquired"); "1" })(_ => IO.println("Resource 1 released"))
  .append(
    Stream
      .bracketWeak(IO { println("Resource 2 acquired"); "2" })(_ => IO.println("Resource 2 released"))
      .append(Stream.emit("3"))
      .append(Stream.exec(IO.println(s"test 1")))
      .scope
  )
  .append(Stream.exec(IO.println(s"test 2")))
  .through(Files.forIO.writeUtf8Lines(Path("test.txt")))
  .compile
  .drain
program.unsafeRunSync()

Output

Resource 1 acquired
Resource 2 acquired
test 1
Resource 2 released
test 2
Resource 1 released

Stream
.chunk(next)
.append(rest)
.intersperse(lineSeparator)
.append(Stream[F, String](lineSeparator))
.underlying
case None => Pull.done
}
.stream
Expand Down
19 changes: 18 additions & 1 deletion io/shared/src/test/scala/fs2/io/file/FilesSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package fs2
package io
package file

import cats.effect.{IO, Resource}
import cats.effect.{IO, Resource, Ref}
import cats.kernel.Order
import cats.syntax.all._

Expand Down Expand Up @@ -170,6 +170,23 @@ class FilesSuite extends Fs2IoSuite with BaseFileSuite {
|""".stripMargin)
}

test("writeUtf8Lines - side effect") {
Stream
.resource(tempFile)
.flatMap { path =>
Stream.eval(Ref[IO].of(0)).flatMap { counter =>
Stream
.eval(counter.update(_ + 1).as(""))
.append(Stream.eval(counter.update(_ + 1).as("")))
.through(Files[IO].writeUtf8Lines(path)) ++
Stream.eval(counter.get)
}
}
.compile
.foldMonoid
.assertEquals(2)
}

test("writeUtf8Lines - empty stream") {
Stream
.resource(tempFile)
Expand Down