benchmark
c82e13a8 - Snapshot dict items in get_items_from_dict to avoid mutation race (#191281)

Commit
90 days ago
Snapshot dict items in get_items_from_dict to avoid mutation race (#191281) Summary: Fixes #190612 `test_trace_return_dataclass` is flaky under `PYTORCH_TEST_WITH_DYNAMO=1`: it defines a `dataclass` inline, which on Python 3.14 makes `dataclasses._process_class` pass the defining module's `globals()` dict into `_FuncBuilder`. Dynamo realizes that dict argument in `VariableBuilder._wrap`, draining it lazily via a generator inside `dict(...)`. `get_items_from_dict`'s exact-dict branch returned the live `.items()` view instead of a list (unlike its other two branches, which already materialize lists). That globals dict is the same one Dynamo writes its own generated `__compiled_fn_*`/`__resume_at_*` names into and removes via `CleanupHook` weakref callbacks fired at GC time. If a callback fires mid-iteration, CPython raises `RuntimeError: dictionary changed size during iteration`, surfacing as `InternalTorchDynamoError`. The fix makes the exact-dict branch return `list(obj.items())`, consistent with the function's other branches. This narrows the vulnerable window from hundreds of Python bytecode steps (the full `VariableTracker` construction) to a single C-level loop; it is not a theoretically airtight fix against the free-threaded race (a GC pass could in principle still land mid-`list()`-call), but it eliminates the observed flake in practice. A provably atomic fix would need to live in the `CleanupHook`/`CleanupManager` layer instead (deferring or gating the `del`), which is a materially larger change than warranted for what is currently a disabled flaky test; that layer is also touched by the separate, still-open #191128 for a different (name-reuse) bug in the same area, so I left it alone here to keep this change minimal and low-risk. ## Test Plan Regression test added at `test/dynamo/test_utils.py::TestUtils::test_enumerate_items_with_dict_position_snapshots`, verified in both directions: ``` cd /tmp && python -m pytest test/dynamo/test_utils.py -k test_enumerate_items_with_dict_position_snapshots -v # with fix: 1 passed # git stash; same command: 1 failed with RuntimeError: dictionary changed # size during iteration (the exact CI error); git stash pop restores fix ``` Original failing test, both a CPython 3.12 and a free-threaded 3.14t build: ``` cd /tmp && PYTORCH_TEST_WITH_DYNAMO=1 python test/test_fx.py TestFX.test_trace_return_dataclass TestFX.test_trace_return_dataclass_nested # OK (2 tests) on both builds ``` Full `test/dynamo/test_utils.py` on 3.14t, compared against baseline: ``` cd /tmp && python -m pytest test/dynamo/test_utils.py # baseline (fix stashed): 10 failed, 16 passed, 1 skipped # with fix: 10 failed, 17 passed, 1 skipped # same 10 pre-existing failures (TestDynamoTimed/inductor, need a C++ # compiler unavailable in this environment), plus the one new passing test. ``` `lintrunner -a`: no findings on the two changed files. X-link: https://github.com/pytorch/pytorch/pull/191281 Approved by: https://github.com/williamwen42, https://github.com/jansel Reviewed By: jeanschmidt Differential Revision: D116859721 fbshipit-source-id: d65bffeb3bbc0fdd1085ebf86e574a31fd373e5d
Author
generatedunixname499836121
Committer
Parents
Loading