LudovicRousseau / pyscard

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

Fix SCARD_E_SERVICE_STOPPED from SCardListReaders #75

Closed Goblenus closed 5 years ago

Goblenus commented 5 years ago

OS: Windows 10 Pro (10.0.17134) Reader (tested on): Athena ASEDrive V3CR, ARDS JaCarta, Aladdin R.D. JaCarta3 Python version: 3.7 pyscard version: 1.9.7

Behavior:

  1. Plug reader to PC (only one!)
  2. Start python interpreter
  3. Execute
    >>> from smartcard.System import readers
  4. Unplug reader
  5. Plug reader
  6. Execute
    >>> readers()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Program Files\Python37\lib\site-packages\smartcard\System.py", line 42, in readers
        return smartcard.reader.ReaderFactory.ReaderFactory.readers(groups)
      File "C:\Program Files\Python37\lib\site-packages\smartcard\reader\ReaderFactory.py", line 58, in readers
        zreaders += fm(groups)
      File "C:\Program Files\Python37\lib\site-packages\smartcard\pcsc\PCSCReader.py", line 110, in readers
        for reader in __PCSCreaders__(hcontext, groups):
      File "C:\Program Files\Python37\lib\site-packages\smartcard\pcsc\PCSCReader.py", line 49, in __PCSCreaders__
        raise ListReadersException(hresult)
    smartcard.pcsc.PCSCExceptions.ListReadersException: 'Failure to list readers: Диспетчер ресурсов смарт-карт завершил работу. '

How it was fixed:

  1. Check for SCARD_E_SERVICE_STOPPED from SCardListReaders
  2. Raise CardServiceStoppedException
  3. Establish new context for PCSCContext
coveralls commented 5 years ago

Coverage Status

Coverage increased (+0.002%) to 24.666% when pulling 4b9363fa012a862afb2c80bc4ecf96a08454cd44 on Goblenus:master into a31fba3412fc100dd84cb38191abf85f0f5bd080 on LudovicRousseau:master.

LudovicRousseau commented 5 years ago

Your patch is nice but not complete.

  1. start python (I use Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32)
  2. Execute
    >>> from smartcard.System import readers
    >>> readers()
    []
  3. plug a reader
  4. Execute
    >>> readers()
    ['reader name']
  5. unplug the reader
  6. Execute
    
    >>> readers()
    Failed to SCardFreeMemory!
    Traceback (most recent call last):
    File "C:\Users\Ludovic\AppData\Local\Programs\Python\Python36\lib\site-packages\smartcard\pcsc\PCSCReader.py", line 113, in readers
    pcsc_readers = __PCSCreaders__(hcontext, groups)
    File "C:\Users\Ludovic\AppData\Local\Programs\Python\Python36\lib\site-packages\smartcard\pcsc\PCSCReader.py", line 49, in __PCSCreaders__
    raise CardServiceStoppedException()
    smartcard.Exceptions.CardServiceStoppedException

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 1, in File "C:\Users\Ludovic\AppData\Local\Programs\Python\Python36\lib\site-packages\smartcard\System.py", line 42, in readers return smartcard.reader.ReaderFactory.ReaderFactory.readers(groups) File "C:\Users\Ludovic\AppData\Local\Programs\Python\Python36\lib\site-packages\smartcard\reader\ReaderFactory.py", line 58, in readers zreaders += fm(groups) File "C:\Users\Ludovic\AppData\Local\Programs\Python\Python36\lib\site-packages\smartcard\pcsc\PCSCReader.py", line 116, in readers pcsc_readers = PCSCreaders(hcontext, groups) File "C:\Users\Ludovic\AppData\Local\Programs\Python\Python36\lib\site-packages\smartcard\pcsc\PCSCReader.py", line 44, in PCSCreaders hresult, readers = SCardListReaders(hcontext, groups) File "C:\Users\Ludovic\AppData\Local\Programs\Python\Python36\lib\site-packages\smartcard\scard\scard.py", line 1067, in SCardListReaders return _scard.SCardListReaders(hcontext, readergroups) TypeError: Expected a python long as SCARDCONTEXT.

Can you fix that? Thanks.

Goblenus commented 5 years ago

It seems I forget to "getContext()" from "PCSCContext.establish_new_context()".

I'll check it on Monday. Is it ok?

LudovicRousseau commented 5 years ago

OK for me. Take your time to provide a correct patch.

LudovicRousseau commented 5 years ago

The problem is that your new method releaseContext() must create a new PC/SC context. Or getContext() will return a now invalid context. __PCSCContextSingleton is no more really a singleton.

I propose to rename establish_new_context() in renewContext()

Goblenus commented 5 years ago

Ok. I done it.

Now I think this is not the best way how I have fixed it, I need some time to think. But it works nice and you can merge it.

LudovicRousseau commented 5 years ago

Merged in https://github.com/LudovicRousseau/pyscard/commit/bf906d3bc00bc929b05a3a10aabcf349138032d6

Thanks