LudovicRousseau / pyscard

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

Failed to list readers #117

Closed mbokil closed 2 years ago

mbokil commented 2 years ago

Your system information

Please describe your issue in as much detail as possible:

Describe what you expected should happen. Return list of readers Describe what did happen. I get an error message and script fails to run. Traceback (most recent call last): File "/usr/local/lib/python3.9/site-packages/smartcard/CardMonitoring.py", line 163, in run currentcards = self.cardrequest.waitforcardevent() File "/usr/local/lib/python3.9/site-packages/smartcard/CardRequest.py", line 72, in waitforcardevent return self.pcsccardrequest.waitforcardevent() File "/usr/local/lib/python3.9/site-packages/smartcard/pcsc/PCSCCardRequest.py", line 291, in waitforcardevent readernames = self.getReaderNames() File "/usr/local/lib/python3.9/site-packages/smartcard/pcsc/PCSCCardRequest.py", line 93, in getReaderNames raise ListReadersException(hresult) smartcard.Exceptions.ListReadersException: Failed to list readers: Service not available. (0x-7FEFFFE3)

Steps for reproducing this issue:

  1. Create a simple class that consumes a ReaderObserver
  2. Error occurs running script
  3. Rolling back to pyscard 1.9.9 fixes the error for me
class DeviceObserver(ReaderObserver):
    """A reader observer that is notified when card readers are added/removed"""

    def update(self, observable, actions):
        global key, log

        (addedreaders, removedreaders) = actions
        if addedreaders:
            log("Added readers", addedreaders)
            key = 1;
        if removedreaders:
            log("Removed readers", removedreaders)
            key = -2;
LudovicRousseau commented 2 years ago

First test your smart card reader is visible under macOS. Follow https://ludovicrousseau.blogspot.com/2014/03/level-1-smart-card-support-on-mac-os-x.html

I guess that in your case NO reader was connected. Exact?

mbokil commented 2 years ago

Thanks for getting back to me. There is no problem with the reader. I think you missed my comment ,"Rolling back to pyscard 1.9.9 fixes the error for me." So if I uninstall the current version and install 1.9.9 the reader and code work fine. Seems to have broken after 2.x.

LudovicRousseau commented 2 years ago

Can you provide a complete sample code?

And really run pcsctest when you get the "Service not available" error.

mbokil commented 2 years ago

Sample code file attached as TXT. I extracted it from a larger project but tested it to ensure the error happens. Sometimes I don't get the error but if I stop the script and restart it I will often get the error.

MUSCLE PC/SC Lite Test Program

Testing SCardEstablishContext    : Command successful.
Testing SCardGetStatusChange
Please insert a working reader   : Command successful.
Testing SCardListReaders         : Command successful.
Reader 01: HID Global OMNIKEY 5022 Smart Card Reader
Enter the reader number          :

Added readers ['HID Global OMNIKEY 5022 Smart Card Reader']
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/smartcard/CardMonitoring.py", line 163, in run
    currentcards = self.cardrequest.waitforcardevent()
  File "/usr/local/lib/python3.9/site-packages/smartcard/CardRequest.py", line 72, in waitforcardevent
    return self.pcsccardrequest.waitforcardevent()
  File "/usr/local/lib/python3.9/site-packages/smartcard/pcsc/PCSCCardRequest.py", line 291, in waitforcardevent
    readernames = self.getReaderNames()
  File "/usr/local/lib/python3.9/site-packages/smartcard/pcsc/PCSCCardRequest.py", line 93, in getReaderNames
    raise ListReadersException(hresult)
smartcard.Exceptions.ListReadersException: Failed to list readers: Service not available. (0x-7FEFFFE3)

reader-test.txt

LudovicRousseau commented 2 years ago

What is the result of pcsctest after you get the "Service not available." error?

Did you removed the reader to get the error?

mbokil commented 2 years ago

The result of pcsctest is the same as before the error occurred. It shows command successful for all functions. I didn't remove the reader it has been plugged in all the time. Interestingly if I downgrade to version 1.9.9 the error goes away completely. For now as a workaround we have set the max version to 1.9.9 for mac users.

LudovicRousseau commented 2 years ago

I suspect the issue to be linked with db52d271240568145be43b8d726d8a552c48c18e

My problems:

mbokil commented 2 years ago

Was able to reproduce on Big Sur using the below profile.

macOS BigSur Python 3.9.6 pyscard 2.0.1

Testing SCardEstablishContext : Command successful. Testing SCardGetStatusChange Please insert a working reader : Command successful. Testing SCardListReaders : Command successful. Reader 01: HID Global OMNIKEY 5022 Smart Card Reader

➜ scanner python3 reader-test.py Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/smartcard/CardMonitoring.py", line 163, in run currentcards = self.cardrequest.waitforcardevent() File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/smartcard/CardRequest.py", line 72, in waitforcardevent return self.pcsccardrequest.waitforcardevent() File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/smartcard/pcsc/PCSCCardRequest.py", line 291, in waitforcardevent readernames = self.getReaderNames() File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/smartcard/pcsc/PCSCCardRequest.py", line 93, in getReaderNames raise ListReadersException(hresult) smartcard.Exceptions.ListReadersException: Failed to list readers: Service not available. (0x-7FEFFFE3)

LudovicRousseau commented 2 years ago

I can reproduce the error. I have to wait some time (like 30 seconds) before I get the error.

LudovicRousseau commented 2 years ago

Try with this patch:

--- a/smartcard/pcsc/PCSCContext.py
+++ b/smartcard/pcsc/PCSCContext.py
@@ -52,7 +52,8 @@ class PCSCContext(object):
     def __init__(self):
         PCSCContext.mutex.acquire()
         try:
-            self.renewContext()
+            if not PCSCContext.instance:
+                self.renewContext()
         finally:
             PCSCContext.mutex.release()
mbokil commented 2 years ago

@LudovicRousseau I applied the patch and rebuild using ➜ pyscard-master python3 setup.py build_ext install. Confirming the error is gone now. Thanks!

LudovicRousseau commented 2 years ago

Thanks for the feedback Fixed in https://github.com/LudovicRousseau/pyscard/commit/da43ddac3c3775566d50b9f860d1faa1fccea9b2