Skip to content

Iterating combineLatest, merge, zip and debounce when Task is already cancelled #249

Closed
@swhitty

Description

@swhitty

Version 0.0.4 added a preconditionFailure() to combineLatest, merge, zip and debounce that prevents iteration if the task is already cancelled.

The following pattern reproduces the issue in each of the operators:

let t = Task {
  try? await Task.sleep(nanoseconds: 1_000_000_000)
  let c1 = AsyncChannel<Int>()
  let c2 = AsyncChannel<Int>()
  for await (v1, v2) in combineLatest(c1, c2) {
    print(v1, v2)
  }
}
t.cancel()

The failure occurs within each operators version of func cancelled():

  mutating func cancelled() -> CancelledAction? {
    switch self.state {
    case .initial:
      preconditionFailure("Internal inconsistency current state \(self.state) and received cancelled()")

Is this intentional? The workaround is a tedious manual check before any iteration...

if !Task.isCancelled {
  for await v1 in merge(c1, c2) {
    print(v1)
  }
}

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