-
Notifications
You must be signed in to change notification settings - Fork 260
fix: stop leaking client supervisor children on failed and closed connections #554
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
Merged
polvalente
merged 6 commits into
elixir-grpc:master
from
mvanhorn:fix/531-grpc-client-supervisor-memory-leak
Jul 27, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
20a2746
fix: stop leaking supervisor children when client connections close
mvanhorn e4fdaae
test: cover finalize-failure child cleanup and bound supervisor memory
mvanhorn b8755b3
fix: address review feedback on client supervisor lifecycle
mvanhorn d4ad461
test: synchronize on registry cleanup before asserting the entry is gone
mvanhorn e6177d0
changes due to review
polvalente 14a202f
docs: document client application config
polvalente File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,12 +16,31 @@ defmodule GRPC.Client.ConnectionTest do | |
| end | ||
| end | ||
|
|
||
| defmodule NoPickResolver do | ||
| def resolve(_target) do | ||
| {:ok, | ||
| %{ | ||
| addresses: [%{address: "127.0.0.1", port: 50051}], | ||
| service_config: %{} | ||
| }} | ||
| end | ||
|
|
||
| def init(_target, _opts) do | ||
| test_pid = Application.fetch_env!(:grpc, __MODULE__) | ||
| lb_table = Enum.find(:ets.all(), &(:ets.info(&1, :owner) == self())) | ||
| true = :ets.delete(lb_table) | ||
| send(test_pid, {:connection_started, self()}) | ||
| {:ok, nil} | ||
| end | ||
| end | ||
|
|
||
| setup do | ||
| %{ | ||
| ref: make_ref(), | ||
| ip: "127.0.0.1", | ||
| target: "ipv4:127.0.0.1:50051", | ||
| adapter: GRPC.Test.ClientAdapter | ||
| adapter: GRPC.Test.ClientAdapter, | ||
| supervisor_children: DynamicSupervisor.count_children(GRPC.Client.Supervisor).active | ||
| } | ||
| end | ||
|
|
||
|
|
@@ -40,6 +59,33 @@ defmodule GRPC.Client.ConnectionTest do | |
| {:ok, channel} = Connection.connect(target, adapter: adapter, name: ref) | ||
|
|
||
| assert {:ok, ^channel} = Connection.pick_channel(%Channel{ref: ref}) | ||
|
|
||
| Connection.disconnect(channel) | ||
| end | ||
| end | ||
|
|
||
| describe "connect/2 - failed finalize" do | ||
| test "terminates a newly started child when no channel can be picked", %{ | ||
| ref: ref, | ||
| adapter: adapter, | ||
| supervisor_children: supervisor_children | ||
| } do | ||
| Application.put_env(:grpc, NoPickResolver, self()) | ||
| on_exit(fn -> Application.delete_env(:grpc, NoPickResolver) end) | ||
|
Comment on lines
+73
to
+74
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not for this PR, but I think this test suite needs refactoring so we can speed it up |
||
|
|
||
| assert {:error, :no_connection} = | ||
| Connection.connect("test://connection", | ||
| adapter: adapter, | ||
| name: ref, | ||
| resolver: NoPickResolver | ||
| ) | ||
|
|
||
| assert_receive {:connection_started, pid}, 500 | ||
| monitor_ref = Process.monitor(pid) | ||
| assert_receive {:DOWN, ^monitor_ref, :process, ^pid, _reason}, 500 | ||
|
|
||
| assert DynamicSupervisor.count_children(GRPC.Client.Supervisor).active == | ||
| supervisor_children | ||
| end | ||
| end | ||
|
|
||
|
|
@@ -92,16 +138,20 @@ defmodule GRPC.Client.ConnectionTest do | |
| test "GenServer process is no longer alive after disconnect", %{ | ||
| ref: ref, | ||
| target: target, | ||
| adapter: adapter | ||
| adapter: adapter, | ||
| supervisor_children: supervisor_children | ||
| } do | ||
| {:ok, channel} = Connection.connect(target, adapter: adapter, name: ref) | ||
|
|
||
| pid = whereis_name(ref) | ||
| ref_mon = Process.monitor(pid) | ||
|
|
||
| {:ok, _} = Connection.disconnect(channel) | ||
|
|
||
| ref_mon = Process.monitor(pid) | ||
| assert_receive {:DOWN, ^ref_mon, :process, ^pid, _reason}, 500 | ||
|
|
||
| assert DynamicSupervisor.count_children(GRPC.Client.Supervisor).active == | ||
| supervisor_children | ||
| end | ||
|
|
||
| test "pick_channel returns {:error, :no_connection} after disconnect (persistent_term entry is erased)", | ||
|
|
@@ -114,23 +164,6 @@ defmodule GRPC.Client.ConnectionTest do | |
| end | ||
| end | ||
|
|
||
| describe "terminate/2 - persistent_term cleanup on process kill" do | ||
| test "persistent_term entry is erased when process is killed without disconnect", %{ | ||
| ref: ref, | ||
| target: target, | ||
| adapter: adapter | ||
| } do | ||
| {:ok, channel} = Connection.connect(target, adapter: adapter, name: ref) | ||
|
|
||
| pid = whereis_name(ref) | ||
| ref_mon = Process.monitor(pid) | ||
| GenServer.stop(pid, :shutdown) | ||
| assert_receive {:DOWN, ^ref_mon, :process, ^pid, :shutdown}, 500 | ||
|
|
||
| assert {:error, :no_connection} = Connection.pick_channel(channel) | ||
| end | ||
| end | ||
|
|
||
| describe "LB ETS table lifecycle" do | ||
| test "disconnect/1 exits the GenServer and the ETS table is freed", %{ | ||
| ref: ref, | ||
|
|
@@ -215,6 +248,35 @@ defmodule GRPC.Client.ConnectionTest do | |
| end | ||
|
|
||
| describe "resource leaks over repeated connect/disconnect" do | ||
| test "50 binary-heavy cycles leave supervisor memory bounded after garbage collection", %{ | ||
| target: target, | ||
| adapter: adapter, | ||
| supervisor_children: supervisor_children | ||
| } do | ||
| supervisor = Process.whereis(GRPC.Client.Supervisor) | ||
| :erlang.garbage_collect(supervisor) | ||
| {:memory, before_memory} = Process.info(supervisor, :memory) | ||
|
|
||
| for cycle <- 1..50 do | ||
| ref = make_ref() | ||
| header = {"x-test-data", :binary.copy(<<cycle>>, 100_000)} | ||
|
|
||
| {:ok, channel} = | ||
| Connection.connect(target, adapter: adapter, name: ref, headers: [header]) | ||
|
|
||
| {:ok, _} = Connection.disconnect(channel) | ||
| end | ||
|
|
||
| :erlang.garbage_collect(supervisor) | ||
| {:memory, after_memory} = Process.info(supervisor, :memory) | ||
|
|
||
| assert %{active: ^supervisor_children} = | ||
| DynamicSupervisor.count_children(GRPC.Client.Supervisor) | ||
|
|
||
| assert after_memory <= before_memory + 100_000, | ||
| "supervisor memory grew: before=#{before_memory} after=#{after_memory}" | ||
| end | ||
|
|
||
| test "500 cycles leave persistent_term clean and no per-LB tables leak", %{ | ||
| target: target, | ||
| adapter: adapter | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.