i have some code that is relying on unstructured tasks to send the right task cancellation signals into some async-colored code.
try await stream.frames.executeThenClose
{
(
inbound:NIOAsyncChannelInboundStream<HTTP2Frame.FramePayload>,
outbound:NIOAsyncChannelOutboundWriter<HTTP2Frame.FramePayload>
) in
let request:Task<ServerResponse, any Error> = .init
{
try await self.respond(to: inbound)
}
stream.frames.channel.closeFuture.whenComplete
{
_ in request.cancel()
}
let message:ServerMessage
do
{
message = .init(response: try await request.value)
}
catch let error
{
message = .init(redacting: error)
}
try await outbound.send(message)
}
but since this is relying on unstructured tasks i wondered if there was a better way to achieve this with structured concurrency.
lukasa
(Cory Benfield)
2
Annoyingly we're missing some APIs here that would make this more tractable. In particular, I think we want a nice structured cancelOnClose API, or some way to get us to deliver you a message. Can you file a github issue for this?
Given this was something I was just looking into the other week and hit some deadends. I added the issue Add Channel.cancelOnClose(_:) · Issue #2696 · apple/swift-nio · GitHub