From 719e796621da4072197d7b5ca2070fcb62bb9d2d Mon Sep 17 00:00:00 2001 From: Alexander Koutmos Date: Sun, 7 May 2023 01:32:24 -0400 Subject: [PATCH] Attempting to fix binary memory leak in ETS flusher (#200) (cherry picked from commit b80c52e2f80d938dee0ff277cd038d1d6b540706) --- lib/prom_ex/ets_cron_flusher.ex | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/prom_ex/ets_cron_flusher.ex b/lib/prom_ex/ets_cron_flusher.ex index 2f19c215..bd8e91da 100644 --- a/lib/prom_ex/ets_cron_flusher.ex +++ b/lib/prom_ex/ets_cron_flusher.ex @@ -8,6 +8,8 @@ defmodule PromEx.ETSCronFlusher do use GenServer + @flush_timeout 10_000 + @doc """ Used to start the `PromEx.ETSCronFlusher` process. """ @@ -48,7 +50,15 @@ defmodule PromEx.ETSCronFlusher do @impl true def handle_info(:flush_ets, state) do - PromEx.get_metrics(state.prom_ex_module) + # In order to avoid leaking large binaries of metrics, the flush should take place + # inside of an ephemeral task so that the heap memory is reclaimed when the process + # dies + flush_task = + Task.async(fn -> + PromEx.get_metrics(state.prom_ex_module) + end) + + Task.await(flush_task, @flush_timeout) timer_ref = schedule_flush(state) {:noreply, %{state | timer_ref: timer_ref}}