Thanks to visit codestin.com
Credit goes to github.com

Skip to content

_remove_obj_from_tls to fix deadlock involving _stash_obj_in_tls (#182083)#182083

Closed
williamwen42 wants to merge 1 commit into
mainfrom
export-D103314406
Closed

_remove_obj_from_tls to fix deadlock involving _stash_obj_in_tls (#182083)#182083
williamwen42 wants to merge 1 commit into
mainfrom
export-D103314406

Conversation

@williamwen42

@williamwen42 williamwen42 commented May 1, 2026

Copy link
Copy Markdown
Member

Summary:

Fix deadlock caused by #173568, which stashes a contextvars.Context as a SafePyObject in C++ thread_local storage during every backward() call. When a C++ thread exits, __call_tls_dtors destroys the thread_local and the SafePyObject destructor tries to acquire the GIL — deadlocking if the GIL holder is blocked on that thread (as in UP's GpuThreadGroup).

The old cleanup (_stash_obj_in_tls("context", None)) replaced the entry with a SafePyObject(None) but still left a SafePyObject in the map. This adds _remove_obj_from_tls which erases the key entirely, so no SafePyObject survives to thread exit.

Test Plan:
Green signals

test_correctness_fsdp_semi_frozen and test_correctness_one_pp_clip_mixed under UP tests passing locally

Reviewed By: malfet

Differential Revision: D103314406

@pytorch-bot

pytorch-bot Bot commented May 1, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit 024f390 with merge base 0051214 (image):
💚 Looks good so far! There are no failures yet. 💚

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

@meta-codesync

meta-codesync Bot commented May 1, 2026

Copy link
Copy Markdown

@williamwen42 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D103314406.

@rtimpe

rtimpe commented May 4, 2026

Copy link
Copy Markdown
Collaborator

Actually, thinking about this some more, wouldn't this be subject to the same deadlock/race described in #180701?

@meta-codesync meta-codesync Bot changed the title _remove_obj_from_tls to fix deadlock involving _stash_obj_in_tls _remove_obj_from_tls to fix deadlock involving _stash_obj_in_tls (#182083) May 14, 2026
meta-codesync Bot pushed a commit that referenced this pull request May 14, 2026
…2083)

Summary:

Fix deadlock caused by #173568, which stashes a contextvars.Context as a SafePyObject in C++ thread_local storage during every backward() call. When a C++ thread exits, __call_tls_dtors destroys the thread_local and the SafePyObject destructor tries to acquire the GIL — deadlocking if the GIL holder is blocked on that thread (as in UP's GpuThreadGroup).

The old cleanup (_stash_obj_in_tls("context", None)) replaced the entry with a SafePyObject(None) but still left a SafePyObject in the map. This adds _remove_obj_from_tls which erases the key entirely, so no SafePyObject survives to thread exit.

Test Plan: Green signals

Differential Revision: D103314406
@meta-codesync meta-codesync Bot force-pushed the export-D103314406 branch from 3fd07d4 to f997a4f Compare May 14, 2026 21:44
@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label May 15, 2026
meta-codesync Bot pushed a commit that referenced this pull request Jun 4, 2026
…2083)

Summary:

Fix deadlock caused by #173568, which stashes a contextvars.Context as a SafePyObject in C++ thread_local storage during every backward() call. When a C++ thread exits, __call_tls_dtors destroys the thread_local and the SafePyObject destructor tries to acquire the GIL — deadlocking if the GIL holder is blocked on that thread (as in UP's GpuThreadGroup).

The old cleanup (_stash_obj_in_tls("context", None)) replaced the entry with a SafePyObject(None) but still left a SafePyObject in the map. This adds _remove_obj_from_tls which erases the key entirely, so no SafePyObject survives to thread exit.

Test Plan:
Green signals

test_correctness_fsdp_semi_frozen and test_correctness_one_pp_clip_mixed under UP tests passing locally

Reviewed By: malfet

Differential Revision: D103314406
@meta-codesync meta-codesync Bot force-pushed the export-D103314406 branch from f997a4f to 6b9bc6e Compare June 4, 2026 18:19
meta-codesync Bot pushed a commit that referenced this pull request Jun 4, 2026
…2083)

Summary:

Fix deadlock caused by #173568, which stashes a contextvars.Context as a SafePyObject in C++ thread_local storage during every backward() call. When a C++ thread exits, __call_tls_dtors destroys the thread_local and the SafePyObject destructor tries to acquire the GIL — deadlocking if the GIL holder is blocked on that thread (as in UP's GpuThreadGroup).

The old cleanup (_stash_obj_in_tls("context", None)) replaced the entry with a SafePyObject(None) but still left a SafePyObject in the map. This adds _remove_obj_from_tls which erases the key entirely, so no SafePyObject survives to thread exit.

Test Plan:
Green signals

test_correctness_fsdp_semi_frozen and test_correctness_one_pp_clip_mixed under UP tests passing locally

Reviewed By: malfet

Differential Revision: D103314406
@meta-codesync meta-codesync Bot force-pushed the export-D103314406 branch from 6b9bc6e to f682352 Compare June 4, 2026 18:21
…2083)

Summary:

Fix deadlock caused by #173568, which stashes a contextvars.Context as a SafePyObject in C++ thread_local storage during every backward() call. When a C++ thread exits, __call_tls_dtors destroys the thread_local and the SafePyObject destructor tries to acquire the GIL — deadlocking if the GIL holder is blocked on that thread (as in UP's GpuThreadGroup).

The old cleanup (_stash_obj_in_tls("context", None)) replaced the entry with a SafePyObject(None) but still left a SafePyObject in the map. This adds _remove_obj_from_tls which erases the key entirely, so no SafePyObject survives to thread exit.

Test Plan:
Green signals

test_correctness_fsdp_semi_frozen and test_correctness_one_pp_clip_mixed under UP tests passing locally

Reviewed By: malfet

Differential Revision: D103314406
@meta-codesync meta-codesync Bot force-pushed the export-D103314406 branch from f682352 to 024f390 Compare June 4, 2026 18:25
@facebook-github-tools

Copy link
Copy Markdown

@pytorchbot merge

(Initiating merge automatically since Phabricator Diff has merged)

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: This PR needs a release notes: label
If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Details for Dev Infra team Raised by workflow job

@atalman atalman added the topic: not user facing topic category label Jun 5, 2026
@atalman

atalman commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator

@pytorchmergebot merge -f "all looks good"

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@github-actions github-actions Bot deleted the export-D103314406 branch July 6, 2026 02:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request fb-exported Merged meta-exported topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants