The connection reset by peer
is a TCP/IP error that happens when the opposite finish (peer) has unexpectedly closed the connection. It occurs while you ship a packet out of your finish, however the different finish crashes and forcibly closes the reference to the RST
packet as an alternative of the TCP FIN
, which is used to shut a connection below regular circumstances. In Go, you’ll be able to detect the connection reset by peer
by checking if the error returned by the peer is the same as syscall.ECONNRESET
.
Reproduce the connection reset by peer
error
We will reproduce the error by making a server and shopper that do the next:
- the server reads a single byte after which closes the connection
- the shopper sends a couple of byte
If the server closes the reference to the remaining bytes within the socket’s obtain buffer, then an RST
packet is distributed to the shopper. When the shopper tries to learn from such a closed connection, it can get the connection reset by peer
error.
See the next instance, which simulates this conduct.
bundle foremost
import (
"errors"
"log"
"web"
"os"
"syscall"
"time"
)
func server() {
listener, err := web.Pay attention("tcp", ":8080")
if err != nil {
log.Deadly(err)
}
defer listener.Shut()
conn, err := listener.Settle for()
if err != nil {
log.Deadly("server", err)
os.Exit(1)
}
knowledge := make([]byte, 1)
if _, err := conn.Learn(knowledge); err != nil {
log.Deadly("server", err)
}
conn.Shut()
}
func shopper() {
conn, err := web.Dial("tcp", "localhost:8080")
if err != nil {
log.Deadly("shopper", err)
}
if _, err := conn.Write([]byte("ab")); err != nil {
log.Printf("shopper: %v", err)
}
time.Sleep(1 * time.Second) // look forward to shut on the server aspect
knowledge := make([]byte, 1)
if _, err := conn.Learn(knowledge); err != nil {
log.Printf("shopper: %v", err)
if errors.Is(err, syscall.ECONNRESET) {
log.Print("That is connection reset by peer error")
}
}
}
func foremost() {
go server()
time.Sleep(3 * time.Second) // look forward to server to run
shopper()
}
Output:
2021/10/20 19:01:58 shopper: learn tcp [::1]:59897->[::1]:8080: learn: connection reset by peer
2021/10/20 19:01:58 That is connection reset by peer error
Deal with the connection reset by peer
error
Sometimes, you’ll be able to see the connection reset by peer
error in response to a request being despatched from the shopper to the server. It implies that one thing unhealthy has occurred to the server: it has rebooted, this system has crashed, or different issues have occurred that trigger the connection to be forcibly closed. Since TCP connections will be damaged, there is no such thing as a must deal with the connection reset by peer
in any particular means on the shopper aspect. You possibly can log the error, ignore it or retry the connection when it happens. Within the instance above, we detect the error utilizing the errors.Is()
perform by checking if the returned error is an occasion of syscall.ECONNRESET
.
Distinction between connection reset by peer
and damaged pipe
Each connection reset by peer
and damaged pipe
errors happen when a peer (the opposite finish) unexpectedly closes the underlying connection. Nevertheless, there’s a delicate distinction between them. Normally, you get the connection reset by peer
while you learn from the connection after the server sends the RST
packet, and while you write to the connection after the RST
as an alternative, you get the damaged pipe
error.
Verify find out how to deal with the
damaged pipe
error in Go put up, the place will discover one other instance of producing anRST
packet and thedamaged pipe
error.
Exchange the shopper()
perform within the instance above with the next code to breed the damaged pipe
error.
func shopper() {
conn, err := web.Dial("tcp", "localhost:8080")
if err != nil {
log.Deadly("shopper", err)
}
if _, err := conn.Write([]byte("ab")); err != nil {
log.Printf("shopper: %v", err)
}
time.Sleep(1 * time.Second) // look forward to shut on the server aspect
if _, err := conn.Write([]byte("b")); err != nil {
log.Printf("shopper: %v", err)
}
}
With the brand new shopper, you will notice the output:
2021/10/20 19:55:40 shopper: write tcp [::1]:60399->[::1]:8080: write: damaged pipe
Notice that these easy examples don’t cowl all instances the place connection reset by peer
and damaged pipe
could happen. There are way more conditions the place you’ll be able to see these errors, and what error you see in what scenario requires a deep understanding of the TCP design.