Skip to content

Commit

Permalink
Ensure socket owner always exits (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
whatyouhide authored Apr 14, 2024
1 parent 4a8537a commit 9058dfc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/redix/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,13 @@ defmodule Redix.Connection do
{:keep_state, data, actions}

{:error, _reason} ->
# The socket owner will get a closed message at some point, so we just move to the
# disconnected state.
# The socket owner is not guaranteed to get a "closed" message, even if we close the
# socket here. So, we move to the disconnected state but also notify the owner that
# sending failed. If the owner already got the "closed" message, it exited so this
# message goes nowere, otherwise the socket owner will exit and notify the connection.
# See https://github.com/whatyouhide/redix/issues/265.
:ok = data.transport.close(data.socket)
send(data.socket_owner, {:send_errored, self()})
{:next_state, :disconnected, data}
end
else
Expand Down
13 changes: 13 additions & 0 deletions lib/redix/socket_owner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ defmodule Redix.SocketOwner do
end
end

# The connection is notifying the socket owner that sending failed. If the socket owner
# gets this, it can stop normally without waiting for the "closed"/"error" network
# message from the socket.
def handle_info({:send_errored, conn}, %__MODULE__{conn: conn} = state) do
error =
case state.transport do
:ssl -> {:ssl_error, :closed}
:gen_tcp -> {:tcp_error, :closed}
end

stop(error, state)
end

def handle_info({transport, socket, data}, %__MODULE__{socket: socket} = state)
when transport in [:tcp, :ssl] do
:ok = setopts(state, socket, active: :once)
Expand Down

0 comments on commit 9058dfc

Please sign in to comment.