@@ -91,13 +91,15 @@ func NewServerTailnet(
91
91
})
92
92
}
93
93
94
- derpMapUpdaterClosed := make ( chan struct {})
94
+ bgRoutines := & sync. WaitGroup {}
95
95
originalDerpMap := derpMapFn ()
96
96
// it's important to set the DERPRegionDialer above _before_ we set the DERP map so that if
97
97
// there is an embedded relay, we use the local in-memory dialer.
98
98
conn .SetDERPMap (originalDerpMap )
99
+ bgRoutines .Add (1 )
99
100
go func () {
100
- defer close (derpMapUpdaterClosed )
101
+ defer bgRoutines .Done ()
102
+ defer logger .Debug (ctx , "polling DERPMap exited" )
101
103
102
104
ticker := time .NewTicker (5 * time .Second )
103
105
defer ticker .Stop ()
@@ -120,7 +122,7 @@ func NewServerTailnet(
120
122
tn := & ServerTailnet {
121
123
ctx : serverCtx ,
122
124
cancel : cancel ,
123
- derpMapUpdaterClosed : derpMapUpdaterClosed ,
125
+ bgRoutines : bgRoutines ,
124
126
logger : logger ,
125
127
tracer : traceProvider .Tracer (tracing .TracerName ),
126
128
conn : conn ,
@@ -170,8 +172,15 @@ func NewServerTailnet(
170
172
// registering the callback also triggers send of the initial node
171
173
tn .coordinatee .SetNodeCallback (tn .nodeCallback )
172
174
173
- go tn .watchAgentUpdates ()
174
- go tn .expireOldAgents ()
175
+ tn .bgRoutines .Add (2 )
176
+ go func () {
177
+ defer tn .bgRoutines .Done ()
178
+ tn .watchAgentUpdates ()
179
+ }()
180
+ go func () {
181
+ defer tn .bgRoutines .Done ()
182
+ tn .expireOldAgents ()
183
+ }()
175
184
return tn , nil
176
185
}
177
186
@@ -204,6 +213,7 @@ func (s *ServerTailnet) Collect(metrics chan<- prometheus.Metric) {
204
213
}
205
214
206
215
func (s * ServerTailnet ) expireOldAgents () {
216
+ defer s .logger .Debug (s .ctx , "stopped expiring old agents" )
207
217
const (
208
218
tick = 5 * time .Minute
209
219
cutoff = 30 * time .Minute
@@ -255,6 +265,7 @@ func (s *ServerTailnet) doExpireOldAgents(cutoff time.Duration) {
255
265
}
256
266
257
267
func (s * ServerTailnet ) watchAgentUpdates () {
268
+ defer s .logger .Debug (s .ctx , "stopped watching agent updates" )
258
269
for {
259
270
conn := s .getAgentConn ()
260
271
resp , ok := conn .NextUpdate (s .ctx )
@@ -317,9 +328,9 @@ func (s *ServerTailnet) reinitCoordinator() {
317
328
}
318
329
319
330
type ServerTailnet struct {
320
- ctx context.Context
321
- cancel func ()
322
- derpMapUpdaterClosed chan struct {}
331
+ ctx context.Context
332
+ cancel func ()
333
+ bgRoutines * sync. WaitGroup
323
334
324
335
logger slog.Logger
325
336
tracer trace.Tracer
@@ -532,10 +543,12 @@ func (c *netConnCloser) Close() error {
532
543
}
533
544
534
545
func (s * ServerTailnet ) Close () error {
546
+ s .logger .Info (s .ctx , "closing server tailnet" )
547
+ defer s .logger .Debug (s .ctx , "server tailnet close complete" )
535
548
s .cancel ()
536
549
_ = s .conn .Close ()
537
550
s .transport .CloseIdleConnections ()
538
- <- s . derpMapUpdaterClosed
551
+ s . bgRoutines . Wait ()
539
552
return nil
540
553
}
541
554
0 commit comments