Fix: openvpn run loop panic about stdout streams

This commit is contained in:
Quentin McGaw (desktop)
2021-07-16 19:02:04 +00:00
parent ac3ff095a1
commit c2d527bbd3

View File

@@ -171,6 +171,11 @@ func (l *looper) Run(ctx context.Context, done chan<- struct{}) { //nolint:gocog
lineCollectionDone := make(chan struct{}) lineCollectionDone := make(chan struct{})
go l.collectLines(stdoutLines, stderrLines, lineCollectionDone) go l.collectLines(stdoutLines, stderrLines, lineCollectionDone)
closeStreams := func() {
close(stdoutLines)
close(stderrLines)
<-lineCollectionDone
}
// Needs the stream line from main.go to know when the tunnel is up // Needs the stream line from main.go to know when the tunnel is up
portForwardDone := make(chan struct{}) portForwardDone := make(chan struct{})
@@ -200,29 +205,26 @@ func (l *looper) Run(ctx context.Context, done chan<- struct{}) { //nolint:gocog
openvpnCancel() openvpnCancel()
<-waitError <-waitError
close(waitError) close(waitError)
close(stdoutLines) closeStreams()
close(stderrLines)
<-lineCollectionDone
<-portForwardDone <-portForwardDone
return return
case <-l.stop: case <-l.stop:
l.logger.Info("stopping") l.logger.Info("stopping")
openvpnCancel() openvpnCancel()
<-waitError <-waitError
// do not close waitError or the waitError
// select case will trigger
closeStreams()
<-portForwardDone
l.stopped <- struct{}{} l.stopped <- struct{}{}
case <-l.start: case <-l.start:
l.logger.Info("starting") l.logger.Info("starting")
stayHere = false stayHere = false
case err := <-waitError: // unexpected error case err := <-waitError: // unexpected error
openvpnCancel() openvpnCancel()
if ctx.Err() != nil { close(waitError)
close(waitError) closeStreams()
close(stdoutLines) <-portForwardDone
close(stderrLines)
<-lineCollectionDone
<-portForwardDone
return
}
l.state.setStatusWithLock(constants.Crashed) l.state.setStatusWithLock(constants.Crashed)
l.logAndWait(ctx, err) l.logAndWait(ctx, err)
l.crashed = true l.crashed = true
@@ -241,13 +243,13 @@ func (l *looper) Run(ctx context.Context, done chan<- struct{}) { //nolint:gocog
l.logger.Warn("unhealthy program: restarting openvpn") l.logger.Warn("unhealthy program: restarting openvpn")
openvpnCancel() openvpnCancel()
<-waitError <-waitError
close(waitError)
closeStreams()
<-portForwardDone
l.state.setStatusWithLock(constants.Stopped) l.state.setStatusWithLock(constants.Stopped)
stayHere = false stayHere = false
} }
} }
close(waitError)
close(stdoutLines)
close(stderrLines)
openvpnCancel() // just for the linter openvpnCancel() // just for the linter
} }
} }