A channel is sent to or closed again after it was closed

tx0/go:use-after-closedefaultwarningbug, concurrency

What it finds

close(ch) followed by ch <- v or another close(ch): both panic on a closed channel. Usually the close came too early, or two paths of a function both close.

The later statement must be in the same block as the close, after it, with no statement in between that assigns the channel variable or contains a jump, a label or a select. The channel must be a variable, and close the builtin.

From the rule's source, use_after_close.rs.

Example from the rule's tests
package p func f(ch chan int, done chan struct{}) {	close(ch)	ch <- 1tx0 reports: ch is already closed, so sending to it panics	close(done)	println("closed")	close(done)tx0 reports: done is already closed, so closing it again panics} func g(ch chan int) {	close(ch)	ch = make(chan int)	ch <- 1} func h(chs []chan int) {	for _, ch := range chs {		close(ch)	}}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/go:use-after-close" = "off"

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:use-after-close] reason="..."

tx0 explain tx0/go:use-after-close shows the same in the terminal. Found a false positive? Report it.