LudovicRousseau / pyscard

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

Plugout an PCSC device will raise ListReadersException(hresult) on windows #74

Closed leileigong closed 5 years ago

leileigong commented 5 years ago

I use pyscard-1.9.17, but i got an problem which is similar to several. 1)Exception ListReadersException raised after disconnecting PCSC reader #13 2)Stop CardMonitor monitor thread after traceback print. #22

I guess that #13 #22 do not fixed the problem.

Here is my traceback info:

Traceback (most recent call last): File "C:\Users\lei.virtualenvs\TestTool-gkAj8wo0\lib\site-packages\smartcard\CardMonitoring.py", line 162, in run currentcards = self.cardrequest.waitforcardevent() File "C:\Users\lei.virtualenvs\TestTool-gkAj8wo0\lib\site-packages\smartcard\CardRequest.py", line 73, in waitforcardevent return self.pcsccardrequest.waitforcardevent() File "C:\Users\lei.virtualenvs\TestTool-gkAj8wo0\lib\site-packages\smartcard\pcsc\PCSCCardRequest.py", line 288, in waitforcardevent readernames = self.getReaderNames() File "C:\Users\lei.virtualenvs\TestTool-gkAj8wo0\lib\site-packages\smartcard\pcsc\PCSCCardRequest.py", line 90, in getReaderNames raise ListReadersException(hresult) ListReadersException: ('Failed to list readers', -2146435042L)

leileigong commented 5 years ago

this problem trap on win10 , but work fine on win7 `def getReaderNames(self): """Returns the list or PCSC readers on which to wait for cards."""

    # get inserted readers
    hresult, pcscreaders = SCardListReaders(self.hcontext, [])
    if 0 != hresult and SCARD_E_NO_READERS_AVAILABLE != hresult:
        raise ListReadersException(hresult)`

i use SCardGetErrorMessage print error message: SCardGetErrorMessage(hresult).decode('gbk').encode('utf8')

the error mssage is "smartcard resource manager is closed"("智能卡资源管理器已关闭") . I guess this may be a bug of pyscard on win10.

LudovicRousseau commented 5 years ago

-2146435042L is 0x8010001E and is SCARD_E_SERVICE_STOPPED

What should be the expected behaviour for you?

leileigong commented 5 years ago

After i plug in a device, the pyscard service should not stop.

LudovicRousseau commented 5 years ago

PySCard does not stop, but raises an exception.

When a reader is plugged IN no exception is raised. My question is what should happen when a reader is plugged OUT.

leileigong commented 5 years ago

When a reader is plugged OUT, ScardListReader methord should not raise an exception.But now it raises SCARD_E_SERVICE_STOPPED

LudovicRousseau commented 5 years ago

The exception SCARD_E_SERVICE_STOPPED is not raised by SCardListReaders() but by getReaderNames() https://github.com/LudovicRousseau/pyscard/blob/master/smartcard/pcsc/PCSCCardRequest.py#L84

getReaderNames() returns a list of reader(s). The only way for it to return an error is by throwing an exception.

What should getReaderNames() do instead?

leileigong commented 5 years ago

After reader is pluged OUT, the windows smartcard service does not really stop, so getReaderNames() should not raise an exception, it should return a list of reader(s).

LudovicRousseau commented 5 years ago

I don't know if PC/SC manager really stopped or not. But SCardListReaders() returned SCARD_E_SERVICE_STOPPED. I don't think PySCard should just ignore this error code.

Do you get the error/exception if you have 2 reader connected and disconnect one, or only when you disconnect the last reader?

leileigong commented 5 years ago

i just have one reader contected, if it is plugged OUT, SCardListReaders() should not return an error but return an empty list .

leileigong commented 5 years ago

I got an new tip. If you have 2 reader connected and disconnect one, i do not get an error/exception. But if disconnect the last reader, then SCardListReaders() returned SCARD_E_SERVICE_STOPPED.

LudovicRousseau commented 5 years ago

I expected this behaviour with 2 readers.

I propose you to:

leileigong commented 5 years ago

OK,thanks,you help a lot.

LudovicRousseau commented 5 years ago

Fixed in https://github.com/LudovicRousseau/pyscard/commit/bf906d3bc00bc929b05a3a10aabcf349138032d6