fix(portforward): no longer stuck after failed port forwarding

This commit is contained in:
Quentin McGaw
2026-04-20 15:27:47 +00:00
parent 8bc2fbd487
commit 7eef1c89a7
2 changed files with 12 additions and 2 deletions
+1
View File
@@ -32,6 +32,7 @@ type Logger interface {
Info(s string) Info(s string)
Warn(s string) Warn(s string)
Error(s string) Error(s string)
Errorf(format string, args ...any)
} }
type Cmder interface { type Cmder interface {
+11 -2
View File
@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"sync" "sync"
"time"
"github.com/qdm12/gluetun/internal/configuration/settings" "github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/portforward/service" "github.com/qdm12/gluetun/internal/portforward/service"
@@ -87,6 +88,8 @@ func (l *Loop) run(runCtx context.Context, runDone chan<- struct{},
defer close(runDone) defer close(runDone)
var serviceRunError <-chan error var serviceRunError <-chan error
var retryAfter <-chan time.Time
const retryDelay = 5 * time.Second
for { for {
updateReceived := false updateReceived := false
select { select {
@@ -105,10 +108,12 @@ func (l *Loop) run(runCtx context.Context, runDone chan<- struct{},
l.settingsMutex.Unlock() l.settingsMutex.Unlock()
case err := <-serviceRunError: case err := <-serviceRunError:
l.logger.Error(err.Error()) l.logger.Error(err.Error())
case <-retryAfter:
// Retry starting the service after a delay
retryAfter = nil
} }
firstRun := serviceRunError == nil if l.service != nil {
if !firstRun {
err := l.service.Stop() err := l.service.Stop()
if err != nil { if err != nil {
runErrorCh <- fmt.Errorf("stopping previous service: %w", err) runErrorCh <- fmt.Errorf("stopping previous service: %w", err)
@@ -132,6 +137,10 @@ func (l *Loop) run(runCtx context.Context, runDone chan<- struct{},
err = fmt.Errorf("starting port forwarding service: %w", err) err = fmt.Errorf("starting port forwarding service: %w", err)
} }
updateResult <- err updateResult <- err
} else if err != nil {
// Log the error and schedule a retry
l.logger.Errorf("starting port forwarding service: %s - retrying in %s", err, retryDelay)
retryAfter = time.After(retryDelay)
} }
} }
} }