Skip to content

Commit

Permalink
Protect initializedIfnames with mutex
Browse files Browse the repository at this point in the history
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
  • Loading branch information
anatol committed Jun 1, 2022
1 parent c7e328f commit b42139c
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions init/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"os"
"strings"
"sync"
"time"

"github.com/insomniacslk/dhcp/dhcpv4"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit b42139c

Please sign in to comment.