LudovicRousseau / pyscard

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

fix #74: plugout the last device will raise an smartcard.Exceptions #93

Closed leileigong closed 3 years ago

leileigong commented 4 years ago

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

coveralls commented 4 years ago

Coverage Status

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

LudovicRousseau commented 4 years ago

Can you document why each change is needed?

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

LudovicRousseau commented 4 years ago

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.

LudovicRousseau commented 3 years ago

@leileigong Any comment to add?

Without your feedback I will just close this PR.

leileigong commented 3 years ago

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

LudovicRousseau commented 3 years ago

@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 commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

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 commented 2 years ago

@leileigong I think your change in d5cf89cc31b4d4193145b8c554b54583c0a0fb7c is bogus. It creates a regression on macOS. See https://github.com/LudovicRousseau/pyscard/issues/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 commented 2 years ago

I handled the last reader removal in https://github.com/LudovicRousseau/pyscard-debug/commit/5b43ef5006483e43e62f7497ef6ce466a0f67aaf Please test

LudovicRousseau commented 2 years ago

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