LudovicRousseau/pyscard

Failed to list readers

mbokil opened this issue · 12 comments

Your system information

  • Operating system used: macOS Catalina
  • pyscard version: 2.0.1
  • Python version: 3.9.6

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;

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?

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.

Can you provide a complete sample code?

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

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

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

Did you removed the reader to get the error?

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.

I suspect the issue to be linked with db52d27

My problems:

  • I don't understand why you get the "Service not available." error if pcsctest does not report the same error
  • I can't reproduce the problem on my macOS (Big Sur)

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)

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

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()

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

Thanks for the feedback
Fixed in da43dda