Waits properly for all subprocess to exit

This commit is contained in:
Quentin McGaw
2020-07-08 23:42:54 +00:00
parent 99e386abc8
commit 3d25db1bed
4 changed files with 8 additions and 12 deletions

View File

@@ -110,9 +110,7 @@ func (l *looper) Run(ctx context.Context, restart <-chan struct{}, wg *sync.Wait
waitError := make(chan error)
go func() {
err := waitFn() // blocking
if unboundCtx.Err() != context.Canceled {
waitError <- err
}
waitError <- err
}()
// Wait for one of the three cases below
@@ -120,6 +118,7 @@ func (l *looper) Run(ctx context.Context, restart <-chan struct{}, wg *sync.Wait
case <-ctx.Done():
l.logger.Warn("context canceled: exiting loop")
unboundCancel()
<-waitError
close(waitError)
return
case <-restart: // triggered restart

View File

@@ -62,14 +62,13 @@ func (l *looper) Run(ctx context.Context, restart <-chan struct{}, wg *sync.Wait
waitError := make(chan error)
go func() {
err := waitFn() // blocking
if openvpnCtx.Err() != context.Canceled {
waitError <- err
}
waitError <- err
}()
select {
case <-ctx.Done():
l.logger.Warn("context canceled: exiting loop")
openvpnCancel()
<-waitError
close(waitError)
return
case <-restart: // triggered restart

View File

@@ -90,14 +90,13 @@ func (l *looper) Run(ctx context.Context, restart <-chan struct{}, wg *sync.Wait
waitError := make(chan error)
go func() {
err := waitFn() // blocking
if shadowsocksCtx.Err() != context.Canceled {
waitError <- err
}
waitError <- err
}()
select {
case <-ctx.Done():
l.logger.Warn("context canceled: exiting loop")
shadowsocksCancel()
<-waitError
close(waitError)
return
case <-restart: // triggered restart

View File

@@ -82,14 +82,13 @@ func (l *looper) Run(ctx context.Context, restart <-chan struct{}, wg *sync.Wait
waitError := make(chan error)
go func() {
err := waitFn() // blocking
if tinyproxyCtx.Err() != context.Canceled {
waitError <- err
}
waitError <- err
}()
select {
case <-ctx.Done():
l.logger.Warn("context canceled: exiting loop")
tinyproxyCancel()
<-waitError
close(waitError)
return
case <-restart: // triggered restart