Skip to content

Commit a238f3e

Browse files
fix(networkmonitor): false engine restarts
Network monitor should only trigger engine restarts when `route get` actually changes. There are many other instances when unspecified address get added to the system without actually affecting the real default route. fixes #3352
1 parent d7d5b1b commit a238f3e

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

client/internal/networkmonitor/monitor_bsd.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"context"
77
"errors"
88
"fmt"
9+
"net/netip"
910
"syscall"
1011
"unsafe"
1112

@@ -50,7 +51,6 @@ func checkChange(ctx context.Context, nexthopv4, nexthopv6 systemops.Nexthop, ca
5051
continue
5152
}
5253
if n < unix.SizeofRtMsghdr {
53-
log.Debugf("Network monitor: read from routing socket returned less than expected: %d bytes", n)
5454
continue
5555
}
5656

@@ -65,23 +65,25 @@ func checkChange(ctx context.Context, nexthopv4, nexthopv6 systemops.Nexthop, ca
6565
continue
6666
}
6767

68+
// Gateway route was modified
6869
if route.Dst.Bits() != 0 {
6970
continue
7071
}
7172

72-
intf := "<nil>"
73-
if route.Interface != nil {
74-
intf = route.Interface.Name
73+
// Compare current with saved netxhop
74+
actualNextHopV4, errv4 := systemops.GetNextHop(netip.IPv4Unspecified())
75+
actualNextHopV6, errv6 := systemops.GetNextHop(netip.IPv6Unspecified())
76+
if errv4 != nil || errv6 != nil {
77+
err := errors.Join(errv4, errv6)
78+
log.Infof("Network monitor: failed to check next hop, assuming no network connection available: %s", err)
79+
go callback()
7580
}
76-
switch msg.Type {
77-
case unix.RTM_ADD:
78-
log.Infof("Network monitor: default route changed: via %s, interface %s", route.Gw, intf)
81+
hasV4HopChanged := nexthopv4.IP.Compare(actualNextHopV4.IP) != 0 || nexthopv4.Intf.Name != actualNextHopV4.Intf.Name
82+
hasV6HopChanged := nexthopv6.IP.Compare(actualNextHopV6.IP) != 0 || nexthopv6.Intf.Name != actualNextHopV6.Intf.Name
83+
84+
if hasV4HopChanged || hasV6HopChanged {
85+
log.Infof("Network monitor: default route changed, IPv4: %t, IPv6: %t", hasV4HopChanged, hasV6HopChanged)
7986
go callback()
80-
case unix.RTM_DELETE:
81-
if nexthopv4.Intf != nil && route.Gw.Compare(nexthopv4.IP) == 0 || nexthopv6.Intf != nil && route.Gw.Compare(nexthopv6.IP) == 0 {
82-
log.Infof("Network monitor: default route removed: via %s, interface %s", route.Gw, intf)
83-
go callback()
84-
}
8587
}
8688
}
8789
}

0 commit comments

Comments
 (0)