Skip to content

Fix races in Logger and CaptureIO tests - #16255

Merged
josevalim merged 2 commits into
elixir-lang:mainfrom
SteffenDE:sd-fix-test-races
Dec 1, 2026
Merged

josevalim merged 2 commits into
elixir-lang:mainfrom
SteffenDE:sd-fix-test-races

Conversation

@SteffenDE

Copy link
Copy Markdown
Contributor

Three tests depended on an order of messages that BEAM usually produces but doesn't guarantee. Each one fails when a process is descheduled at the wrong moment. None failed in 200 runs on BEAM, but each one failed in a deterministic-simulation sweep.

Logger: "logs when discarding messages"

After :sys.resume(Logger), the test's Logger.flush/0 can reach Logger before Logger handles the first queued event. Handling that event is when Logger.Backends.Config notifies Logger that it is discarding. The notification is then handled after the flush, and update_counter/2 sets the counter to the number of messages queued at that point: the test's :update_counter message and capture_log's flush. The periodic check then logs "Attempted to log 2 messages, which is below :discard_threshold" instead of 0, and the assert_receive fails.

Fix: flush twice. The notification is queued while Logger handles the first event, which comes before the first flush, so it has been handled by the time the second flush returns.

Simulation of backends_test.exs: 4 of 200 seeds failed before, 0 of 400 after.

CaptureIO: "does not leak StringIO when named device does not exist"

StringIO replies to close before it exits. After the failed capture, the StringIO that ExUnit.CaptureServer opened can therefore still be alive and monitoring the server when the test reads monitored_by.

Fix: monitor each new monitoring process and assert that it goes down. A leaked StringIO never exits, so the test still catches a real leak.

CaptureIO: stale :stderr capture with input

"raises when async capturing a named device with an input given to an already captured device" left a process capturing :stderr with input: "first" until ExUnit shut the test process down. CaptureServer releases the device only when it handles that process's :DOWN, which can come after the next test has started. A later test that captures :stderr (seen in "supports async capture_io", "double capture from the same process", "raises when async capturing a named device with a different encoding than the first" and a doctest) then fails with "attempted multiple captures on device :standard_error with input".

Fix: the process ends its capture when the test tells it to, and the test waits for it to exit. Its device_capture_off call completes before it exits, so the device is released before the test ends.

Simulation of capture_io_test.exs (preemption rates 1/8 and 1/32): 7 failures in 800 seeds for the two CaptureIO races before, 0 in 800 after.

All three test files pass on BEAM (OTP 29.1.3).

Assisted-by: Claude Code Opus 5.5

After :sys.resume(Logger), the test's Logger.flush/0 may reach Logger
before it handles the first queued event, which is when
Logger.Backends.Config notifies Logger that it is discarding. The
notification is then handled after the flush, and update_counter sets
the counter to the messages queued at that time (the :update_counter
message and capture_log's flush), so the periodic check logs
"Attempted to log 2 messages" instead of 0. A second flush makes sure
the notification was handled.

Found with rotp's deterministic simulation (4 of 200 seeds of
backends_test.exs; 0 of 400 with this change).
"raises when async capturing a named device with an input given to an
already captured device" left a process that captured :stderr with
input until ExUnit shut the test process down. CaptureServer released
the device only when it handled that process's :DOWN, which could come
after the next test captured :stderr, which then failed with "attempted
multiple captures on device :standard_error with input". The process
now ends its capture before the test ends.

"does not leak StringIO when named device does not exist" read the
monitors of CaptureServer right after the failed capture. StringIO
replies to close before it exits, so the StringIO could still monitor
CaptureServer. The test now checks that each new monitoring process
exits.

Found with rotp's deterministic simulation (7 failures in 800 seeds of
capture_io_test.exs; 0 in 800 with this change).
@josevalim
josevalim merged commit 2151291 into elixir-lang:main Dec 1, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants