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 #74: plugout the last device will raise an smartcard.Exceptions #93

Closed
wants to merge 1 commit into from

Conversation

leileigong
Copy link

the problem #74 still not resolved yet.
smartcard.Exceptions.ListReadersException: Failed to list readers: -2146435043/8010001D

raise ListReadersException(hresult)
smartcard.Exceptions.ListReadersException: Failed to list readers: ÖÇÄÜż¨×ĘÔ´ąÜŔíĆ÷δÔËĐСŁ (0x-7FEFFFE3) by getReaderNames

…rd.Exceptions.ListReadersException error,-2146435043/8010001D
@coveralls
Copy link

coveralls commented May 16, 2020

Coverage Status

Coverage remained the same at 28.518% when pulling 2e9cb09 on leileigong:master into 203487d on LudovicRousseau:master.

@LudovicRousseau
Copy link
Owner

Can you document why each change is needed?

The problem is Windows specific. I would not like to have regression in Unix.

@LudovicRousseau
Copy link
Owner

I tested on Windows 10 + pyscard 1.9.9 + two sample codes: sample_MonitorCards.py and sample_MonitorReaders.py. I can't reproduce the problem.

  • What version of pyscard are you using?
  • What version of Windows are you using?
  • What is your sample code?

@LudovicRousseau
Copy link
Owner

@leileigong Any comment to add?

Without your feedback I will just close this PR.

@leileigong
Copy link
Author

@LudovicRousseau this bug still exists on pyscard 2.0.0 can you merge my fix commit 2e9cb0

@LudovicRousseau
Copy link
Owner

@leileigong before I fix a problem I need to be able to reproduce it, or at least to understand it.

Please provide a sample code and a detailed sequence of actions to reproduce the problem?

@leileigong
Copy link
Author

@LudovicRousseau
Reproduce the problem see the follow steps.

step 1. run the sample code sample_MonitorCards.py) on WIndows 10.

step2 plugin the smartcard.

step3 then plugout the smartcard, will got an error.

C:\Users\lei\AppData\Local\pypoetry\Cache\virtualenvs\opensrc-MNpuXIpQ-py3.6\Scripts\python.exe E:/opensrc/pyscard/smartcard/Examples/framework/sample_MonitorCards.py
Insert or remove a smartcard in the system.
This program will exit in 10 seconds

+Inserted:  3B B5 11 00 81 31 46 15 56 20 31 2E 30 1E
Traceback (most recent call last):
  File "C:\Users\lei\AppData\Local\pypoetry\Cache\virtualenvs\opensrc-MNpuXIpQ-py3.6\lib\site-packages\smartcard\CardMonitoring.py", line 164, in run
    currentcards = self.cardrequest.waitforcardevent()
  File "C:\Users\lei\AppData\Local\pypoetry\Cache\virtualenvs\opensrc-MNpuXIpQ-py3.6\lib\site-packages\smartcard\CardRequest.py", line 73, in waitforcardevent
    return self.pcsccardrequest.waitforcardevent()
  File "C:\Users\lei\AppData\Local\pypoetry\Cache\virtualenvs\opensrc-MNpuXIpQ-py3.6\lib\site-packages\smartcard\pcsc\PCSCCardRequest.py", line 288, in waitforcardevent
    readernames = self.getReaderNames()
  File "C:\Users\lei\AppData\Local\pypoetry\Cache\virtualenvs\opensrc-MNpuXIpQ-py3.6\lib\site-packages\smartcard\pcsc\PCSCCardRequest.py", line 90, in getReaderNames
    raise ListReadersException(hresult)
smartcard.Exceptions.ListReadersException: Failed to list readers: ÖÇÄÜż¨×ĘÔ´ąÜŔíĆ÷δÔËĐСŁ  (0x-7FEFFFE3)
press Enter to continue

@LudovicRousseau
Copy link
Owner

