Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ssl_closed message leak when server hangs up ("Connection: close" header) #640

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

rozap
Copy link
Contributor

@rozap rozap commented Jun 12, 2020

This should fix #464

  • mutually recursive maybe_continue and async_recv
    functions are really tricky to reason about, but
    it doesn't seem like it makes sense for maybe_continue
    to do error handling at all, since async_recv was already
    doing it

    • maybe_continue and stream_loop shouldn't select
      socket messages
    • we're getting into a state where the socket sends
      us a message while we're doing other things and
      we do that receive and then crash
  • this change makes async_recv the only one to do
    a select for transport messages

  • When the server closes the connection
    and :ssl sends us :ssl_closed, we weren't
    handling the message, which caused it to leak
    into the mailbox even after the request was ended

    • need to flush to make sure it's gone in this case
      where the server hangs up
    • caused when Connection: close header is sent

fix debugging statement

* mutually recursive maybe_continue and async_recv
  functions are really tricky to reason about, but
  it doesn't seem like it makes sense for maybe_continue
  to do error handling at all, since async_recv was already
  doing it
  * maybe_continue and stream_loop shouldn't select
    socket messages
  * we're getting into a state where the socket sends
    us a message while we're doing other things and
    we do that receive and then crash
* this change makes async_recv the only one to do
  a select for transport messages

* When the server closes the connection
  and :ssl sends us :ssl_closed, we weren't
  handling the message, which caused it to leak
  into the mailbox even after the request was ended
  * need to flush to make sure it's gone in this case
    where the server hangs up
  * caused when Connection: close header is sent

fix debugging statement
@rozap rozap changed the title Fix erroneous unexpted message Fix ssl_closed message leak when server hangs up ("Connection: close" header) Jun 12, 2020
* bug i think appeared in 6ddc33c
* when streaming the response, we terminate
  the parse recursive calls early since
  there is no match for {:more, {hparser, ...:on_trailers...}}
  case
@benoitc benoitc self-requested a review June 19, 2020 07:20
@benoitc benoitc self-assigned this Jun 19, 2020
@benoitc benoitc added this to the 1.17.0 milestone Sep 11, 2020
@erik-mocny
Copy link

@benoitc Hi, please why is it still not merged as a whole? I can see only second commit changes in 1.16.0...v1.17.0 probably cherry-picked? Thanks

@benoitc
Copy link
Owner

benoitc commented Dec 29, 2020

i didn't cherry-picked :) I merged your other PR #641 . I didn't merge this one yet as i wanted to test it first. There will be a new release next week that should fix the issue.

@erik-mocny
Copy link

Thanks for the reply. It wasn't my PR I just asked about it because we were facing same issue in our projects.

@Dvogiatz
Copy link

Dvogiatz commented Feb 1, 2021

Hello @benoitc, what is the current status on this PR?

There will be a new release next week that should fix the issue.

Also is this referring to a 1.17.1 version coming soon? Because with 1.17.0 which mentions this PR, the issue still exists. Thank
you

edit* I cloned the 1.17.0 master branch and cherry picked this PR and I can confirm that it solved the issue.

@Dvogiatz
Copy link

Dvogiatz commented Feb 4, 2021

Update: after 2 days of working properly without any issues, it started to throw the same error again.

@benoitc
Copy link
Owner

benoitc commented Feb 5, 2021 via email

@luizmiranda7
Copy link

@benoitc I'm trying the new version of hackney: 1.17.4 but it keeps the same behavior:

{:ssl_closed,
 {:sslsocket, {:gen_tcp, #Port<0.330>, :tls_connection, :undefined}
  • Erlang/OTP 22 [erts-10.7.2.7]
  • Hackney 1.17.4

@ringofhealth
Copy link

ringofhealth commented May 18, 2021

 [error] Unexpected message: {:ssl_closed, {:sslsocket, {:gen_tcp, #Port<0.213>, :tls_connection, :undefined}, [#PID<0.2999.0>, #PID<0.2998.0>]}}

same issue here as well

Erlang/OTP 22 [erts-10.7.2.7]
Hackney 1.17.4

here is the function implementation


  def get(url) do
    Stream.resource(
      # start_fun
      fn ->
        HTTPoison.get!(
          url,
          %{},
          stream_to: self(),
          async: :once,
          hackney: [
            ssl_options: [
              versions: [:"tlsv1.2"]
            ]
          ]
        )
      end,
      fn %HTTPoison.AsyncResponse{id: id} = resp ->
        receive do
          %HTTPoison.AsyncStatus{id: ^id, code: _code} ->
            HTTPoison.stream_next(resp)
            {[], resp}

          %HTTPoison.AsyncHeaders{id: ^id, headers: _headers} ->
            HTTPoison.stream_next(resp)
            {[], resp}

          %HTTPoison.AsyncChunk{id: ^id, chunk: chunk} ->
            HTTPoison.stream_next(resp)
            {[chunk], resp}

          %HTTPoison.AsyncEnd{id: ^id} ->
            {:halt, resp}
        end
      end,
      fn _end_func ->
        nil
      end
    )
  end

jutonz added a commit to jutonz/homepage that referenced this pull request May 28, 2021
We're seeing a whole bunch of [errors] in sentry related to an unhandled
`{:ssl_closed, _info}` message. This was apparently [fixed] by a Hackney
patch, but we're on latest and still experiencing the issue, so I guess
not!

[errors]: https://sentry.io/share/issue/207a371da909426aadf6658651b0ebc9/
[fixed]: benoitc/hackney#640
jutonz added a commit to jutonz/homepage that referenced this pull request May 28, 2021
We're seeing a whole bunch of [errors] in sentry related to an unhandled
`{:ssl_closed, _info}` message. This was apparently [fixed] by a Hackney
patch, but we're on latest and still experiencing the issue, so I guess
not!

[errors]: https://sentry.io/share/issue/207a371da909426aadf6658651b0ebc9/
[fixed]: benoitc/hackney#640
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

unhandled {ssl_closed, _} message when using async
6 participants