fix(reverse_proxy): release the HTTP client on every outcome

Three paths obtained a client ref and abandoned it without closing:
the content-length rejection in call/3, the invalid-status clause in
request/4, and the success branch of response/6.

Under Hackney 4 an abandoned ref is a permanent process leak. A conn
that finishes or errors without an explicit stop parks in `closed` or
`connected` and never terminates: closed/3 only arms its stop timer for
pooled conns, `connected` gets no state_timeout when idle_timeout is
infinity, and a non-pooled conn monitors hackney_conn_sup rather than
its caller, so the owner-DOWN cleanup never fires. Each parked process
pins every refc binary it read, which for proxied video is tens of MB.

Releasing on every outcome means the gun client must stop releasing in
stream_body/1, or the ref would be removed twice.

Hackney does reclaim a pooled connection itself for a response that
cannot carry a body (204, 304, HEAD), so those refs are marked and left
alone; stopping one would evict a connection another request may
already hold. The mark is gated on the request actually being pooled,
since a direct or proxy-tunnelled conn is never checked in and leaks
unless we stop it.
This commit is contained in:
Lain Soykaf 2026-07-25 18:46:43 +04:00
commit 24bdd9774a
No known key found for this signature in database
6 changed files with 249 additions and 15 deletions

View file

@ -147,6 +147,87 @@ defmodule Pleroma.HTTP.HackneyFollowRedirectRegressionTest do
Pleroma.ReverseProxy.Client.Hackney.request(:get, url, [], "", opts)
end
# A pooled connection serving a bodyless response is checked back in by hackney
# at header time, so stopping that ref afterwards would evict a connection
# another request may already hold. These go direct rather than through the
# CONNECT proxy, because a proxy tunnel is never pooled.
test "hackney reverse proxy marks a pooled bodyless response as already released",
%{tls_server: tls_server} do
url = "#{tls_server.base_url}/no_content"
assert {:ok, 204, _headers, client} =
Pleroma.ReverseProxy.Client.Hackney.request(
:get,
url,
[],
"",
direct_opts(tls_server)
)
assert %{released: true} = client
assert :done = Pleroma.ReverseProxy.Client.Hackney.stream_body(client)
assert :ok = Pleroma.ReverseProxy.Client.Hackney.close(client)
end
test "hackney reverse proxy still owns a response that carries a body",
%{tls_server: tls_server} do
url = "#{tls_server.base_url}/final"
assert {:ok, 200, _headers, client} =
Pleroma.ReverseProxy.Client.Hackney.request(
:get,
url,
[],
"",
direct_opts(tls_server)
)
refute Map.has_key?(client, :released)
assert "ok" == collect_body(client)
Pleroma.ReverseProxy.Client.Hackney.close(client)
end
# An unpooled connection is never checked in and parks forever with no idle
# timeout, so it stays ours to stop even when the response has no body.
test "hackney reverse proxy still owns a bodyless response over a proxy tunnel", %{
tls_server: tls_server,
proxy: proxy
} do
url = "#{tls_server.base_url}/no_content"
opts = Keyword.put(direct_opts(tls_server), :proxy, proxy.proxy_url)
assert {:ok, 204, _headers, client} =
Pleroma.ReverseProxy.Client.Hackney.request(:get, url, [], "", opts)
refute Map.has_key?(client, :released)
Pleroma.ReverseProxy.Client.Hackney.close(client)
end
test "hackney reverse proxy still owns a bodyless response when pooling is off", %{
tls_server: tls_server
} do
url = "#{tls_server.base_url}/no_content"
opts = Keyword.put(direct_opts(tls_server), :pool, false)
assert {:ok, 204, _headers, client} =
Pleroma.ReverseProxy.Client.Hackney.request(:get, url, [], "", opts)
refute Map.has_key?(client, :released)
Pleroma.ReverseProxy.Client.Hackney.close(client)
end
defp direct_opts(tls_server) do
[
pool: :media,
ssl_options: tls_client_options(tls_server),
connect_timeout: 1_000,
recv_timeout: 1_000,
follow_redirect: false
]
end
defp collect_body(ref, acc \\ "") do
case Pleroma.ReverseProxy.Client.Hackney.stream_body(ref) do
:done -> acc
@ -216,6 +297,9 @@ defmodule Pleroma.HTTP.HackneyFollowRedirectRegressionTest do
"/closed" ->
:ok
"/no_content" ->
send_ssl_response(ssl_socket, 204, "No Content", [], "")
"/delayed" ->
Process.sleep(100)
send_ssl_response(ssl_socket, 200, "OK", [], "ok")