SIGN IN SIGN UP

cephadm/tests: pre-import tracemalloc to avoid pyfakefs setup race

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 <david.galloway@ibm.com>
Assisted-by: Claude Fable 5 (claude-fable-5)
D
David Galloway committed
79898177cb5f13da1631bb7e22f41248beeb54dc
Parent: dea19d2