Laetitia

Laetitia

Task + DbTransaction DbConnection error

Hi guys,

I would like to know if anybody could help me.
I have a genserver that calls a TaskSupervisor to start tasks like this:

 task =
      Task.Supervisor.async_nolink(
        UiccProvisioningsSupervisor,
        fn -> Toto.bar() end
      )

    Process.send_after(self(), {:kill, task}, state.task_timeout)

and a handle_info like this:

def handle_info({:kill, task}, %{tasks: tasks} = state) do
if tasks[task.ref] do
      Task.shutdown(task, :brutal_kill)
      Toto.call_to_db()
      {:noreply, %{state | tasks: Map.delete(tasks, task.ref)}}
    else
      {:noreply, state}
    end

The toto.foo contains a transaction like this:

  def foo(params) do
    Repo.transaction(fn ->
      ota_request = Repo.insert!(%Foo{} |> Foo.create_changeset(params))
      Process.sleep(150)
    end)
  end

In this case , I got in the terminate callback of the Genserver:

scheduler terminated: {{:shutdown,
  %DBConnection.ConnectionError{
    message: "client #PID<0.698.0> exited",
    reason: :error,
    severity: :error
  }},
 {DBConnection.Holder, :checkout,
  [
    #PID<0.682.0>,
    [
      log: #Function<13.1473765/1 in Ecto.Adapters.SQL.with_log/3>,
      source: "uicc_provisionings",
      cast_params: [nil, nil, ~N[2023-06-22 11:12:24], "icc_id_3529"],
      repo: SimManager.Repo,
      timeout: 15000,
      pool: DBConnection.Ownership,
      pool_size: 10
    ]
  ]}}

I found that the task is the exited client. And the problem disappears when I remove the transaction.

First Post!

al2o3cr

al2o3cr

Hard to say 100% without real code, but my suspicion is that the Task ends up sharing a database connection with the GenServer, then brutal_killing the Task leaves the connection in an unusable state.

Where Next?

Popular in Questions Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement