Skip to content

[BUG]: Stream.take resource safety #1398

@johnhungerford

Description

@johnhungerford

Version

1.0-RC1+14-a287ac73+20250724-1908-SNAPSHOT

Scala Version

3.7.1

Expected Behavior

When a stream is defined using an Emit[Chunk] effect wrapped in Sync.ensure or Scope.ensure, and then the stream is run using .take the finalizer should run.

Actual Behavior

The finalizer does not run

Steps to Reproduce

Run the following tests:

"Sync.ensure" - {
    "take" in run {
        for
            ref <- AtomicRef.init(List.empty[String])
            stream = Stream:
                Sync.ensure(ref.getAndUpdate("finalized" :: _)):
                    Loop(0)(i => ref.getAndUpdate(i.toString :: _).andThen(Emit.valueWith(Chunk(i))(Loop.continue(i + 1))))
            emittedValues <- stream.take(5).run
            refValues     <- ref.get
        yield assert(emittedValues == Chunk(0, 1, 2, 3, 4) && refValues == List("finalized", "4", "3", "2", "1", "0"))
    }
}

"Scope.ensure" - {
    "take" in run {
        for
            ref <- AtomicRef.init(List.empty[String])
            stream = Stream:
                Scope.run:
                    Scope.ensure(ref.getAndUpdate("finalized" :: _)).andThen:
                        Loop(0)(i => ref.getAndUpdate(i.toString :: _).andThen(Emit.valueWith(Chunk(i))(Loop.continue(i + 1))))
            emittedValues <- stream.take(5).run
            refValues     <- ref.get
        yield assert(emittedValues == Chunk(0, 1, 2, 3, 4) && refValues == List("finalized", "4", "3", "2", "1", "0"))
    }
}

Current Workaround

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions