devinus/poolboy

Proper handling of exceptions in workers

CGenie opened this issue · 0 comments

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.