From b42139cd6f87b4016e605d08cf8513c3e0653e67 Mon Sep 17 00:00:00 2001 From: Anatol Pomozov Date: Tue, 31 May 2022 21:58:43 -0700 Subject: [PATCH] Protect initializedIfnames with mutex With booster 0.8 release network udev events are handled concurrently. initializedIfnames slice might be modified by different threads. To prevent corruption we need to proted the access with mutex. Issue #155 --- init/network.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/init/network.go b/init/network.go index d4a98b72..ced409b3 100644 --- a/init/network.go +++ b/init/network.go @@ -6,6 +6,7 @@ import ( "net" "os" "strings" + "sync" "time" "github.com/insomniacslk/dhcp/dhcpv4" @@ -69,7 +70,10 @@ func runDhcp(ifname string) error { } func shutdownNetwork() { - for _, ifname := range initializedIfnames { + initializedIfnames.Lock() + defer initializedIfnames.Unlock() + + for _, ifname := range initializedIfnames.v { debug("shutting down network interface %s", ifname) link, err := netlink.LinkByName(ifname) if err != nil { @@ -90,7 +94,10 @@ func shutdownNetwork() { } } -var initializedIfnames []string +var initializedIfnames = struct { + v []string + sync.Mutex +}{} func initializeNetworkInterface(ifname string) error { debug("%s: start initializing network interface", ifname) @@ -109,7 +116,9 @@ func initializeNetworkInterface(ifname string) error { if err := netlink.LinkSetUp(link); err != nil { return err } - initializedIfnames = append(initializedIfnames, ifname) + initializedIfnames.Lock() + initializedIfnames.v = append(initializedIfnames.v, ifname) + initializedIfnames.Unlock() timeout := time.After(20 * time.Second) debug("%s waiting interface to be UP", ifname)