I installed miniconda3 on Windows 10 20H2. Then I installed PySCard 2.0.0 using pip.
I execute the sample code. But I still do not reproduce your problem.

  1. I plug the smart card reader and insert a smart card.
  2. I run the sample code.
  3. I remove the smart card reader by disconnecting the USB cable.
  4. I get a card removed event
  5. after the end of the 10 seconds timeout I get the "press Enter..." message

It is strange I see a new "smart card inserted" event. But I do NOT get a crash or exception.

>python sample_MonitorCards.py
Insert or remove a smartcard in the system.
This program will exit in 10 seconds

+Inserted:  3B A7 00 40 18 80 65 A2 08 01 01 52
+Inserted:
-Removed:  3B A7 00 40 18 80 65 A2 08 01 01 52
-Removed:
press Enter to continue

Can you also test with miniconda on your side?
Can you test on another Windows 10 computer?
Do you have errors from PC/SC in your systems logs (I don't know how to see that)?

@leileigong
Copy link
Author

@LudovicRousseau
I test multiple Windows 10 computers, all got the same issue.
And no errors got from PC/SC in the systems logs.

How do install and test with miniconda? is it miniconda version 3?

@antonio-fr
Copy link

I guess I face a variant, with a Windows SSH PIV agent in development :
Windows 10, Python3.8 CPython, pyscard 2.0.0
Failed to list readers: The Smart Card Resource Manager has shut down. (0x-7FEFFFE2)

The software handles the card with simply :

cardrequest = CardRequest(timeout=8, cardType=card_atr)
self.cardservice = cardrequest.waitforcard()
self.cardservice.connection.connect()

It happens when the reader is disconnected after a first initial pyscard session. Whatever the disconnection is brutal (during data exchange), gentle (operations finished), or after a nicely deletion of the card connection object, the Windows software can't reconnect to the smartcard and throws this error.
After closing and starting again the Windows software, pyscard can reconnect to the reader. It is like pyscard can't start (or doesn't detect it is down) the card resource manager afterwards, as it does initially on the first card request.

Annoying, since some USB keys as the Yubico act as a full reader and not a single card, and when user removes that, Windows stops the card manager, the source of many issues with pyscard, and this is exacerbated with these kind of USB dongles.

Let me know if you want a separate issue.

@LudovicRousseau
Copy link
Owner

@antonio-fr your code is waiting for a card insertion. Or am I mistaken?
Can you provide a complete sample code? And also the complete exception message you get?

As I wrote I was not able to reproduce the problem on my Windows 10 system.

LudovicRousseau added a commit that referenced this pull request Jun 11, 2021
On Windows, when the last reader is removed the PC/SC manager is
stopped. A PC/SC call will receive SCARD_E_SERVICE_STOPPED.

The idea here is to renew the PC/SC context in getReaderNames().

Thanks to Lemon Gong for the patch
"fix #74: plugout the last device will raise an smartcard.Exceptions #93"
https://github.com/LudovicRousseau/pyscard/pull/93/files

This change should also fix:
- "Plugout an PCSC device will raise ListReadersException(hresult) on windows #74"
  #74
- "Windows smartcard service is started only once #114"
  #114
@LudovicRousseau
Copy link
Owner

I applied 2 of your 3 patches. Thanks and sorry for the delay.

I don't understand what problem the patch for smartcard/CardMonitoring.py is solving.
Can you check again?
Please provide a sample problem to show the problem if it is still present.

@LudovicRousseau
Copy link
Owner

I released version 2.0.1. https://ludovicrousseau.blogspot.com/2021/06/pyscard-201-released.html
If you still have a problem please open an new issue or PR.

@LudovicRousseau
Copy link
Owner

@leileigong I think your change in d5cf89c is bogus.
It creates a regression on macOS. See #117

I reverted the change in my https://github.com/LudovicRousseau/pyscard-debug fork. Can you check on your side the problem on Windows is still fixed?

@LudovicRousseau
Copy link
Owner

I handled the last reader removal in LudovicRousseau/pyscard-debug@5b43ef5
Please test

@LudovicRousseau
Copy link
Owner

Fixed released in version 2.0.2
https://ludovicrousseau.blogspot.com/2021/09/pyscard-202-released.html

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

4 participants