go runtime_error ai_generated true

panic: send on closed channel (inside select statement)

ID: go/send-on-closed-channel-select

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-05-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active — — —
1.22 active — — —

Root Cause

A select case attempts to send on a channel that has been closed by another goroutine, even though select is often assumed to protect sends.

generic

中文

select 的某个 case 尝试向已被其他 goroutine 关闭的 channel 发送数据,尽管通常认为 select 能保护发送操作。

Workarounds

  1. 90% success
    select {
    case ch <- v:
    case <-done:
        return
    }
  2. 92% success
    // only the owner closes; producers send via a request channel

Dead Ends

Common approaches that don't work:

  1. 90% fail

    default does not prevent the panic when the send case is selected and the channel is closed.

  2. 85% fail

    Buffering delays but does not prevent the panic; a closed channel rejects all sends regardless of buffer.