From 146564d33c5631b07392c2e925675a3d261aab02 Mon Sep 17 00:00:00 2001 From: Leandro Martelli Date: Thu, 2 Jan 2020 11:42:43 +0000 Subject: [PATCH] Fix "conn" leaking, which eventually leads to thread exhaustion on machines with many NICs Signed-off-by: Leandro Martelli --- netboot/rtnetlink_linux.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/netboot/rtnetlink_linux.go b/netboot/rtnetlink_linux.go index 41f5b3e6..f6886f8d 100644 --- a/netboot/rtnetlink_linux.go +++ b/netboot/rtnetlink_linux.go @@ -1,6 +1,10 @@ package netboot -import "github.com/jsimonetti/rtnetlink" +import ( + "log" + + "github.com/jsimonetti/rtnetlink" +) // getOperState returns the operational state for the given interface index. func getOperState(iface int) (rtnetlink.OperationalState, error) { @@ -8,6 +12,13 @@ func getOperState(iface int) (rtnetlink.OperationalState, error) { if err != nil { return 0, err } + defer func() { + err := conn.Close() + if err != nil { + log.Printf("failed to close rtnetlink connection: %v", err) + } + }() + msg, err := conn.Link.Get(uint32(iface)) if err != nil { return 0, err