diff --git a/cmd/gluetun/main.go b/cmd/gluetun/main.go index 789cd620..c052c14e 100644 --- a/cmd/gluetun/main.go +++ b/cmd/gluetun/main.go @@ -123,8 +123,6 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, return fmt.Errorf("command %q is unknown", args[1]) } } - ctx, cancel := context.WithCancel(ctx) - defer cancel() const clientTimeout = 15 * time.Second httpClient := &http.Client{Timeout: clientTimeout} @@ -261,7 +259,7 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, healthy := make(chan bool) openvpnLooper := openvpn.NewLooper(allSettings.OpenVPN, nonRootUsername, puid, pgid, allServers, - ovpnConf, firewallConf, routingConf, logger, httpClient, os.OpenFile, tunnelReadyCh, healthy, cancel) + ovpnConf, firewallConf, routingConf, logger, httpClient, os.OpenFile, tunnelReadyCh, healthy) wg.Add(1) // wait for restartOpenvpn go openvpnLooper.Run(ctx, wg) diff --git a/internal/openvpn/loop.go b/internal/openvpn/loop.go index 7788561d..38e54b50 100644 --- a/internal/openvpn/loop.go +++ b/internal/openvpn/loop.go @@ -46,7 +46,6 @@ type looper struct { openFile os.OpenFileFunc tunnelReady chan<- struct{} healthy <-chan bool - cancel context.CancelFunc // Internal channels and locks loopLock sync.Mutex running chan models.LoopStatus @@ -67,7 +66,7 @@ func NewLooper(settings configuration.OpenVPN, username string, puid, pgid int, allServers models.AllServers, conf Configurator, fw firewall.Configurator, routing routing.Routing, logger logging.Logger, client *http.Client, openFile os.OpenFileFunc, - tunnelReady chan<- struct{}, healthy <-chan bool, cancel context.CancelFunc) Looper { + tunnelReady chan<- struct{}, healthy <-chan bool) Looper { return &looper{ state: state{ status: constants.Stopped, @@ -86,7 +85,6 @@ func NewLooper(settings configuration.OpenVPN, openFile: openFile, tunnelReady: tunnelReady, healthy: healthy, - cancel: cancel, start: make(chan struct{}), running: make(chan models.LoopStatus), stop: make(chan struct{}), @@ -126,10 +124,9 @@ func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup) { //nolint:gocogni if len(settings.Config) == 0 { connection, err = providerConf.GetOpenVPNConnection(settings.Provider.ServerSelection) if err != nil { - l.logger.Error(err) l.signalCrashedStatus() - l.cancel() - return + l.logAndWait(ctx, err) + continue } lines = providerConf.BuildConf(connection, l.username, settings) } else { @@ -142,24 +139,21 @@ func (l *looper) Run(ctx context.Context, wg *sync.WaitGroup) { //nolint:gocogni } if err := writeOpenvpnConf(lines, l.openFile); err != nil { - l.logger.Error(err) l.signalCrashedStatus() - l.cancel() - return + l.logAndWait(ctx, err) + continue } if err := l.conf.WriteAuthFile(settings.User, settings.Password, l.puid, l.pgid); err != nil { - l.logger.Error(err) l.signalCrashedStatus() - l.cancel() - return + l.logAndWait(ctx, err) + continue } if err := l.fw.SetVPNConnection(ctx, connection); err != nil { - l.logger.Error(err) l.signalCrashedStatus() - l.cancel() - return + l.logAndWait(ctx, err) + continue } openvpnCtx, openvpnCancel := context.WithCancel(context.Background())