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

Failed to list readers #117

Closed
mbokil opened this issue Aug 21, 2021 · 12 comments
Closed

Failed to list readers #117

mbokil opened this issue Aug 21, 2021 · 12 comments

Comments

@mbokil
Copy link
Contributor

mbokil commented Aug 21, 2021

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;
@LudovicRousseau
Copy link
Owner

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

mbokil commented Aug 23, 2021

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

LudovicRousseau commented Aug 23, 2021

Can you provide a complete sample code?

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

@mbokil
Copy link
Contributor Author

mbokil commented Aug 23, 2021

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

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

Did you removed the reader to get the error?

@mbokil
Copy link
Contributor Author

mbokil commented Aug 23, 2021

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

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)

@mbokil
Copy link
Contributor Author

mbokil commented Aug 24, 2021

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

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

@LudovicRousseau
Copy link
Owner

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 added a commit to LudovicRousseau/pyscard-debug that referenced this issue Aug 25, 2021
This reverts commit d5cf89c.

This code introduced a regression on (at least) macOS,
See LudovicRousseau/pyscard#117
@mbokil
Copy link
Contributor Author

mbokil commented Aug 25, 2021

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

@LudovicRousseau
Copy link
Owner

Thanks for the feedback
Fixed in da43dda

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

No branches or pull requests

2 participants