A goroutine sends its result on an unbuffered channel that the function may stop waiting for, and then blocks forever
What it finds
``go ch := make(chan result) go func() { ch <- fetch() }() select { case r := <-ch: return r, nil case <-ctx.Done(): return result{}, ctx.Err() } ` When the other case wins, nobody receives from ch` any more, and since it has no buffer the goroutine blocks on its send forever, with everything it holds. A buffer of one lets the send complete either way.
The channel must be a local variable made in the function by make of a channel type without a size, or with the literal size 0. A function literal that the function starts with go must send on it, outside of loops and of select statements, and the function itself must receive from it only in one case of a select that has another case or a default and is not in a loop. The channel must not be used in any other way, so no one else can be receiving.
From the rule's source, goroutine_stuck_sending.rs.
package p import "context" func fetch() int { return 1 } func f(ctx context.Context) (int, error) { ch := make(chan int) go func() { ch <- fetch()tx0 reports: this send blocks forever if the select stops waiting for ch }() select { case v := <-ch: return v, nil case <-ctx.Done(): return 0, ctx.Err() }} func buffered(ctx context.Context) (int, error) { ch := make(chan int, 1) go func() { ch <- fetch() }() select { case v := <-ch: return v, nil case <-ctx.Done(): return 0, ctx.Err() }} func waits() int { ch := make(chan int) go func() { ch <- fetch() }() return <-ch} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/go:goroutine-stuck-sending" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/go:goroutine-stuck-sending] reason="..."tx0 explain tx0/go:goroutine-stuck-sending shows the same in the terminal. Found a false positive? Report it.