Skip to content

[MLIR][Python] Fix bug when garbage collecting a DenseResourceElementsAttribute #149414

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion mlir/lib/Bindings/Python/IRAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,16 @@ class PyDenseIntElementsAttribute
}
};

static bool pythonIsFinalizing() {
#if PY_VERSION_HEX >= 0x030d0000
return Py_IsFinalizing();
#elif PY_VERSION_HEX >= 0x03070000
return _Py_IsFinalizing();
#else
return _Py_Finalizing != nullptr;
#endif
}

class PyDenseResourceElementsAttribute
: public PyConcreteAttribute<PyDenseResourceElementsAttribute> {
public:
Expand Down Expand Up @@ -1474,7 +1484,7 @@ class PyDenseResourceElementsAttribute
// The userData is a Py_buffer* that the deleter owns.
auto deleter = [](void *userData, const void *data, size_t size,
size_t align) {
if (!Py_IsInitialized())
if (!(Py_IsInitialized() || pythonIsFinalizing()))
Py_Initialize();
Py_buffer *ownedView = static_cast<Py_buffer *>(userData);
nb::gil_scoped_acquire gil;
Expand Down
13 changes: 13 additions & 0 deletions mlir/test/python/ir/array_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,3 +617,16 @@ def test_attribute(context, mview):
# CHECK: BACKING MEMORY DELETED
# CHECK: EXIT FUNCTION
print("EXIT FUNCTION")


print("TEST: danglingResource")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR - I'll have a closer look soon - but just heads up this test won't actually "check" anything. Notice the #CHECK comments in the rest of the file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also can you put it into its own function.

Copy link
Contributor Author

@mamanain mamanain Jul 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't check anything specifically but it still will fail since this bug leads to return code of the whole script to be non 0. Without the fix the test fails.

I've tried to put it into a function but I think that in this case it has to be outside of one for bug to replicate. You need a context object to exist when garbage collecting during script termination.

Thank you!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@makslevental hi! Just gently pinging you on this PR. Please let me know if I shouldn't. There is no rush here, the bug is quite mild.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry man I've been caught up - I'll get to it today I promise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries at all

# This error occurs only when there is an alive context with a DenseResourceElementsAttr
# in the end of the program, so we put it here without an encapsulating function.
ctx = Context()

with ctx, Location.unknown():
DenseResourceElementsAttr.get_from_buffer(
memoryview(np.array([1,2,3])),
"some_resource",
RankedTensorType.get((3,), IntegerType.get_signed(32))
)
Loading