Skip to content

Commit 5667170

Browse files
committed
fix(tailnet): Ensure connstats are closed on conn close
1 parent 18aabca commit 5667170

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

tailnet/conn.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -744,16 +744,25 @@ func (c *Conn) forwardTCPToLocal(conn net.Conn, port uint16) {
744744
func (c *Conn) SetConnStatsCallback(maxPeriod time.Duration, maxConns int, dump func(start, end time.Time, virtual, physical map[netlogtype.Connection]netlogtype.Counts)) {
745745
connStats := connstats.NewStatistics(maxPeriod, maxConns, dump)
746746

747+
closeStats := func(s *connstats.Statistics) {
748+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
749+
defer cancel()
750+
_ = s.Shutdown(ctx)
751+
}
752+
747753
c.mutex.Lock()
754+
if c.isClosed() {
755+
c.mutex.Unlock()
756+
closeStats(connStats)
757+
return
758+
}
748759
old := c.trafficStats
749760
c.trafficStats = connStats
750761
c.mutex.Unlock()
751762

752763
// Make sure to shutdown the old callback.
753764
if old != nil {
754-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
755-
defer cancel()
756-
_ = old.Shutdown(ctx)
765+
closeStats(old)
757766
}
758767

759768
c.tunDevice.SetStatistics(connStats)

0 commit comments

Comments
 (0)