Skip to content

Commit e7dfd84

Browse files
committed
Prevent exits from leaking in ExUnit.CaptureServer
When a process that traps exits used ExUnit's log capture functionality, it would receive stray exits from the Tasks started inside it that could lead to issues if not handled. We fix this by disabling exit trapping while awaiting the tasks in the CaptureServer's log handler.
1 parent 8e49286 commit e7dfd84

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/ex_unit/lib/ex_unit/capture_server.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ defmodule ExUnit.CaptureServer do
231231
## :logger handler callback.
232232

233233
def log(event, _config) do
234+
trap_exits = Process.flag(:trap_exit, false)
235+
234236
:ets.tab2list(@ets)
235237
|> Enum.filter(fn {_ref, _string_io, level, _formatter_mod, _formatter_config} ->
236238
:logger.compare_levels(event.level, level) in [:gt, :eq]
@@ -255,6 +257,8 @@ defmodule ExUnit.CaptureServer do
255257
end)
256258
|> Task.await_many(:infinity)
257259

260+
Process.flag(:trap_exit, trap_exits)
261+
258262
:ok
259263
end
260264
end

lib/ex_unit/test/ex_unit/capture_log_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ defmodule ExUnit.CaptureLogTest do
9595
end)
9696
end
9797

98+
test "exits don't leak" do
99+
Process.flag(:trap_exit, true)
100+
101+
capture_log(fn ->
102+
Logger.error("oh no!")
103+
end)
104+
105+
refute_receive {:EXIT, _, _}
106+
end
107+
98108
describe "with_log/2" do
99109
test "returns the result and the log" do
100110
{result, log} =

0 commit comments

Comments
 (0)