Skip to content

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

Merged
batrick merged 1 commit into
ceph:mainfrom
djgalloway:wip-cephadm-tox-tracemalloc
Aug 19, 2026
Merged

batrick merged 1 commit into
ceph:mainfrom
djgalloway:wip-cephadm-tox-tracemalloc

Conversation

@djgalloway

@djgalloway djgalloway commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

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 one

RuntimeError: dictionary changed size during iteration
  .tox/py3/lib/python3.10/site-packages/pyfakefs/fake_filesystem_unittest.py:831: in _find_modules

during fs fixture setup, followed by 80+ cascading AttributeError: 'NoneType' object has no attribute 'add_real_directory' (pyfakefs' Patcher is a ref-counted singleton; a failed setUp() never gets its tearDown(), so every later fs test sees fs=None). Examples: arm64 #102097, x86 #191801.

Root cause

Reproduced locally (arm64 jammy container, py3.10, pyfakefs 5.3.5, pytest 9.1) with _find_modules instrumented: no other threads exist; sys.modules gains tracemalloc/_tracemalloc during the iteration and a gen-2 GC runs inside it.

  1. Several TestBootstrap tests run command_bootstrap() to completion, which leaves NamedTemporaryFile objects created on the pyfakefs fake filesystem to be garbage collected after the fake fs is torn down.
  2. Their __del__ raises (these show up as PytestUnraisableExceptionWarning: Exception ignored in: <function _TemporaryFileCloser.__del__ ...> in the run output already).
  3. pytest ≥ 8.4's unraisable-exception hook runs inside the GC pass and, the first time, lazily imports tracemalloc (_pytest.tracemalloc.tracemalloc_message).
  4. If that GC fires while pyfakefs 5.3.5 is doing list(sys.modules.items()) in Patcher._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 tracemalloc in src/cephadm/tests/conftest.py so nothing new is added to sys.modules from within a finalizer. Verified with a pytest plugin that forces a tracemalloc import 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 6 in an arm64 jammy container: parent of #65592 → 0 hits, main after #65592 → 8 hits (20%), main + this patch → 0 hits.

Alternatives considered: pyfakefs ≥ 5.10 snapshots sys.modules with dict.copy() and is immune, but 5.10.2 breaks 16 cephadm tests with PermissionError (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 x between the brackets: [ ] becomes [x]. Spaces and capitalization matter when checking off items this way.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced (ff93b87, cephadm: Version Tracking #65592)
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)

@djgalloway
djgalloway requested a review from a team as a code owner August 19, 2026 16:04
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)
@djgalloway
djgalloway force-pushed the wip-cephadm-tox-tracemalloc branch from 9848473 to 7989817 Compare August 19, 2026 16:15
@djgalloway

Copy link
Copy Markdown
Contributor Author

Hit counts for the run_tox_cephadm pyfakefs race, for context.

Jenkins make check, completed (SUCCESS/FAILURE) runs since #65592 merged (2026-08-18 13:59 UTC → 2026-08-19 ~17:00 UTC)

job completed runs run_tox_cephadm race hits rate
ceph-pull-requests-arm64 (make check (arm64)) 56 15 27%
ceph-pull-requests (make check, x86_64) 68 6 9%

Every run_tox_cephadm failure in that window on both jobs has this exact signature (no other cause); zero hits in the ~40 arm64 builds before the merge. 3 of the x86 hits are #71137, the umbrella backport of #65592.

Local reproduction — full cephadm tox py3 suite, arm64 Ubuntu 22.04 container (py3.10, pyfakefs 5.3.5, pytest 9.1.1), stress-ng --cpu 6 in the background to mimic a loaded build container:

tree runs race hits
origin/main, unstressed 25 0
origin/main, stressed 25 1
parent of #65592 (ff93b87b850^), stressed 40 0
#65592 merge commit (ff93b87b850), stressed 40 8
#65592 merge commit + this patch, stressed 40 0

Hits were detected by wrapping Patcher._find_modules; on every hit the modules added to sys.modules during the iteration were exactly ['_tracemalloc', 'tracemalloc'], a gen-2 GC had run inside _find_modules, and MainThread was the only thread.

@phlogistonjohn phlogistonjohn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks ok to me 🤯

@timqn22

timqn22 commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Thank you for this @djgalloway

@batrick
batrick merged commit 78193dc into ceph:main Aug 19, 2026
8 of 9 checks passed
s0nea added a commit to cobaltcore-dev/ceph that referenced this pull request Nov 3, 2026
…fakefs setup race (#43)

Backport-Id: 01a177f4-63f1-7e3d-a389-f72fcfbb888d
Backport-PR: #43
Upstream-PRs: ceph#71189, ceph#71163
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants