From fb549632893f9c152fa3e98b9c1b4a6a648bbf7a Mon Sep 17 00:00:00 2001 From: Lukasz Samson Date: Sun, 18 Oct 2026 23:22:24 +0200 Subject: [PATCH] Fix BreakpointCondition crash for unregistered breakpoint numbers get_condition/2 crashed the GenServer when no condition was registered under the requested number. The instrumented code can still call check_N/1 after the breakpoint was removed, replaced, or after clear/0 freed the number, which hit elem(1) on nil. get_condition/2 now returns nil for an unregistered number, and the generated check_N/1 functions return false in that case. --- .../lib/debug_adapter/breakpoint_condition.ex | 80 +++++++++++-------- .../test/breakpoint_condition_test.exs | 66 +++++++++++++++ 2 files changed, 112 insertions(+), 34 deletions(-) diff --git a/apps/debug_adapter/lib/debug_adapter/breakpoint_condition.ex b/apps/debug_adapter/lib/debug_adapter/breakpoint_condition.ex index 5d5827701..7de219032 100644 --- a/apps/debug_adapter/lib/debug_adapter/breakpoint_condition.ex +++ b/apps/debug_adapter/lib/debug_adapter/breakpoint_condition.ex @@ -49,7 +49,7 @@ defmodule ElixirLS.DebugAdapter.BreakpointCondition do end @spec get_condition(module, non_neg_integer) :: - {Macro.Env.t(), String.t(), String.t() | nil, String.t(), non_neg_integer} + {Macro.Env.t(), String.t(), String.t() | nil, String.t(), non_neg_integer} | nil def get_condition(name \\ __MODULE__, number) do GenServer.call(name, {:get_condition, number}) end @@ -142,11 +142,16 @@ defmodule ElixirLS.DebugAdapter.BreakpointCondition do end def handle_call({:get_condition, number}, _from, %{conditions: conditions, hits: hits} = state) do - {env, condition, log_message, hit_count} = - conditions |> Map.values() |> Enum.find(fn {n, _c} -> n == number end) |> elem(1) + case conditions |> Map.values() |> Enum.find(fn {n, _c} -> n == number end) do + nil -> + # the number may have been freed by unregister_condition/clear while + # the interpreted code still calls the check function + {:reply, nil, state} - hits = hits |> Map.get(number, 0) - {:reply, {env, condition, log_message, hit_count, hits}, state} + {_n, {env, condition, log_message, hit_count}} -> + hits = hits |> Map.get(number, 0) + {:reply, {env, condition, log_message, hit_count, hits}, state} + end end def handle_call(:clear, _from, _state) do @@ -187,35 +192,42 @@ defmodule ElixirLS.DebugAdapter.BreakpointCondition do for i <- @range do @spec unquote(:"check_#{i}")(term) :: boolean def unquote(:"check_#{i}")(binding) do - {env, condition, log_message, hit_count_condition, hits} = get_condition(unquote(i)) - elixir_binding = binding |> ElixirLS.DebugAdapter.Binding.to_elixir_variable_names() - result = eval_condition(condition, elixir_binding, env) - - result = - if result do - register_hit(unquote(i)) - # do not break if hit count not reached - # the spec requires: - # If both this property and `condition` are specified, `hitCondition` should - # be evaluated only if the `condition` is met, and the debug adapter should - # stop only if both conditions are met. - - hit_count = eval_hit_condition(hit_count_condition, elixir_binding, env) - hits + 1 >= hit_count - else - result - end - - if result and log_message != nil do - # Debug Adapter Protocol: - # If this attribute exists and is non-empty, the backend must not 'break' (stop) - # but log the message instead. Expressions within {} are interpolated. - # If either `hitCondition` or `condition` is specified, then the message - # should only be logged if those conditions are met. - Output.debugger_console(interpolate(log_message, elixir_binding, env)) - false - else - result + case get_condition(unquote(i)) do + # breakpoint was removed or replaced after the code was instrumented + nil -> + false + + {env, condition, log_message, hit_count_condition, hits} -> + elixir_binding = binding |> ElixirLS.DebugAdapter.Binding.to_elixir_variable_names() + result = eval_condition(condition, elixir_binding, env) + + result = + if result do + register_hit(unquote(i)) + + # do not break if hit count not reached + # the spec requires: + # If both this property and `condition` are specified, `hitCondition` should + # be evaluated only if the `condition` is met, and the debug adapter should + # stop only if both conditions are met. + + hit_count = eval_hit_condition(hit_count_condition, elixir_binding, env) + hits + 1 >= hit_count + else + result + end + + if result and log_message != nil do + # Debug Adapter Protocol: + # If this attribute exists and is non-empty, the backend must not 'break' (stop) + # but log the message instead. Expressions within {} are interpolated. + # If either `hitCondition` or `condition` is specified, then the message + # should only be logged if those conditions are met. + Output.debugger_console(interpolate(log_message, elixir_binding, env)) + false + else + result + end end end end diff --git a/apps/debug_adapter/test/breakpoint_condition_test.exs b/apps/debug_adapter/test/breakpoint_condition_test.exs index d15bb36ae..eb3ca55d4 100644 --- a/apps/debug_adapter/test/breakpoint_condition_test.exs +++ b/apps/debug_adapter/test/breakpoint_condition_test.exs @@ -190,6 +190,72 @@ defmodule ElixirLS.DebugAdapter.BreakpointConditionTest do assert {_, "c == d", "xxx", "2", 1} = BreakpointCondition.get_condition(@name, 1) end + test "get_condition for an unregistered number returns nil and keeps the server alive" do + pid = Process.whereis(@name) + + assert BreakpointCondition.get_condition(@name, 5) == nil + + assert {:ok, {BreakpointCondition, :check_0}} == + BreakpointCondition.register_condition(@name, Some, 123, __ENV__, "a == b", nil, "1") + + assert :ok == BreakpointCondition.unregister_condition(@name, Some, 123) + assert BreakpointCondition.get_condition(@name, 0) == nil + + assert :ok == BreakpointCondition.clear(@name) + assert BreakpointCondition.get_condition(@name, 0) == nil + + assert Process.alive?(pid) + assert Process.whereis(@name) == pid + end + + test "check functions return false for an unregistered number and keep the server alive" do + start_supervised!( + Supervisor.child_spec({BreakpointCondition, name: BreakpointCondition}, + id: :default_breakpoint_condition + ) + ) + + pid = Process.whereis(BreakpointCondition) + + # never registered + refute apply(BreakpointCondition, :check_3, [[]]) + + # registered, then removed while the instrumented code still calls it + {:ok, {BreakpointCondition, check_fun}} = + BreakpointCondition.register_condition( + BreakpointCondition, + Some, + 201, + __ENV__, + "true", + nil, + "0" + ) + + assert apply(BreakpointCondition, check_fun, [[]]) + + :ok = BreakpointCondition.unregister_condition(BreakpointCondition, Some, 201) + refute apply(BreakpointCondition, check_fun, [[]]) + + # cleared after registration + {:ok, {BreakpointCondition, check_fun}} = + BreakpointCondition.register_condition( + BreakpointCondition, + Some, + 202, + __ENV__, + "true", + nil, + "0" + ) + + :ok = BreakpointCondition.clear(BreakpointCondition) + refute apply(BreakpointCondition, check_fun, [[]]) + + assert Process.alive?(pid) + assert Process.whereis(BreakpointCondition) == pid + end + test "register_hit" do assert {:ok, {BreakpointCondition, :check_0}} == BreakpointCondition.register_condition(@name, Some, 123, __ENV__, "a == b", nil, "1")