LudovicRousseau / pyscard

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

Quitting Session causes "Exception ignored in" #131

Closed frankmorgner closed 2 years ago

frankmorgner commented 2 years ago

Your system information

Please describe your issue in as much detail as possible:

The following example uses the Session interface to connect to a card. While the script is running, unplug the reader. Consequently, when session.close() is called, this will throw a disconnection error (which is deliberately ignored).

import smartcard
import time

readers = smartcard.System.listReaders()
readernum = 2
reader = readers[readernum]
session = smartcard.Session(reader)
print("remove %s", reader)
time.sleep(5)
try:
    session.close()
except:
    pass

disconnect.txt

However, some background process still has a connection and fails to report this exception properly when the script exits. That's why python reports this exception by printing it on the command line:

Exception ignored in: <function Session.__del__ at 0x7f8027d240d0>
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/smartcard/Session.py", line 85, in __del__
  File "/usr/lib/python3/dist-packages/smartcard/Session.py", line 91, in close
  File "/usr/lib/python3/dist-packages/smartcard/CardConnectionDecorator.py", line 58, in disconnect
  File "/usr/lib/python3/dist-packages/smartcard/pcsc/PCSCCardConnection.py", line 154, in disconnect
smartcard.Exceptions.CardConnectionException: Failed to disconnect: Invalid value given.
Exception ignored in: <function CardService.__del__ at 0x7f8027d04ee0>
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/smartcard/CardService.py", line 52, in __del__
  File "/usr/lib/python3/dist-packages/smartcard/CardConnectionDecorator.py", line 58, in disconnect
  File "/usr/lib/python3/dist-packages/smartcard/pcsc/PCSCCardConnection.py", line 154, in disconnect
smartcard.Exceptions.CardConnectionException: Failed to disconnect: Invalid value given.
Exception ignored in: <function PCSCCardConnection.__del__ at 0x7f8027d8de50>
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/smartcard/pcsc/PCSCCardConnection.py", line 88, in __del__
  File "/usr/lib/python3/dist-packages/smartcard/pcsc/PCSCCardConnection.py", line 154, in disconnect
smartcard.Exceptions.CardConnectionException: Failed to disconnect: Invalid value given.

Printing this backtrace should be avoided. See https://github.com/python/asyncio/issues/423 for a similar problem

Steps for reproducing this issue:

  1. run python disconnect.txt
  2. unplug the connected reader
  3. when script exits, the ignored Exception can be observed
LudovicRousseau commented 2 years ago

Fixed. Thanks