Resi Dwi Thawasa

TIL

context.WithTimeout won't stop a blocking call

I assumed context.WithTimeout would cancel anything once the deadline passed. It does not. It only signals. Your code has to actually watch the context for it to do anything:

select {
case <-ctx.Done():
    return ctx.Err()
case res := <-work:
    return res
}

If a function just blocks (a tight loop, a library call that ignores context, a syscall that does not take one), the timeout fires, ctx.Done() closes, and the blocking call keeps right on blocking. Nothing interrupts it.

For a hard wall-clock limit on work that does not check ctx, you need a separate timer and a way to abandon or move on. The context alone will not save you there.

#go #concurrency