net.ErrClosed
GoERRORNotableNetwork
use of closed network connection
Quick Answer
Use errors.Is(err, net.ErrClosed) to detect closed-connection errors in server accept loops.
What this means
Returned when a network operation is attempted on a connection that has already been closed. Often surfaces as a wrapped error string.
Why it happens
- 1Listener or connection closed while a goroutine is still blocking on it
- 2Graceful shutdown closing the listener before accept loop exits
Fix
Check in accept loop
Check in accept loop
if errors.Is(err, net.ErrClosed) {
return // listener intentionally closed
}
log.Println("accept error:", err)Why this works
Returning on net.ErrClosed allows clean shutdown without spurious error logs.
Code examples
Detect in accept loopgo
conn, err := ln.Accept()
if errors.Is(err, net.ErrClosed) {
return
}Close listener on quitgo
go func() {
<-quit
ln.Close()
}()Wrap checkgo
if err != nil && !errors.Is(err, net.ErrClosed) {
log.Fatal(err)
}Same error in other languages
Sources
Official documentation ↗
Go standard library
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev