I've encountered an issue where instances of Kino.JS.Live.Server.init/1 are not …shutting down properly after their use, leading to an accumulation of processes in the system. This seems to persist across sessions where Kino.JS.Live.Server is expected to terminate after completing its tasks. This can potentially lead to resource leaks and performance degradation over time.
Environment: Nerves Livebook on raspi zero w
I think this bug can be reproduced on usual livebook without nerves
Relevant Code Snippet:
```
defmodule SensorUpdate do
use GenServer
alias VegaLite, as: Vl
alias SGP40
alias SHTC3
@time_interval 180_000
@data_count 100
def start_link(frame) do
GenServer.start_link(__MODULE__, frame, name: __MODULE__)
end
def init(frame) do
{:ok, shtc} = SHTC3.start_link("i2c-1")
{:ok, sgp} = SGP40.start_link(bus_name: "i2c-1")
:timer.send_interval(@time_interval, :draw)
state = %{data: [], shtc: shtc, sgp: sgp, frame: frame}
{:ok, state}
end
def handle_info(:draw, state) do
{:ok, temp, humidity} = SHTC3.read_temp_and_humidity(state.shtc)
SGP40.update_rht(state.sgp, humidity, temp)
{:ok, sgp_measures} = SGP40.measure(state.sgp)
sgp_measures.voc_index
{:ok, time_now} = DateTime.now("Asia/Taipei")
new_data =
[
%{
datetime: time_now,
temperatur: :erlang.float_to_binary(temp, decimals: 1),
humidity: :erlang.float_to_binary(humidity, decimals: 1),
voc: sgp_measures.voc_index
}
] ++ state.data
reversed_data = Enum.reverse(new_data)
Kino.Frame.render(
state.frame.frame_chart,
Kino.Layout.grid(
[
Vl.new(height: 300, title: "VOC")
|> Vl.data_from_values(reversed_data, only: ["datetime", "voc"])
|> Vl.mark(:line)
|> Vl.encode_field(:x, "datetime", type: :temporal)
|> Vl.encode_field(:y, "voc", type: :quantitative),
Vl.new(height: 300, title: "Temperature")
|> Vl.data_from_values(reversed_data, only: ["datetime", "temperatur"])
|> Vl.mark(:line)
|> Vl.encode_field(:x, "datetime", type: :temporal)
|> Vl.encode_field(:y, "temperatur", type: :quantitative, scale: %{domain: [25, 35]}),
Vl.new(height: 300, title: "Humidity")
|> Vl.data_from_values(reversed_data, only: ["datetime", "humidity"])
|> Vl.mark(:line)
|> Vl.encode_field(:x, "datetime", type: :temporal)
|> Vl.encode_field(:y, "humidity", type: :quantitative, scale: %{domain: [45, 75]})
],
columns: 3
)
)
Kino.Frame.render(
state.frame.frame_data,
Kino.DataTable.new(new_data)
)
new_state = %{
data: Enum.take(new_data, @data_count - 1),
shtc: state.shtc,
sgp: state.sgp,
frame: state.frame
}
{:noreply, new_state}
end
end
frame_chart = Kino.Frame.new()
frame_data = Kino.Frame.new()
SensorUpdate.start_link(%{frame_chart: frame_chart, frame_data: frame_data})
```
after running as an app for 5 minutes, there are nearly 300 processes of Kino.JS.Live.Server.init
I understand this is not a suitable use case for livebook and kino, but I just want to test the stability for Kino. I think if this issue can be solved, it will help extend the use field of livebook.