Repository navigation
cephadm/tests: pre-import tracemalloc to avoid pyfakefs setup race - #71163
Conversation
Since 2026-08-18 run_tox_cephadm has been failing in roughly a quarter of
arm64 make check runs (and ~8% of x86 runs) with
RuntimeError: dictionary changed size during iteration
pyfakefs/fake_filesystem_unittest.py:831 in Patcher._find_modules
during `fs` fixture setup, followed by 80+ cascading
AttributeError: 'NoneType' object has no attribute 'add_real_directory'
because pyfakefs' Patcher is a ref-counted singleton and a failed setUp()
never gets its tearDown().
There is no second thread involved. Several TestBootstrap tests run
command_bootstrap() to completion, which leaves NamedTemporaryFile objects
created on the fake filesystem to be garbage collected after the fake fs
has been torn down. Their __del__ raises, which makes pytest's (>= 8.4)
unraisable-exception hook run *inside the GC pass*, and the first time it
does so it lazily imports tracemalloc (tracemalloc_message()). If that GC
fires while pyfakefs 5.3.5 is doing `list(sys.modules.items())`, the
iteration blows up. Newer pyfakefs (>= 5.10) snapshots sys.modules with
dict.copy() and is immune, but 5.10.x is not a drop-in upgrade for this
test suite, so import tracemalloc up front instead so that nothing new
gets added to sys.modules from within a finalizer.
Signed-off-by: David Galloway <[email protected]>
Assisted-by: Claude Fable 5 (claude-fable-5)
9848473 to
7989817
Compare
|
Hit counts for the Jenkins
Every Local reproduction — full cephadm tox
Hits were detected by wrapping |
|
Thank you for this @djgalloway |
…fakefs setup race (#43) Backport-Id: 01a177f4-63f1-7e3d-a389-f72fcfbb888d Backport-PR: #43 Upstream-PRs: ceph#71189, ceph#71163
Since 2026-08-18
run_tox_cephadmhas been failing in roughly a quarter of arm64make checkruns (and ~8% of x86 runs) with oneduring
fsfixture setup, followed by 80+ cascadingAttributeError: 'NoneType' object has no attribute 'add_real_directory'(pyfakefs'Patcheris a ref-counted singleton; a failedsetUp()never gets itstearDown(), so every laterfstest seesfs=None). Examples: arm64 #102097, x86 #191801.Root cause
Reproduced locally (arm64 jammy container, py3.10, pyfakefs 5.3.5, pytest 9.1) with
_find_modulesinstrumented: no other threads exist;sys.modulesgainstracemalloc/_tracemallocduring the iteration and a gen-2 GC runs inside it.TestBootstraptests runcommand_bootstrap()to completion, which leavesNamedTemporaryFileobjects created on the pyfakefs fake filesystem to be garbage collected after the fake fs is torn down.__del__raises (these show up asPytestUnraisableExceptionWarning: Exception ignored in: <function _TemporaryFileCloser.__del__ ...>in the run output already).tracemalloc(_pytest.tracemalloc.tracemalloc_message).list(sys.modules.items())inPatcher._find_modules(), the iteration fails.#65592 didn't introduce the leaked temp files (the unraisable warnings exist before and after it); it shifted allocation/GC timing in exactly the tests preceding the victim, which is why the flake started with that merge (and its umbrella backport #71137 shows the same failure on x86). The arm64/x86 rate difference is GC-timing luck, not an arch-specific bug.
Fix
Pre-import
tracemallocinsrc/cephadm/tests/conftest.pyso nothing new is added tosys.modulesfrom within a finalizer. Verified with a pytest plugin that forces atracemallocimport from a GC callback during_find_modules: pyfakefs 5.3.5 fails deterministically without this change and passes 195/195 with it.Also measured with the real (non-forced) race, 40 full-suite runs each under
stress-ng --cpu 6in an arm64 jammy container: parent of #65592 → 0 hits,mainafter #65592 → 8 hits (20%),main+ this patch → 0 hits.Alternatives considered: pyfakefs ≥ 5.10 snapshots
sys.moduleswithdict.copy()and is immune, but 5.10.2 breaks 16 cephadm tests withPermissionError(stricter fake-fs permission checks), so it isn't a drop-in bump. A proper follow-up would be to close the bootstrap temp files so the unraisable exceptions disappear entirely.cc @timqn22 @adk3798
Contribution Guidelines
To sign and title your commits, please refer to Submitting Patches to Ceph.
If you are submitting a fix for a stable branch (e.g. "quincy"), please refer to Submitting Patches to Ceph - Backports for the proper workflow.
When filling out the below checklist, you may click boxes directly in the GitHub web UI. When entering or editing the entire PR message in the GitHub web UI editor, you may also select a checklist item by adding an
xbetween the brackets:[ ]becomes[x]. Spaces and capitalization matter when checking off items this way.Checklist