The Hidden Rule That Skips KTN-GOROUTINE-CTXCANCEL
Go’s KTN-GOROUTINE-CTXCANCEL rule blocks defer cancel() in most context setups - but a classic exemption exists. When a constructor embeds cancel in a struct field, cleanup happens elsewhere, making defer cancel() both unnecessary and dangerous. Consider NewWorker(): storing cancel in a struct field shifts responsibility to Stop(), where cancel() is called intentionally. Putting defer cancel() there breaks the flow - Go already fires it on return. The real danger? Mistakenly assigning cancel to a struct literal or field, triggering false positives. Test suites now catch these patterns with precise cases: struct literals, field assignments, and named returns. This isn’t just code hygiene - it’s preventing wasted memory and race conditions. When building resilient Go apps, know: ownership transfer exempts, but never deferCancel.