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

[AsyncChannel] Question: is task cancellation in sending operations too violent?

Open twittemb opened this issue 3 years ago • 2 comments

Hi @phausler

I was wondering if the task cancellation when sending an element in an AsyncChannel was not a bit too violent ?

This is the code when sending an element:

func _send(_ element: Element) async {
  await withTaskCancellationHandler {
    terminateAll()
  } operation: { ... }

the terminateAll() function will resume all the suspended operations (producers and consumers). What if the sending operations are performed in several Tasks? it means the async channel is finished for every one of them, which seem a bit violent right ?

twittemb avatar Jul 22 '22 09:07 twittemb

I guess in case of a sending cancellation, we should only make sure the sending continuation is resumed immediately, and the operation is removed from the pending list (and only that operation). If we are in the case where there is an awaiting that is being resumed by the sending then we should also check for Task.isCancelled right before resuming the awaiting and not do it if there is a cancellation ?

what do you think ?

twittemb avatar Jul 23 '22 08:07 twittemb

https://github.com/apple/swift-async-algorithms/pull/184

twittemb avatar Jul 28 '22 17:07 twittemb

PR is merged

twittemb avatar Sep 16 '22 05:09 twittemb