Skip to content

tests: a per-process temp file for ariadne_media_test's encode() - #1068

Merged
transfix merged 2 commits into
masterfrom
fix/media-test-temp-name
Nov 9, 2026
Merged

transfix merged 2 commits into
masterfrom
fix/media-test-temp-name

Conversation

@transfix

@transfix transfix commented Nov 8, 2026 •

Copy link
Copy Markdown
Owner

ariadne_media_test's encode() writes each test picture to a temp file, reads it back and removes it. The file name came from the thread id alone:

temp_directory_path() / ("ariadne_media_test_" + std::to_string(std::hash<std::thread::id>()(std::this_thread::get_id())) + "." + ext)

ctest runs each gtest case in its own process, and under -j several run at once. On macOS the main thread's pthread_t is a static inside libsystem_pthread, which sits in the dyld shared cache. That cache is mapped at the same address in every process for a given boot, so every test process got the same thread id hash and the SAME file name. Concurrent cases wrote, read and removed each other's file, so the bytes cvc::write_image had just written came back empty and decoding failed with "png: no image data". (hdf5_test.cpp already notes the same macOS thread-id reuse.)

Evidence

#1054's recipe / macos-latest / Release job (run 42459028712, job 128664142118):

  • Tests 757 MediaDecode.PngJpegBmpGifDecodeFromMemory, 760 MediaDecode.LimitsRefuseBeforeDecoding and 761 MediaDecode.AnimatedGifAndWebpKeepTheirFrames started within 0.8 s of each other and all failed. Their until-pass:3 retries overlapped too: 757 and 760 failed all three tries, which failed the job ("2 tests failed out of 6423").
  • MediaCache.LoadsOnceAndSharesTheFetchBetweenBoxes and MediaBackground.FtxuiDrawsHalfBlocksBehindTextAndABannerAbove, the other users of encode(), each failed once and then passed.
  • The same tests passed on ci-recipe: install the packaged cvcpkg 2.5.12, not the stale libcvc-deps repo #1050's run of that lane, where each took about 0.1 s instead of about 1 s.

#1054 itself only swaps cmake, ninja and pkg-config for cvcpkg's, and its ImageMagick, libpng and compiler match #1050's passing run. So the failure was this race, not that change.

Fix

The name is now the process id plus a per-process atomic call counter: ariadne_media_test_<pid>_<n>.<ext>. That makes it unique across concurrent ctest processes and across threads and calls within one process. It uses the same CVC_GETPID() macro as hdf5_test.cpp, hdf5_volume_test.cpp, utility_test.cpp, volume_io_test.cpp and volume_io_extra_test.cpp. The file is still closed before the remove, for Windows.

No other test in src/cvc/tests names temp files from the thread id alone. The five tests above already add the pid and a counter, and thread_pool_test.cpp hashes the thread id only to count distinct workers.

Testing

Windows (MSVC 14.44 under vcvars64, static Release, cvcpkg deps): built ariadne_media_test and ran all 44 of its cases at the same time with ctest -R "^(Media|AriadneReactiveBudget)" -j 16 --repeat until-fail:10. All 440 runs passed, and no ariadne_media_test_* file was left in %TEMP%.

Windows thread ids are unique across live processes, so the old name never collided there. This run checks that the new name holds up under concurrency. The macOS Release lane in this PR's CI is the real check.

Second commit: the scheduler stop() race

This PR's first CI run passed recipe / macos-latest / Release and Debug, but sanitizers / clang-tsan failed. TSan reported a race in state_exec_hazards_test (Hazards.ASchedulerStoppedFromAnotherThreadMayGoWhenItsRunReturns) between ~scheduler() and stop()'s store of the stop flag. That race was already on master, whose own run at 04b72a3 failed the same way, and has nothing to do with the media test.

The second commit is a cherry-pick of #1073 ("state_exec: a run reads stop()'s flag with acquire"), so this PR's tsan leg can pass. #1073 explains the cause and the fix. Whichever of the two merges first brings the change in. The other then carries an identical diff for those five files, so it merges without conflicts there.

encode() named its temp file from std::hash<std::thread::id> of the
calling thread alone. ctest runs each case in its own process, several
at once under -j, and on macOS the main thread's pthread_t lives in the
dyld shared cache at the same address in every process for a boot, so
every test process picked the same file. Concurrent cases wrote, read
and removed each other's file and decoding saw "png: no image data"
(MediaDecode.* on #1054's macOS Release recipe job).

Name the file from the process id plus a per-process atomic call
counter, with the CVC_GETPID() macro the hdf5/utility/volume_io tests
already use. The file is still closed before the remove for Windows.
stop() stores stop_requested_ with release, but run_steps(), idle_wait()
and the async run_loop() read it relaxed, so nothing ordered stop()'s
store before what the run's owner does once run() returns. An owner may
destroy the scheduler at once (Hazards.ASchedulerStoppedFromAnotherThread-
MayGoWhenItsRunReturns does), and that delete then raced the store:
clang-tsan reported it on master (04b72a3) and on unrelated PRs, as a
data race between ~scheduler()'s operator delete and stop()'s
atomic<bool>::store.

Read the flag with acquire in all three places. On x86 that is the same
plain load; elsewhere it is one ordered load per loop iteration.
@transfix
transfix merged commit 061ddf3 into master Nov 9, 2026
24 of 30 checks passed
@transfix
transfix deleted the fix/media-test-temp-name branch November 9, 2026 02:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant