Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Proper handling of exceptions in workers #148

@CGenie

Description

@CGenie

Hello,

It's not clear to me how to handle exceptions inside poolboy workers. I'm using Elixir, but I think this applies to Erlang as well.

My code looks more or less like this:

task = Task.async(fn ->
  :poolboy.transaction(:worker, fn pid -> 
    try do
      GenServer.call(pid, {:someWorkerFunction})
    catch
      e, r ->
        # reraise e, r
    end
  end
end)
Task.await(task)

Suppose the worker function throws an exception (i.e. the call with {:someWorkerFunction}). If I comment out reraise e, r as above, worker fails silently and my code continues to run as nothing happened.

However, when I uncomment that reraise e, r statement (or in fact just remove the try...catch block), the worker crashes in such a way that I'm not able to catch it (wrapping Task.await in try...after seems to do nothing).

How should I proceed to be able to recover from such crashes as I need to call some cleanup code? I know the docs say that try...after "provides only a soft guarantee", but I guess I'm bit of a noob to handle this properly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions