`NIOAsyncChannel` can hit an assertion failure in `NIOAsyncWriter` if the listening channel closes while activating an accepted connection
If the listening channel is shutting down then accepted connections which haven't yet been referenced by the caller will be dropped. If the as-yet-unreferenced accepted connections have been activated (channelActive) has fired the channel will drop its ref to the outbound writer. As the ref count drops to zero we'll the assertion failure in NIOAsyncWriter.
Thanks for opening this. The problem is similar to what we see in H2 when the consuming task of the inbound streams sequence gets cancelled. Currently both async sequences in question are dropping all buffered elements on the ground on cancellation. Now in both cases the element type can by a NIOAsyncSequence producer and that will lead to us hitting the precondition of the writer.
The fundamental issue that we are having here is that those async sequence are our place where we hand off the element from the EL to Swift Concurrency. Two solutions that come to mind:
- We don't drop the buffer on cancellation but just tell whatever produces the elements to stop and continue to hand the elements out
- We find a way to gracefully drop all the elements on the ground. Now the complicated part here is that the element type is generic and a user could return anything from the channel initialisers.