Skip to content

AsyncStream & AsyncThrowingStream behavioral divergences & misleading documentation #76547

Open
@jamieQ

Description

@jamieQ

Description

issues:

  1. AsyncStream supports multiple concurrent consumption, but its documentation states that it does not
  2. AsyncThrowingStream does not support multiple concurrent consumption (its documentation is accurate)

Reproduction

// non-throwing stream
func test_stream() async {
  let (s, c) = AsyncStream<Int>.makeStream()

  let t1 = Task { @MainActor in
    for await _ in s {}
  }
  await MainActor.run {}
  print("installed consumer 1")

  let t2 = Task { @MainActor in
    for await _ in s {}
  }
  await MainActor.run {}
  print("installed consumer 2")
  // works
}

// throwing stream
func test_throwing_stream() async {
  let (s, c) = AsyncThrowingStream<Int, Error>.makeStream()

  let t1 = Task { @MainActor in
    for try await _ in s {}
  }
  await MainActor.run {}
  print("installed consumer 1")

  let t2 = Task { @MainActor in
    for try await _ in s {}
  }
  await MainActor.run {}
  print("installed consumer 2")
  // fatal error shortly after this point
}

Task { await test_stream() }
Task { await test_throwing_stream() }

Expected behavior

the async stream implementations should function analogously with respect to multi-consumption, and the corresponding documentation should accurately reflect their behaviors. in particular, the throwing variant should behave in the same manner that the non-throwing stream does.

Environment

Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0

Additional information

relevant forum posts:

  1. https://forums.swift.org/t/consuming-an-asyncstream-from-multiple-tasks-redux/69818
  2. https://forums.swift.org/t/question-how-to-re-introduce-concurrent-iteration-runtime-error-for-asyncstream/71831

Metadata

Metadata

Assignees

No one assigned

    Labels

    ConcurrencуArea → standard library: The `Concurrency` module under the standard library umbrellabugA deviation from expected or documented behavior. Also: expected but undesirable behavior.concurrencyFeature: umbrella label for concurrency language featuresstandard libraryArea: Standard library umbrella

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions