appcues / mojito

An easy-to-use Elixir HTTP client, built on the low-level Mint library.
https://hexdocs.pm/mojito/Mojito.html
MIT License
349 stars 34 forks source link

Mojito leaking messages to caller #57

Closed egze closed 2 years ago

egze commented 4 years ago

We have a GenServer that is polling a webservice with Mojito. Constantly the genserver process is receiving messages like:

{:mojito_response, ref, payload}

We ignore them like so:

def handle_info({:mojito_response, _ref, message}, state) do
  # Not sure why is Mojito sending it here.
  {:noreply, state}
end

But I think this is a bug. Why is the caller process receiving these messages in the first place? I think these messages come back after Mojito already returned a timeout error, but not sure.

andyleclair commented 4 years ago

IIRC this is a poolboy quirk that has to do with monitoring processes that have timed out. Machine Gun (which also uses poolboy) has the same issue and has a note at the bottom of the readme about using it in GenServer processes.

https://github.com/petrohi/machine_gun

gamache commented 4 years ago

It's not a Poolboy quirk per se. It happens in certain cases when a calling process makes a request that hits the timeout period. The caller stops listening for a message and moves past the call to Mojito.request. But because this takes place in a distributed system, it is possible for this to happen when the response has actually arrived, which results in a Mojito response message being sent to a caller process that is no longer waiting for it. When that caller process is a GenServer, the message will end up being handled by the handle_info callback.

It may be possible to cut down on the number of times this occurs by sending a "hangup" message from the caller to the Mojito connserver when a timeout occurs, essentially doubling the number of race conditions that need to be met for a spurious emission, but I don't know of a lightweight way to solve it completely.

ntenczar commented 2 years ago

Hi! This project is now deprecated:

We recommend that you use Finch which is also built on Mint. The creator of Finch has an excellent writeup here describing the problems with Mojito, and as a result we use Finch internally at Appcues now.