LudovicRousseau / pyscard

pyscard smartcard library for python
http://pyscard.sourceforge.net/
GNU Lesser General Public License v2.1
380 stars 108 forks source link

Crash when using sys.exit() #40

Closed sigsergv closed 7 years ago

sigsergv commented 7 years ago

When I use sys.exit() in my program it often throws AttributeError exception.

Test program:

#!/usr/bin/env python3

from smartcard.CardType import AnyCardType
from smartcard.CardConnection import CardConnection
from smartcard.CardRequest import CardRequest

from sys import exit

cardtype = AnyCardType()
cardrequest = CardRequest( timeout=1, cardType=cardtype )
cardservice = cardrequest.waitforcard()

cardservice.connection.connect()

exit(0)

Error:

Exception ignored in: <bound method CardService.__del__ of <smartcard.PassThruCardService.PassThruCardService object at 0x101500828>>
Traceback (most recent call last):
  File "/Users/sigsergv/projects/nfc/pyscard-demos/.venv/lib/python3.5/site-packages/pyscard-1.9.5-py3.5-macosx-10.6-intel.egg/smartcard/CardService.py", line 52, in __del__
  File "/Users/sigsergv/projects/nfc/pyscard-demos/.venv/lib/python3.5/site-packages/pyscard-1.9.5-py3.5-macosx-10.6-intel.egg/smartcard/CardConnectionDecorator.py", line 58, in disconnect
  File "/Users/sigsergv/projects/nfc/pyscard-demos/.venv/lib/python3.5/site-packages/pyscard-1.9.5-py3.5-macosx-10.6-intel.egg/smartcard/pcsc/PCSCCardConnection.py", line 154, in disconnect
  File "/Users/sigsergv/projects/nfc/pyscard-demos/.venv/lib/python3.5/site-packages/pyscard-1.9.5-py3.5-macosx-10.6-intel.egg/smartcard/scard/scard.py", line 545, in SCardDisconnect
AttributeError: 'NoneType' object has no attribute 'SCardDisconnect'
Exception ignored in: <bound method PCSCCardConnection.__del__ of <smartcard.pcsc.PCSCCardConnection.PCSCCardConnection object at 0x1015007b8>>
Traceback (most recent call last):
  File "/Users/sigsergv/projects/nfc/pyscard-demos/.venv/lib/python3.5/site-packages/pyscard-1.9.5-py3.5-macosx-10.6-intel.egg/smartcard/pcsc/PCSCCardConnection.py", line 88, in __del__
  File "/Users/sigsergv/projects/nfc/pyscard-demos/.venv/lib/python3.5/site-packages/pyscard-1.9.5-py3.5-macosx-10.6-intel.egg/smartcard/pcsc/PCSCCardConnection.py", line 154, in disconnect
  File "/Users/sigsergv/projects/nfc/pyscard-demos/.venv/lib/python3.5/site-packages/pyscard-1.9.5-py3.5-macosx-10.6-intel.egg/smartcard/scard/scard.py", line 545, in SCardDisconnect
AttributeError: 'NoneType' object has no attribute 'SCardDisconnect'

Looks like the destructor is executing twice when using sys.exit().

LudovicRousseau commented 7 years ago

I was not able to reproduce the problem on Debian using Python3:

$ python3 ./a.py 
Traceback (most recent call last):
  File "./a.py", line 11, in <module>
    cardservice = cardrequest.waitforcard()
  File "/usr/local/lib/python3.4/dist-packages/pyscard-1.9.5-py3.4-linux-x86_64.egg/smartcard/CardRequest.py", line 68, in waitforcard
    return self.pcsccardrequest.waitforcard()
  File "/usr/local/lib/python3.4/dist-packages/pyscard-1.9.5-py3.4-linux-x86_64.egg/smartcard/pcsc/PCSCCardRequest.py", line 208, in waitforcard
    raise CardRequestTimeoutException()
smartcard.Exceptions.CardRequestTimeoutException: Time-out during card request

or on macOS Sierra 10.12 using Python2. I get the same result as above.

I will try later using Python3 on macOS.

What version of PySCard are you using? 1.9.5? What version of macOS are you using? 10.6? This is a quiet old version.

Can you try with a recent version of macOS?

sigsergv commented 7 years ago

I've installed python-3.6.1 and problem is gone. Maybe it is python-3.5 fault, I was able to reproduce it on linux with python3.5:

% ./test.py
Exception ignored in: <bound method CardService.__del__ of <smartcard.PassThruCardService.PassThruCardService object at 0xb7058d0c>>
Traceback (most recent call last):
  File "/home/sigsergv/tmp/test-venv/lib/python3.5/site-packages/smartcard/CardService.py", line 52, in __del__
  File "/home/sigsergv/tmp/test-venv/lib/python3.5/site-packages/smartcard/CardConnectionDecorator.py", line 58, in disconnect
  File "/home/sigsergv/tmp/test-venv/lib/python3.5/site-packages/smartcard/pcsc/PCSCCardConnection.py", line 154, in disconnect
  File "/home/sigsergv/tmp/test-venv/lib/python3.5/site-packages/smartcard/scard/scard.py", line 547, in SCardDisconnect
AttributeError: 'NoneType' object has no attribute 'SCardDisconnect'
Exception ignored in: <bound method PCSCCardConnection.__del__ of <smartcard.pcsc.PCSCCardConnection.PCSCCardConnection object at 0xb7058c8c>>
Traceback (most recent call last):
  File "/home/sigsergv/tmp/test-venv/lib/python3.5/site-packages/smartcard/pcsc/PCSCCardConnection.py", line 88, in __del__
  File "/home/sigsergv/tmp/test-venv/lib/python3.5/site-packages/smartcard/pcsc/PCSCCardConnection.py", line 154, in disconnect
  File "/home/sigsergv/tmp/test-venv/lib/python3.5/site-packages/smartcard/scard/scard.py", line 547, in SCardDisconnect
AttributeError: 'NoneType' object has no attribute 'SCardDisconnect'
sigsergv commented 7 years ago
% pip list |grep pyscard
pyscard (1.9.5)
% dpkg -l python3.5
ii  python3.5                                  3.5.3-1                    i386      
LudovicRousseau commented 7 years ago

I can reproduce the problem now. I need to have a smart card reader connected.

No problem with Python 2.7.9. Problem with Python 3.4.2 and 3.5.3. I have not tried with Python 3.6.

I guess the problem is related to the way the library is unloaded/uninitialized.

LudovicRousseau commented 7 years ago

The solution is to explicitly call the destructor before exiting, or when you no longer need cardservice. Before the exit() add the line:

cardservice = None

I tried to change the PySCard code to cleanly exit. That would add complexity and depends on the order the libraries are unloaded. The solution I propose above is much simpler.

rohankumbhar-01 commented 6 months ago

hi @LudovicRousseau Screenshot from 2024-01-04 18-06-19

can you please check this , i am using python version Python 3.10.12 and Linux Ubuntu

sigsergv commented 6 months ago

It's not crashing on 3.10 and 3.11.

LudovicRousseau commented 6 months ago

@rohankumbhar-01 please create a new issue with complete details.