From 4965b575c93639bb357ab6a2ef75b51676cac2fa Mon Sep 17 00:00:00 2001 From: Angel Velazquez Date: Fri, 5 Nov 2021 14:58:19 -0700 Subject: [PATCH] Fix memory leak in task stats collector --- agent/stats/task_linux.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/agent/stats/task_linux.go b/agent/stats/task_linux.go index e617d790314..b388f596641 100644 --- a/agent/stats/task_linux.go +++ b/agent/stats/task_linux.go @@ -79,15 +79,15 @@ func newStatsTaskContainer(taskARN string, containerPID string, numberOfContaine }, nil } -func (task *StatsTask) StartStatsCollection() { +func (taskStat *StatsTask) StartStatsCollection() { queueSize := int(config.DefaultContainerMetricsPublishInterval.Seconds() * 4) - task.StatsQueue = NewQueue(queueSize) - task.StatsQueue.Reset() - go task.collect() + taskStat.StatsQueue = NewQueue(queueSize) + taskStat.StatsQueue.Reset() + go taskStat.collect() } -func (task *StatsTask) StopStatsCollection() { - task.Cancel() +func (taskStat *StatsTask) StopStatsCollection() { + taskStat.Cancel() } func (taskStat *StatsTask) collect() { @@ -204,7 +204,7 @@ func linkStatsToDockerStats(netLinkStats *netlinklib.LinkStatistics, numberOfCon func (taskStat *StatsTask) getAWSVPCNetworkStats() (<-chan *types.StatsJSON, <-chan error) { - errC := make(chan error) + errC := make(chan error, 1) statsC := make(chan *dockerstats.StatsJSON) if taskStat.TaskMetadata.NumberContainers > 0 { go func() { @@ -248,7 +248,11 @@ func (taskStat *StatsTask) getAWSVPCNetworkStats() (<-chan *types.StatsJSON, <-c Read: time.Now(), }, } - statsC <- dockerStats + select { + case <-taskStat.Ctx.Done(): + return + case statsC <- dockerStats: + } } }() }