Skip to content

Commit 1a94ccf

Browse files
committed
fix: Refactor runCoordinator so that previous is closed/freed
1 parent 38c7ec1 commit 1a94ccf

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
@@ -294,24 +303,23 @@ func (a *agent) runCoordinator(ctx context.Context) {
294303
defer coordinator.Close()
295304
select {
296305
case <-ctx.Done():
297-
return
306+
return false
298307
default:
299308
}
300309
sendNodes, errChan := tailnet.ServeCoordinator(coordinator, a.network.UpdateNodes)
301310
a.network.SetNodeCallback(sendNodes)
302311
select {
303312
case <-ctx.Done():
304-
return
313+
return false
305314
case err := <-errChan:
306315
if a.isClosed() {
307-
return
316+
return false
308317
}
309318
if errors.Is(err, context.Canceled) {
310-
return
319+
return false
311320
}
312321
a.logger.Debug(ctx, "node broker accept exited; restarting connection", slog.Error(err))
313-
a.runCoordinator(ctx)
314-
return
322+
return true
315323
}
316324
}
317325

0 commit comments

Comments
 (0)