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

Don't send RST_STREAM when not needed #434

Merged
merged 2 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions lib/mint/http2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1566,7 +1566,7 @@ defmodule Mint.HTTP2 do
responses = [{:data, stream.ref, data} | responses]

if flag_set?(flags, :data, :end_stream) do
conn = close_stream!(conn, stream.id, :no_error)
conn = close_stream!(conn, stream.id, :remote_end_stream)
{conn, [{:done, stream.ref} | responses]}
else
{conn, responses}
Expand Down Expand Up @@ -1675,7 +1675,7 @@ defmodule Mint.HTTP2 do
{conn, responses}

end_stream? ->
conn = close_stream!(conn, stream.id, :no_error)
conn = close_stream!(conn, stream.id, :remote_end_stream)
{conn, [{:done, ref} | new_responses]}

true ->
Expand All @@ -1685,7 +1685,7 @@ defmodule Mint.HTTP2 do
end

end_stream? ->
conn = close_stream!(conn, stream.id, :no_error)
conn = close_stream!(conn, stream.id, :remote_end_stream)
{conn, [{:done, ref} | new_responses]}

true ->
Expand All @@ -1695,7 +1695,7 @@ defmodule Mint.HTTP2 do
# Trailer headers. We don't care about the :status header here.
headers when received_first_headers? ->
if end_stream? do
conn = close_stream!(conn, stream.id, :no_error)
conn = close_stream!(conn, stream.id, :remote_end_stream)
headers = headers |> Headers.remove_unallowed_trailer() |> join_cookie_headers()
{conn, [{:done, ref}, {:headers, ref, headers} | responses]}
else
Expand Down Expand Up @@ -2094,18 +2094,27 @@ defmodule Mint.HTTP2 do
throw({:mint, %{conn | state: :closed}, wrap_error({error_code, debug_data})})
end

defp close_stream!(conn, stream_id, error_code) do
# Reason is either an error code or `remote_end_stream`
defp close_stream!(conn, stream_id, reason) do
stream = Map.fetch!(conn.streams, stream_id)

# First of all we send a RST_STREAM with the given error code so that we
# move the stream to the :closed state (that is, we remove it).
rst_stream_frame = rst_stream(stream_id: stream_id, error_code: error_code)

conn =
if open?(conn) do
send!(conn, Frame.encode(rst_stream_frame))
else
conn
cond do
# If the stream is ended on both sides, it is already deemed closed and
# there's no need to send a RST_STREAM frame
reason == :remote_end_stream and stream.state == :half_closed_local ->
conn

# We send a RST_STREAM with the given error code so that we move the
# stream to the :closed state (that is, we remove it).
open?(conn) ->
error_code = if reason == :remote_end_stream, do: :no_error, else: reason
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this line is ever used, but it doesn't feel right to remove it (?)

rst_stream_frame = rst_stream(stream_id: stream_id, error_code: error_code)
send!(conn, Frame.encode(rst_stream_frame))

# If the connection is already closed, no-op
true ->
conn
end

delete_stream(conn, stream)
Expand Down
26 changes: 21 additions & 5 deletions test/mint/http2/conn_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@

# If the first argument is not a connection struct, we return false.
assert is_connection_message(%{socket: conn.socket}, {:tcp, conn.socket, "foo"}) == false
assert is_connection_message(%URI{}, {:tcp, conn.socket, "foo"}) == false

Check warning on line 84 in test/mint/http2/conn_test.exs

View workflow job for this annotation

GitHub Actions / Test (Elixir 1.11.4-otp-21, OTP 21.3)

this expression will fail with KeyError
end
end

Expand Down Expand Up @@ -325,7 +325,7 @@

assert [{:status, ^ref, 200}, {:headers, ^ref, []}, {:done, ^ref}] = responses

assert_recv_frames [rst_stream(stream_id: ^stream_id)]
assert Enum.empty?(conn.streams)

assert {:ok, %HTTP2{} = conn, []} =
stream_frames(conn, [
Expand All @@ -335,6 +335,25 @@

assert HTTP2.open?(conn)
end

test "doesn't send RST_STREAM when stream is declared ended in both sides", %{conn: conn} do
{conn, ref} = open_request(conn)

assert_recv_frames [headers(stream_id: stream_id)]

assert conn.streams[stream_id].state == :half_closed_local

assert {:ok, %HTTP2{} = conn, responses} =
stream_frames(conn, [
{:headers, stream_id, [{":status", "200"}], [:end_headers, :end_stream]}
])

assert [{:status, ^ref, 200}, {:headers, ^ref, []}, {:done, ^ref}] = responses

assert_recv_frames([])
assert is_nil(conn.streams[stream_id])
assert HTTP2.open?(conn)
end
end

describe "stream state transitions" do
Expand Down Expand Up @@ -763,8 +782,7 @@

assert [{:status, ^ref, 200}, {:headers, ^ref, _headers}, {:done, ^ref}] = responses

assert_recv_frames [rst_stream(error_code: :no_error)]

assert Enum.empty?(conn.streams)
assert HTTP2.open?(conn)
end

Expand Down Expand Up @@ -1466,8 +1484,6 @@
{:done, ^ref}
] = responses

assert_recv_frames [rst_stream(stream_id: ^stream_id, error_code: :no_error)]

# Here we send headers for the two promised streams. Note that neither of the
# header frames have the END_STREAM flag set otherwise we close the streams and
# they don't count towards the open stream count.
Expand Down
Loading