[client] Avoid deadlock when auto connect and early exit (#2528)

This commit is contained in:
Maycon Santos
2024-09-04 19:22:33 +02:00
committed by GitHub
parent 1ff7a953a0
commit c52b406afa
2 changed files with 28 additions and 18 deletions

View File

@@ -61,9 +61,9 @@ func (c *ConnectClient) Run() error {
// RunWithProbes runs the client's main logic with probes attached
func (c *ConnectClient) RunWithProbes(
probes *ProbeHolder,
runningWg *sync.WaitGroup,
runningChan chan error,
) error {
return c.run(MobileDependency{}, probes, runningWg)
return c.run(MobileDependency{}, probes, runningChan)
}
// RunOnAndroid with main logic on mobile system
@@ -104,7 +104,7 @@ func (c *ConnectClient) RunOniOS(
func (c *ConnectClient) run(
mobileDependency MobileDependency,
probes *ProbeHolder,
runningWg *sync.WaitGroup,
runningChan chan error,
) error {
defer func() {
if r := recover(); r != nil {
@@ -195,6 +195,7 @@ func (c *ConnectClient) run(
log.Debug(err)
if s, ok := gstatus.FromError(err); ok && (s.Code() == codes.PermissionDenied) {
state.Set(StatusNeedsLogin)
_ = c.Stop()
return backoff.Permanent(wrapErr(err)) // unrecoverable error
}
return wrapErr(err)
@@ -263,8 +264,9 @@ func (c *ConnectClient) run(
log.Infof("Netbird engine started, the IP is: %s", peerConfig.GetAddress())
state.Set(StatusConnected)
if runningWg != nil {
runningWg.Done()
if runningChan != nil {
runningChan <- nil
close(runningChan)
}
<-engineCtx.Done()
@@ -287,6 +289,7 @@ func (c *ConnectClient) run(
log.Debugf("exiting client retry loop due to unrecoverable error: %s", err)
if s, ok := gstatus.FromError(err); ok && (s.Code() == codes.PermissionDenied) {
state.Set(StatusNeedsLogin)
_ = c.Stop()
}
return err
}