Skip to content

Debounce silently swallows thrown errors and stalls iteration #269

Closed
@pocketpixels

Description

@pocketpixels

I discovered while debugging an issue in my app that the async sequence returned by AsyncThrowingStream.debounce() silently swallows thrown exceptions, and does not terminate, instead just stalling iteration.

struct MyError: Error {}

let (stream, continuation) = AsyncThrowingStream.makeStream(of: Int.self)

// consume the stream
Task {
    do {
        for try await value in stream.debounce(for: .seconds(0.1)) {
            print(value)
        }
        print("Stream ended normally")
    }
    catch {
        print("Stream ended by throwing")
    }
}

// produce the values (and exception)
Task {
    for i in 1...10 {
        if i == 5 {
            continuation.finish(throwing: MyError())
        }
        else {
            continuation.yield(i)
        }
        try await Task.sleep(for: .seconds(0.3))
    }
    continuation.finish()
}

Running this in a playground (see attached project below), the iteration prints 1, 2, 3, 4 and then just stalls.
Removing the debounce, iteration ends by throwing as expected.

DebounceDebug.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions