Skip to content

logger:get_handler_config/0 may fail if a logger handler is removed in the process #9997

@martosaur

Description

@martosaur

Describe the bug
I'm running a lot of asynchronous tests for logger handlers and whenever logger:get_handler_config/0 is called, it has a chance of crashing if one of the handlers is removed mid-flight.

To Reproduce
To reproduce it, call the function while handle configs are being actively removed. Here's my one file reproducion (sorry for Elixir 😢 )

test
defmodule MyHandler do
  @behaviour :logger_handler
  
  def log(_, _), do: :ok
end

ExUnit.start()

defmodule ATest do
  use ExUnit.Case, async: true
  
  test "A" do
    ids = Enum.map(1..1000, fn i -> i |> to_string() |> String.to_atom() end)
  
    for id <- ids do
      :logger.add_handler(id, MyHandler, %{config: %{}})
    end
    
    GenServer.whereis(:state)
    |> Agent.get(fn pid -> send(pid, :ready) end)

    for id <- ids do
      :logger.remove_handler(id)
    end
  end
end

defmodule BTest do
  use ExUnit.Case, async: true
  
  test "B" do
    test_pid = self()
    Agent.start_link(fn -> test_pid end, name: :state)

  receive do
    :ready ->
      :logger.get_handler_config()
    end
  end
end
  1) test B (BTest)
     otp_repro.exs:31
     ** (MatchError) no match of right hand side value:

         {:error, {:not_found, :"2"}}
     code: :logger.get_handler_config()
     stacktrace:
       (kernel 10.3.1) logger.erl:1101: :logger."-get_handler_config/0-lc$^0/1-0-"/1
       (kernel 10.3.1) logger.erl:1103: :logger."-get_handler_config/0-lc$^0/1-0-"/1
       otp_repro.exs:37: (test)

Expected behavior
I would expect logger:get_handler_config/0 to omit configs for handlers that don't exist at the moment of calling get_handler_config(HandlerId)

Affected versions
28.0.1

Metadata

Metadata

Assignees

Labels

bugIssue is reported as a bugstalledwaiting for input by the Erlang/OTP teamteam:VMAssigned to OTP team VM

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions