Repository navigation
Fix races in Logger and CaptureIO tests - #16255
Merged
Merged
Conversation
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).
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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'sLogger.flush/0can reach Logger before Logger handles the first queued event. Handling that event is whenLogger.Backends.Confignotifies Logger that it is discarding. The notification is then handled after the flush, andupdate_counter/2sets the counter to the number of messages queued at that point: the test's:update_countermessage andcapture_log's flush. The periodic check then logs "Attempted to log 2 messages, which is below :discard_threshold" instead of 0, and theassert_receivefails.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"
StringIOreplies toclosebefore it exits. After the failed capture, theStringIOthatExUnit.CaptureServeropened can therefore still be alive and monitoring the server when the test readsmonitored_by.Fix: monitor each new monitoring process and assert that it goes down. A leaked
StringIOnever exits, so the test still catches a real leak.CaptureIO: stale
:stderrcapture with input"raises when async capturing a named device with an input given to an already captured device" left a process capturing
:stderrwithinput: "first"until ExUnit shut the test process down.CaptureServerreleases 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_offcall 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