Skip to content

Commit 7e2ad50

Browse files
committed
fix: Refactor runCoordinator so that previous is closed/freed
1 parent d84d792 commit 7e2ad50

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

agent/agent.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,15 @@ func (a *agent) runTailnet(ctx context.Context, derpMap *tailcfg.DERPMap) {
272272

273273
// runCoordinator listens for nodes and updates the self-node as it changes.
274274
func (a *agent) runCoordinator(ctx context.Context) {
275+
for {
276+
reconnect := a.runCoordinatorWithRetry(ctx)
277+
if !reconnect {
278+
return
279+
}
280+
}
281+
}
282+
283+
func (a *agent) runCoordinatorWithRetry(ctx context.Context) (reconnect bool) {
275284
var coordinator net.Conn
276285
var err error
277286
// An exponential back-off occurs when the connection is failing to dial.
@@ -280,10 +289,10 @@ func (a *agent) runCoordinator(ctx context.Context) {
280289
coordinator, err = a.coordinatorDialer(ctx)
281290
if err != nil {
282291
if errors.Is(err, context.Canceled) {
283-
return
292+
return false
284293
}
285294
if a.isClosed() {
286-
return
295+
return false
287296
}
288297
a.logger.Warn(context.Background(), "failed to dial", slog.Error(err))
289298
continue
@@ -295,24 +304,23 @@ func (a *agent) runCoordinator(ctx context.Context) {
295304
}
296305
select {
297306
case <-ctx.Done():
298-
return
307+
return false
299308
default:
300309
}
301310
sendNodes, errChan := tailnet.ServeCoordinator(coordinator, a.network.UpdateNodes)
302311
a.network.SetNodeCallback(sendNodes)
303312
select {
304313
case <-ctx.Done():
305-
return
314+
return false
306315
case err := <-errChan:
307316
if a.isClosed() {
308-
return
317+
return false
309318
}
310319
if errors.Is(err, context.Canceled) {
311-
return
320+
return false
312321
}
313322
a.logger.Debug(ctx, "node broker accept exited; restarting connection", slog.Error(err))
314-
a.runCoordinator(ctx)
315-
return
323+
return true
316324
}
317325
}
318326

0 commit comments

Comments
 (0)