Skip to content

[dynamo] Make the frame cache safe for concurrent install and lookup - #193697

Closed
bobrenjc93 wants to merge 23 commits into
gh/bobrenjc93/984/basefrom
gh/bobrenjc93/984/head
Closed

bobrenjc93 wants to merge 23 commits into
gh/bobrenjc93/984/basefrom
gh/bobrenjc93/984/head

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Fourth of the six enablers. The precompile PR above installs and unloads compiled
artifacts onto live code objects at runtime -- both through its package loader
and through the transparent caching_precompile path -- and ExtraState's cache
had no lock at all.

That was survivable while every mutation happened under the GIL between frame
evaluations. It stops being survivable once artifacts are installed at runtime,
because lookup() walks the cache while evaluating guards and guard evaluation
runs Python -- so the GIL can drop mid-iteration and another thread can clear
the list being walked.

ExtraState gets a recursive mutex, and taking it is the part that needs care.
A thread that blocks on the mutex while holding the GIL wedges the owner, who
needs the GIL to finish its guards. CacheLock therefore try_locks first and
releases the GIL only when it actually has to wait. The matching hazard is
invalidate, which receives a raw CacheEntry pointer read before the lock: it
now re-establishes the entry under the lock by the identity of the guard manager
that owns it, since a recycled list node would pass an address check. For the
same reason the frame evaluator takes a strong reference to the code object a
lookup hands back before doing anything that can drop the GIL.

Most of the rest is compile-region plumbing. Cache entries and the exec strategy
already carried a region; this extends it to precompile entries and frame state,
so several artifacts can sit on one code object and each serves only its own.
Precompile entries match their region exactly and deliberately do not fall back
to the default bucket, because the identity guards that would tell two artifacts
apart are the ones precompile has to drop. That makes teardown a question of
ownership rather than of region: a library frame two loaded models both call
holds entries from both packages, so each entry carries a token identifying its
installer and uninstall removes only its own.

Two changes here are not about regions. get_backend returns a backend's
_torchdynamo_cache_key when it has one, behind a process-global one-way gate
that only a precompile backend flips, so two artifacts on one frame get distinct
cache entries. And a _torchdynamo_force_callback_on_cache_miss marker lets a
run-only frame still reach the callback on a miss, which is what makes an
uncovered served call raise instead of quietly running eager.

Test Plan:

python test/dynamo/test_recompile_ux.py
python test/dynamo/test_package.py
python test/test_precompile.py
python test/dynamo/test_misc.py

The deadlock does not fail when reintroduced, it hangs, so its test carries a
faulthandler timeout rather than an assertion. The use-after-free reproduces as
a SIGSEGV in under 7s with 8 caller threads against 2 installers, and survives
737k install/reset cycles after the fix.

Authored with an AI assistant.

Differential Revision: D116222526

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Aug 15, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/193697

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 New Failure, 1 Unrelated Failure

As of commit b48d415 with merge base 1b4e490 (image):

NEW FAILURE - The following job has failed:

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
…reconstruction, and concurrent cache reset.

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
…on, serialization, concurrency, and cache lifetime handling.

[ghstack-poisoned]
[ghstack-poisoned]
@bobrenjc93
bobrenjc93 marked this pull request as draft August 17, 2026 21:49
This was referenced Aug 22, 2026
[ghstack-poisoned]
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

1 similar comment
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@bobrenjc93

Copy link
Copy Markdown
Contributor Author

Superseded by the canonical precompile stack #195353-#195362; this change lives in #195355. Closing to consolidate review there. The branch is kept on GitHub.

@bobrenjc93 bobrenjc93 closed this Sep 3, 2026
@github-actions
github-actions Bot deleted the gh/bobrenjc93/984/head branch November 11, 2026 01:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request meta-exported module: dynamo topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant