-
Notifications
You must be signed in to change notification settings - Fork 23k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: support removing hook in the hook #61250
Conversation
💊 CI failures summary and remediationsAs of commit e21aecb (more details on the Dr. CI page and at hud.pytorch.org/pr/61250):
Preview docs built from this PR This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.Please report bugs/suggestions to the (internal) Dr. CI Users group. |
@soulitzer have updated the code to use |
Looks pretty good overall, thanks for the fix! Two small comments:
|
Not sure what do we want to update? Should we mention that it is possible to remove hook from the call to hook? Isn't it assumed since the OP actually did it?
Using PyDict_Values which returns a new reference. Have verified the fix locally. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. The behavior is probably expected anyway, so current doc should be OK.
33decf6
to
0b3470d
Compare
@soulitzer has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
@soulitzer merged this pull request in 5e9bcf9. |
Fixes: #58354
Problem:
Once a hook is called
pytorch/torch/csrc/autograd/python_hook.cpp
Lines 51 to 54 in 05c1e5b
If the hook has
handle.remove()
while executing and if there are no references to the hook function object thenpython
is free to garbage collect.At the subsequent call to
pytorch/torch/csrc/autograd/python_hook.cpp
Line 54 in 05c1e5b
we have
hook
pointing to invalid memoryThus when we try to fetch the name for
hook
fromcheck_single_result
withpytorch/torch/csrc/autograd/python_hook.cpp
Lines 175 to 177 in 05c1e5b
we get segfault.
Solution:
Temporarily increase the life-time of hook with
Py_INCREF
till we have verified the result.