On Wed, Sep 20, 2023 at 5:04 PM Jerry Londergaard
<
jlonde...@gmail.com> wrote:
>
> Are you comfortable though, *relying* on this behaviour down the stack to enforce the timeout that was declared
> further up the stack (leaving potentially leaked goroutines out of it...)?
That's the nature of handling requests using this layered approach. As
you move down layers in a concurrent program, those that are at a
higher layer set the context for the lower layers. Those that are
lower the stack are not enforcing timeouts or cancellations, they are
merely getting a notification of it.
>
>> If the new goroutine cannot check context cancellation, then the
>> goroutine starting that new goroutine can deal with cancellation like
>> you did, hoping that the new goroutine eventually ends.
>
>
> Ok, I think you're referring to this example of mine
https://go.dev/play/p/EGNlJqo3hY5
>
>> I usually use
>> the former, but the latter has its uses if you are calling a
>> third-party library or some operation you cannot cancel.
>
>
> This is close to the heart of the question. My example of that third-party library is the database one,
> and it seems to say (based on its suggested usage) "You shouldn't need to handle your context timeout, we'll make sure we do".
> Which, maybe works fine in that specific case, but in general feels a bit awkward. It seems you would agree?
I am a bit confused about what you are asking here. I find it awkward
and risky if I cannot pass down a cancelable context down the stack
where things may not return. But then, that's the only way to deal
with such third-party code. If goroutines leak, they leak, and
eventually you run out of memory, crash, and restart. Sometimes
failing and recovering is cheaper than fixing.