Skip to content
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

Need to make class destruction more robust #141

Closed
leofang opened this issue Oct 10, 2024 · 5 comments · Fixed by #246
Closed

Need to make class destruction more robust #141

leofang opened this issue Oct 10, 2024 · 5 comments · Fixed by #246
Assignees
Labels
cuda.core Everything related to the cuda.core module enhancement Any code-related improvements P0 High priority - Must do!

Comments

@leofang
Copy link
Member

leofang commented Oct 10, 2024

Possible solutions include

  1. using weakref.finalize() (as you also suggested)
  2. cythonize everything, make all classes cdef'd, and switch from __del__ to __dealloc__
  3. using a lightweight shutdown detection hack (see, e.g., Memory Leak in libllvmlite.so when using Numba numba/numba#9731 (comment) and numba/llvmlite@7347d77)

Originally posted by @leofang in #87 (comment)

@leofang leofang added enhancement Any code-related improvements P1 Medium priority - Should do labels Oct 10, 2024
@leofang leofang added this to the cuda.core beta 1 milestone Oct 10, 2024
@leofang
Copy link
Member Author

leofang commented Oct 10, 2024

See also #87 (comment). This doesn't seem trivial to me by a curtesy look at the Python atexit docs...

@leofang leofang added the cuda.core Everything related to the cuda.core module label Oct 10, 2024
@leofang leofang added P0 High priority - Must do! and removed P1 Medium priority - Should do labels Oct 15, 2024
@leofang
Copy link
Member Author

leofang commented Oct 15, 2024

Moving this to beta 2 and bumping to P0

@leofang leofang self-assigned this Oct 26, 2024
@leofang leofang added the triage Needs the team's attention label Nov 14, 2024
@rwgk
Copy link
Collaborator

rwgk commented Nov 15, 2024

I spent a few minutes playing with weakref.finalize() and also looked around.

This seems to work great, even with self.close as the weakref.finalize() callback:

import weakref

class ShopKeeper:
    def __init__(self, serno, exception_harness):
        self.serno = serno
        self.exception_harness = exception_harness
        weakref.finalize(self, self.close)

    def close(self):
        if self.exception_harness:
            try:
                print("close", self.serno, 1 / (self.serno - 2))
            except Exception:
                print("problem", self.serno)
        else:
                print("close", self.serno, 1 / (self.serno - 2))

for exception_harness in [False, True]:
    for serno in range(5):
        ShopKeeper(serno, exception_harness)
$ python3 --version
Python 3.10.2
$ python3 ShopKeeper.py
close 4 0.5
close 3 1.0
problem 2
close 1 -1.0
close 0 -0.5
close 4 0.5
close 3 1.0
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/weakref.py", line 667, in _exitfunc
    f()
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/weakref.py", line 591, in __call__
    return info.func(*info.args, **(info.kwargs or {}))
  File "/Users/rgrossekunst/Downloads/ShopKeeper.py", line 16, in close
    print("close", self.serno, 1 / (self.serno - 2))
ZeroDivisionError: division by zero
close 1 -1.0
close 0 -0.5

Is it better than __del__?

Maybe not a lot? — See https://peps.python.org/pep-0442/ (Python 3.4 was released in 2014)

But I also found https://groups.google.com/g/dev-python/c/iFlQm0j5lpU/m/viOe4hQeCAAJ, with postings from 2019/2020.

Quoting for easy reference:

The main thing going for weakref.finalize is that it is called at the beginning of the interpreter shutdown while __del__ may be called much later in the process.

My own conclusion: Replacing __del__ with weakref.finalize() seems to be super easy** and will make the code (slightly) more robust.

** I'm counting only 5 __del__ in cuda_core

@leofang
Copy link
Member Author

leofang commented Nov 15, 2024

Great, @rwgk mind I re-assign this task to you? 😛

@rwgk rwgk self-assigned this Nov 15, 2024
@rwgk
Copy link
Collaborator

rwgk commented Nov 15, 2024

Done :-)

I think this is great for me to learn the cuda-python developer workflow.

@leofang leofang removed their assignment Nov 15, 2024
@leofang leofang removed the triage Needs the team's attention label Nov 15, 2024
@rwgk rwgk closed this as completed in #246 Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cuda.core Everything related to the cuda.core module enhancement Any code-related improvements P0 High priority - Must do!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants