Fix: port forwarding deadlock bug, fix #547
This commit is contained in:
@@ -10,10 +10,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (l *Loop) startPortForwarding(ctx context.Context,
|
func (l *Loop) startPortForwarding(ctx context.Context,
|
||||||
portForwarder provider.PortForwarder, serverName string) {
|
enabled bool, portForwarder provider.PortForwarder,
|
||||||
if !l.GetSettings().Provider.PortForwarding.Enabled {
|
serverName string) {
|
||||||
|
if !enabled {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// only used for PIA for now
|
// only used for PIA for now
|
||||||
gateway, err := l.routing.VPNLocalGatewayIP()
|
gateway, err := l.routing.VPNLocalGatewayIP()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -33,7 +35,12 @@ func (l *Loop) startPortForwarding(ctx context.Context,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Loop) stopPortForwarding(ctx context.Context, timeout time.Duration) {
|
func (l *Loop) stopPortForwarding(ctx context.Context, enabled bool,
|
||||||
|
timeout time.Duration) {
|
||||||
|
if !enabled {
|
||||||
|
return // nothing to stop
|
||||||
|
}
|
||||||
|
|
||||||
if timeout > 0 {
|
if timeout > 0 {
|
||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
ctx, cancel = context.WithTimeout(ctx, timeout)
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||||
|
|||||||
@@ -95,10 +95,12 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
|
|||||||
for stayHere {
|
for stayHere {
|
||||||
select {
|
select {
|
||||||
case <-l.startPFCh:
|
case <-l.startPFCh:
|
||||||
l.startPortForwarding(ctx, providerConf, connection.Hostname)
|
l.startPortForwarding(ctx, settings.Provider.PortForwarding.Enabled,
|
||||||
|
providerConf, connection.Hostname)
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
const pfTimeout = 100 * time.Millisecond
|
const pfTimeout = 100 * time.Millisecond
|
||||||
l.stopPortForwarding(context.Background(), pfTimeout)
|
l.stopPortForwarding(context.Background(),
|
||||||
|
settings.Provider.PortForwarding.Enabled, pfTimeout)
|
||||||
openvpnCancel()
|
openvpnCancel()
|
||||||
<-waitError
|
<-waitError
|
||||||
close(waitError)
|
close(waitError)
|
||||||
@@ -107,7 +109,7 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
|
|||||||
case <-l.stop:
|
case <-l.stop:
|
||||||
l.userTrigger = true
|
l.userTrigger = true
|
||||||
l.logger.Info("stopping")
|
l.logger.Info("stopping")
|
||||||
l.stopPortForwarding(ctx, 0)
|
l.stopPortForwarding(ctx, settings.Provider.PortForwarding.Enabled, 0)
|
||||||
openvpnCancel()
|
openvpnCancel()
|
||||||
<-waitError
|
<-waitError
|
||||||
// do not close waitError or the waitError
|
// do not close waitError or the waitError
|
||||||
@@ -124,7 +126,7 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
|
|||||||
|
|
||||||
l.statusManager.Lock() // prevent SetStatus from running in parallel
|
l.statusManager.Lock() // prevent SetStatus from running in parallel
|
||||||
|
|
||||||
l.stopPortForwarding(ctx, 0)
|
l.stopPortForwarding(ctx, settings.Provider.PortForwarding.Enabled, 0)
|
||||||
openvpnCancel()
|
openvpnCancel()
|
||||||
l.statusManager.SetStatus(constants.Crashed)
|
l.statusManager.SetStatus(constants.Crashed)
|
||||||
l.logAndWait(ctx, err)
|
l.logAndWait(ctx, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user