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

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

Closed
leileigong opened this issue Mar 13, 2019 · 13 comments
Closed
Assignees

Comments

@leileigong
Copy link

leileigong commented Mar 13, 2019

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

leileigong commented Mar 13, 2019

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

-2146435042L is 0x8010001E and is SCARD_E_SERVICE_STOPPED

What should be the expected behaviour for you?

@LudovicRousseau LudovicRousseau self-assigned this Mar 13, 2019
@leileigong
Copy link
Author

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

@LudovicRousseau
Copy link
Owner

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

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

@LudovicRousseau
Copy link
Owner

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

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

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

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

@leileigong
Copy link
Author

leileigong commented Mar 15, 2019

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

I expected this behaviour with 2 readers.

I propose you to:

@leileigong
Copy link
Author

OK,thanks,you help a lot.

LudovicRousseau pushed a commit to LudovicRousseau/pyscard-debug that referenced this issue Mar 25, 2019
LudovicRousseau pushed a commit to LudovicRousseau/pyscard-debug that referenced this issue Mar 25, 2019
@LudovicRousseau
Copy link
Owner

Fixed in bf906d3

leileigong pushed a commit to leileigong/pyscard that referenced this issue May 16, 2020
…rd.Exceptions.ListReadersException error,-2146435043/8010001D
LudovicRousseau added a commit that referenced this issue 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
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