Skip to content

Commit

Permalink
Avoid throwing exception in ClosingTHPObjectPtr (pytorch#109758)
Browse files Browse the repository at this point in the history
Previously, if ClosingTHPObjectPtr was destructed because we
were unwinding the stack from an exception, we would attempt to call
close() which just isn't going to work.  Two fixes:

1. Detect if we're unwinding due to a Python error, and don't try
   to do more Python stuff if so.

2. If close() fails somehow, write an unraisable exception, don't
   try to throw because that will terminate if you're in an
   exception.

Signed-off-by: Edward Z. Yang <[email protected]>

Pull Request resolved: pytorch#109758
Approved by: https://github.com/jansel
  • Loading branch information
ezyang authored and pytorchmergebot committed Sep 21, 2023
1 parent 2cd0b94 commit 0351e20
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion torch/csrc/dynamo/python_compiled_autograd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,15 @@ static PyObject* call_end_capture(PyObject* self, const variable_list& inputs) {
struct ClosingTHPObjectPtr : public THPObjectPtr {
ClosingTHPObjectPtr(PyObject* o) : THPObjectPtr(o) {}
~ClosingTHPObjectPtr() {
if (PyErr_Occurred()) {
// do nothing, do not attempt to close
return;
}
static PyObject* method_name = PyUnicode_InternFromString("close");
check(PyObject_CallMethodNoArgs(get(), method_name));
if (PyObject_CallMethodNoArgs(get(), method_name) == nullptr) {
PyErr_WriteUnraisable(get());
PyErr_Clear();
}
}
};

Expand Down

0 comments on commit 0351e20

Please sign in to comment.