pywinusb
pywinusb copied to clipboard
Freeze when closing handle from __del__ during python exit
I tried to add a __del__
method for cleanup but it freezes if python exits while the object is alive.
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
pywinusb 0.4.2
import pywinusb.hid
class C:
def __init__(self,h):
self.h = h
def __del__(self):
print("closing")
self.h.close()
print("closed")
h = pywinusb.hid.HidDeviceFilter().get_devices()[0]
h.open()
c = C(h)
# del c # works if do this before python exits
This is a known Python behavior. Example alternative: https://www.quora.com/Python-My-class-defines-del__-but-it-is-not-called-when-I-delete-the-object
The problem wasn't that the __del__
method doesn't run, that would be ok behavior. It was that the python process doesn't exit, it freezes.
Makes sense.
No issue here under Windows 10 with latest git.
(py39venv) C:\work\hid\pywinusb [master ≡ +1 ~0 -0 !]> python
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pywinusb.hid
>>> class C:
... def __init__(self,h):
... self.h = h
... def __del__(self):
... print("closing")
... self.h.close()
... print("closed")
...
>>> h = pywinusb.hid.HidDeviceFilter().get_devices()[0]
>>> h.open()
>>> c = C(h)
>>> del c
closing
closed
@Ariakenom Maybe you can test with the latest git and up-to-date Python 3 versions.
del c
is commented in the example
del c
is commented in the example
I know. But please look at my run log, I have the 'del c' in the run log.
It's not supposed to be in the run log.
That's why the statement is commented out. With a comment that says that it works as expected if we run the statement. But without it it does not work as expected and we get the freeze.
Thanks for the clarification. But it does not freeze for me.
(py39venv) C:\work\python> python
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pywinusb.hid
>>> class C:
... def __init__(self,h):
... self.h = h
... def __del__(self):
... print("closing")
... self.h.close()
... print("closed")
...
>>> h = pywinusb.hid.HidDeviceFilter().get_devices()[0]
>>> h
HID device (vID=0x046d, pID=0xc534, v=0x2901); Logitech; USB Receiver, Path: \\?\hid#vid_046d&pid_c534&mi_01&col02#7&383a3a17&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}
>>> h.open()
>>> c = C(h)
>>> c
<__main__.C object at 0x0000016ECD850F10>
>>> exit()
closing
closed
(py39venv) C:\work\python>
Without using the interactive session:
(py39venv) C:\work\python> cat .\test_pywinusb.py
import pywinusb.hid
class C:
def __init__(self,h):
self.h = h
def __del__(self):
print("closing")
self.h.close()
print("closed")
h = pywinusb.hid.HidDeviceFilter().get_devices()[0]
h.open()
c = C(h)
(py39venv) C:\work\python> python .\test_pywinusb.py
closing
closed
@Ariakenom Maybe you want to try again with more recent Python version to see if there is a real issue or not. Thanks.
I can reproduce the issue with Python 3.6.8 64bit, Python 3.7.9 64bit and Python 3.8.10 64bit. Only Python 3.9 is okay (tested with Python 3.9.6 64bit).
(py36venv) C:\work\python> python
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(py36venv) C:\work\python> python .\test_pywinusb.py
closing
<hang>
C:\work\python> .\py37venv\Scripts\Activate.ps1
(py37venv) C:\work\python> python
Python 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
(py37venv) C:\work\python> python .\test_pywinusb.py
closing
<hang>
(py38venv) C:\work\python> python
Python 3.8.10 (tags/v3.8.10:3d8993a, May 3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(py38venv) C:\work\python> python .\test_pywinusb.py
closing
<hang>
(py39venv) C:\work\python> python
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
(py39venv) C:\work\python> python .\test_pywinusb.py
closing
closed