async-http-client: `HTTPClient.execute(...) async throws` API violates Structured Concurrency
Structured Concurrency mandates that upon the return of a function any background work it may have started has completed.
With HTTPClient.execute(...) async throws that’s clearly not the case as it returns a half-finished HTTPClientResponse where the response.body yet has to arrive.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 1
- Comments: 17 (9 by maintainers)
IMO anything that we currently provide in a with style scope makes sense to adopt
~Escapableto get exactly those guarantees enforced by the compiler that we currently only enforce by API shape.~Escapablewill still allow to pass the types into structured child tasks just not outside of the current task. Of course this is mostly hypothetical at this point.I agree that we can transfer ownership for the period of a request for performance reasons; however, lateral communication will always exist in any system. Different tasks have to communicate with each other in larger applications. Doesn’t mean we have to use for a connection pool but I still expect it to be a common pattern between tasks and not violate structured concurrency.
Not quite. Structured Concurrency doesn’t mean you get to pick the structure, the structure of your code (the
{and}s).Quotes from
Wikipedia on Structured Concurrency
SE-0304:
So the one thing that mustn’t happen is that you pass your scope’s
}and still have stuff running. Very clearly, that’s exactly whatexecuteis currently doing.It may have a structure but it’s not the structure that Structured Programming and Structured Concurrency mandate.
All the bad stuff that’s supposed to be impossible with Structured Concurrency can happen with
execute.For example:
Task { ... }yourself)For example if you do
And yes, my program is stupid and wrong. But the thing about structured concurrency is that this shouldn’t matter. The compelling thing is that I’m meant to get a ‘guarantee’ that when I returned I will have cleaned up (or still waiting for it),
HTTPClient.executebreaks this.If you will, you can liken this to structured programming. If you write
then you know that it’ll print
helloonce and thenbye. You don’t need to read some docs to see if maybe the loop continues after the function returned.And this is despite the fact that the loop condition (
true) never goesfalseand despite the fact that I neverbreakthe loop. The loop still stops. Structured Concurrency is meant to work the same way.The loop continuing after you hit the enclosing
}is just not possible because in Structured Programming the control flow follows the structure of your code (the{and}s).Yes, I brought this up as well. The async APIs are not structured concurrency “conform”. We spawn an unstructured Task inside them and that task is “driving” the request. This leads to exactly this problem. We need to provide a
HTTPClient.execute(request: body:)scoped method which we can have overloads for that just accumulate the whole body into a single buffer (with size limits for security)