Skip to content

Commit

Permalink
Timeout rule removing loop and catch IPv6 unsupported error in loop (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lixmal committed Apr 3, 2024
1 parent bb0d5c5 commit 25f5f26
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions client/internal/routemanager/systemops_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ package routemanager

import (
"bufio"
"context"
"errors"
"fmt"
"net"
"net/netip"
"os"
"syscall"
"time"

"github.com/hashicorp/go-multierror"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -444,23 +446,41 @@ func removeRule(params ruleParams) error {
rule.Priority = params.priority
rule.SuppressPrefixlen = params.suppressPrefix

if err := netlink.RuleDel(rule); err != nil && !errors.Is(err, syscall.EAFNOSUPPORT) {
if err := netlink.RuleDel(rule); err != nil {
return fmt.Errorf("remove routing rule: %w", err)
}

return nil
}

func removeAllRules(params ruleParams) error {
for {
if err := removeRule(params); err != nil {
if errors.Is(err, syscall.ENOENT) {
break
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

done := make(chan error, 1)
go func() {
for {
if ctx.Err() != nil {
done <- ctx.Err()
return
}
if err := removeRule(params); err != nil {
if errors.Is(err, syscall.ENOENT) || errors.Is(err, syscall.EAFNOSUPPORT) {
done <- nil
return
}
done <- err
return
}
return err
}
}()

select {
case <-ctx.Done():
return ctx.Err()
case err := <-done:
return err
}
return nil
}

// addNextHop adds the gateway and device to the route.
Expand Down

0 comments on commit 25f5f26

Please sign in to comment.