Skip to content

Commit 6b365f4

Browse files
authored
fix: Ensure coordinator is closed and freed in agent (#4164)
* fix: Close coordinator on context cancellation * fix: Refactor runCoordinator so that previous is closed/freed
1 parent 2e30d05 commit 6b365f4

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

agent/agent.go

+18-9
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,38 +289,38 @@ 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
290299
}
300+
//nolint:revive // Defer is ok because we're exiting this loop.
301+
defer coordinator.Close()
291302
a.logger.Info(context.Background(), "connected to coordination server")
292303
break
293304
}
294305
select {
295306
case <-ctx.Done():
296-
return
307+
return false
297308
default:
298309
}
299-
defer coordinator.Close()
300310
sendNodes, errChan := tailnet.ServeCoordinator(coordinator, a.network.UpdateNodes)
301311
a.network.SetNodeCallback(sendNodes)
302312
select {
303313
case <-ctx.Done():
304-
return
314+
return false
305315
case err := <-errChan:
306316
if a.isClosed() {
307-
return
317+
return false
308318
}
309319
if errors.Is(err, context.Canceled) {
310-
return
320+
return false
311321
}
312322
a.logger.Debug(ctx, "node broker accept exited; restarting connection", slog.Error(err))
313-
a.runCoordinator(ctx)
314-
return
323+
return true
315324
}
316325
}
317326

0 commit comments

Comments
 (0)