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

Concurrent iteration of AsyncStream can 'leak' continuations #71412

Open
jamieQ opened this issue Feb 6, 2024 · 0 comments
Open

Concurrent iteration of AsyncStream can 'leak' continuations #71412

jamieQ opened this issue Feb 6, 2024 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@jamieQ
Copy link

jamieQ commented Feb 6, 2024

Description

per its documention, AsyncStream nominally does not support concurrent iteration. in practice, however, this functionality exists and can be used. if one creates multiple iterators over a single underlying AsyncStream and uses them from different Tasks, then termination of the stream may cause some number of the concurrent iterators to remain suspended indefinitely.

Reproduction

func test_multiConsume() async {
    let (stream, continuation) = AsyncStream<Int>.makeStream()

    await withTaskGroup(of: Void.self) { group in
        for i in 1...10 { // fewer consumers will often also result in the same issue
            group.addTask {
                print("starting iterator \(i)")
                for await value in stream {
                    print("iterator \(i): \(value)")
                }
                print("iterator \(i) finished")
            }
        }

        group.addTask {
            print("terminating stream")
            continuation.finish()
        }
    }
    print("done") // generally never reached
}

Expected behavior

AsyncStream should not allow concurrent iteration. attempting to do so should be either a compiler or runtime error, as it (sometimes) is with AsyncThrowingStream.

Environment

swift-driver version: 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
Target: arm64-apple-macosx13.0

Additional information

related forums discussion
related existing bug report

@jamieQ jamieQ added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

1 participant