Skip to content

Calling debounce is causing async stream to get cancelled early #410

@zkline101

Description

@zkline101

I made a post about this here.

I was recently testing out the debounce method and I noticed some strange behavior that I didn't expect. It looks the async stream I have set up is getting cancelled early when I'm calling the debounce method but it doesn't do that without the debounce method. This seems like a bug but I'm curious to hear others thoughts on it.

Here is the reproducer I have. Thanks!

@Test
func debounceCancellation() async {
    enum Status {
        case inProgress
        case completed
    }

    let drawStatues = AsyncStream<Status> { continuation in
        Task {
            continuation.yield(.inProgress)
            try! await Task.sleep(for: .seconds(2))
            continuation.yield(.completed)
        }

        continuation.onTermination = { termination in
            switch termination {
            case .cancelled:
                print("Cancelled!")
            case .finished:
                print("Finished!")
            @unknown default:
                fatalError("Unknown case")
            }
        }
    }

    _ = await drawStatues
        .debounce(for: .seconds(2))
        .first(where: { @Sendable in $0 == .completed })

    // Current behavior when using debounce:
    // cancelled -> end
    //
    // Expected behavior without using debounce:
    // end -> cancelled

    print("End")
}

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