From 742114db49daac3db1f21c91401f4dbc466d179a Mon Sep 17 00:00:00 2001 From: Richard Kosegi Date: Mon, 29 Aug 2022 10:44:07 +0200 Subject: [PATCH] Check for error before deferring to close resource In case of error, returned resource is always nil. Deference it only when call was successful Closes #113 --- utils/utils.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/utils.go b/utils/utils.go index 3796729..630835a 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -277,10 +277,10 @@ func UDPRoutine(name string, decodeFunc decoder.DecoderFunc, workers int, addr s if sockReuse { pconn, err := reuseport.ListenPacket("udp", addrUDP.String()) - defer pconn.Close() if err != nil { return err } + defer pconn.Close() var ok bool udpconn, ok = pconn.(*net.UDPConn) if !ok { @@ -288,10 +288,10 @@ func UDPRoutine(name string, decodeFunc decoder.DecoderFunc, workers int, addr s } } else { udpconn, err = net.ListenUDP("udp", &addrUDP) - defer udpconn.Close() if err != nil { return err } + defer udpconn.Close() } payload := make([]byte, 9000)