swift-async-algorithms icon indicating copy to clipboard operation
swift-async-algorithms copied to clipboard

`Task.select` may hang indefinitely if the input sequence is empty

Open laclouis5 opened this issue 3 years ago • 0 comments

Task.select waits for the first task of a sequence to complete and then returns this task. If the sequence is empty, then this operation waits indefinitely:

https://github.com/apple/swift-async-algorithms/blob/f44d02eb26f79d43215d0952b6e8cddc9c369baa/Sources/AsyncAlgorithms/TaskSelect.swift#L47-L63

In this withUnsafeContinuation block the for-in loop is a no-op and continuation.resume() is never called while according to the specifications it should be called exactly once. I think that for correctness there should be a precondition() checking for non-empty sequences.

I'm not sure how to do that properly since Sequence is not sized (or at least we want to avoid the O(n) count getter). Maybe requiring a RandomAccessCollection or less radical checking if TaskSelectState's tasks is not empty after the for loop?

laclouis5 avatar Jul 22 '22 20:07 laclouis5