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

Fix SCARD_E_SERVICE_STOPPED from SCardListReaders #75

Closed
wants to merge 3 commits into from

Conversation

Goblenus
Copy link

@Goblenus Goblenus commented Mar 21, 2019

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
Copy link

coveralls commented Mar 21, 2019

Coverage Status

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

@LudovicRousseau
Copy link
Owner

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()
[]
  1. plug a reader
  2. Execute
>>> readers()
['reader name']
  1. unplug the reader
  2. 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 "<stdin>", line 1, in <module>
  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
Copy link
Author

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

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

@LudovicRousseau
Copy link
Owner

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

@LudovicRousseau
Copy link
Owner

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
Copy link
Author

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 pushed a commit to LudovicRousseau/pyscard-debug that referenced this pull request Mar 25, 2019
LudovicRousseau pushed a commit to LudovicRousseau/pyscard-debug that referenced this pull request Mar 25, 2019
@LudovicRousseau
Copy link
Owner

Merged in bf906d3

Thanks

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

Successfully merging this pull request may close these issues.

None yet

3 participants