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

The receive loop is too permissive #14

Closed intentionally-left-nil closed 5 years ago

intentionally-left-nil commented 5 years ago

Mojito reuses whatever process it was called on, and therefore is not in control of what messages arrive in its mailbox.

However, when waiting for a response from the ConnServer, Mojito will take the first message that it finds:

 receive do
        reply ->
          GenServer.stop(pid)
reply

This is too permissive and causes erroneous :error messages to be returned as a Mojito request, as well as eating a message that was not destined for mojito

mrusme commented 5 years ago

Experiencing exactly this right now:

23:14:52.344 [debug] Mojito.ConnServer #PID<0.1678.0>: post https://my_url/
:ok
23:14:54.355 [debug] Mojito.ConnServer #PID<0.1678.0>: terminating (:normal)
23:14:54.355 [debug] Mojito.ConnServer #PID<0.1678.0>: cleaning up
23:14:54.357 [error] HTTP request failed: {:error, %Mojito.Error{message: {:circuits_gpio, 20, 2109433806000, 1}, reason: :unknown}}
23:14:54.357 [debug] {:error, :closed}

The HTTP Post was started, then a message not destined for Mojito arrived and was processed by Mojito causing it to error and abort a perfectly good working HTTP post.

@AnilRedshift thank you for the PR, hope this gets merged & released quickly!