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

Freeze when closing handle from __del__ during python exit #56

Open
Ariakenom opened this issue Jun 27, 2019 · 12 comments
Open

Freeze when closing handle from __del__ during python exit #56

Ariakenom opened this issue Jun 27, 2019 · 12 comments

Comments

@Ariakenom
Copy link

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
@rene-aguirre
Copy link
Owner

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

@Ariakenom
Copy link
Author

Ariakenom commented Jul 24, 2019

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.

@rene-aguirre
Copy link
Owner

Makes sense.

@rene-aguirre rene-aguirre reopened this Jul 26, 2019
@mcuee
Copy link

mcuee commented Aug 12, 2021

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

@mcuee
Copy link

mcuee commented Aug 12, 2021

@Ariakenom Maybe you can test with the latest git and up-to-date Python 3 versions.

@Ariakenom
Copy link
Author

del c is commented in the example

@mcuee
Copy link

mcuee commented Aug 13, 2021

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.

@Ariakenom
Copy link
Author

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.

@mcuee
Copy link

mcuee commented Aug 14, 2021

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>

@mcuee
Copy link

mcuee commented Aug 14, 2021

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

@mcuee
Copy link

mcuee commented Aug 23, 2021

@Ariakenom Maybe you want to try again with more recent Python version to see if there is a real issue or not. Thanks.

@mcuee
Copy link

mcuee commented Aug 23, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants