LudovicRousseau / pyscard

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

smartcard.System.readers will occasionally fail to list readers #43

Closed vbnmmnbv closed 7 years ago

vbnmmnbv commented 7 years ago

Hi, I am using version 1.9.4 in python2. I will occasionally get this error. I have two smartcards attached. The problem will be solved if I re-import the module. Anyone has any clue or suggestion here? Thanks a lot~

Traceback (most recent call last): ................ card_readers = readers() File "/usr/lib/python2.7/dist-packages/smartcard/System.py", line 42, in readers return smartcard.reader.ReaderFactory.ReaderFactory.readers(groups) File "/usr/lib/python2.7/dist-packages/smartcard/reader/ReaderFactory.py", line 58, in readers zreaders += fm(groups) File "/usr/lib/python2.7/dist-packages/smartcard/pcsc/PCSCReader.py", line 110, in readers for reader in PCSCreaders(hcontext, groups): File "/usr/lib/python2.7/dist-packages/smartcard/pcsc/PCSCReader.py", line 49, in PCSCreaders raise ListReadersException(hresult) ListReadersException: 'Failure to list readers: Service not available.'

LudovicRousseau commented 7 years ago

What OS and version are you using?

vbnmmnbv commented 7 years ago

We have a customized OS. But it's based on Ubuntu 14 LTS.

LudovicRousseau commented 7 years ago

The error "Service not available" indicates that pcscd is not (yet) running.

What version of pcsc-lite do you use?

vbnmmnbv commented 7 years ago

Makes sense. Thanks. Next time when this happened, I will try to confirm.

pcsc-lite version 1.8.14. Copyright (C) 1999-2002 by David Corcoran corcoran@musclecard.com. Copyright (C) 2001-2011 by Ludovic Rousseau ludovic.rousseau@free.fr. Copyright (C) 2003-2004 by Damien Sauveron sauveron@labri.fr. Report bugs to muscle@lists.musclecard.com. Enabled features: Linux x86_64-pc-linux-gnu serial usb libudev usbdropdir=/usr/lib/pcsc/drivers ipcdir=/var/run/pcscd configdir=/etc/reader.conf.d

Do you have any clue why it is not running?

vbnmmnbv commented 7 years ago

En... Thanks for your clue. I do find some "workaround" in our old code, trying to deal with not running of pcscd. Probably I should reuse that :) The code will try to restart pcscd and instantiate 'PCSCContext' if the System.readers() call fails.

LudovicRousseau commented 7 years ago

Do you use systemd to start pcscd automatically? See https://ludovicrousseau.blogspot.fr/2011/11/pcscd-auto-start-using-systemd.html

vbnmmnbv commented 7 years ago

It's not managed by systemd and no one will restart it if I killed it. I talked with the guy who added that workaround code before. He said the problem was not because the pcscd was not running. It's because it was not responding, which seems to be more reasonable as re-importing the smartcard module will solve the problem. So the workaround is to manually restart it. I will use that workaround for now.

LudovicRousseau commented 7 years ago

Do you know why pcscd is not responding?

vbnmmnbv commented 7 years ago

I have no idea. Maybe you can give me some guidance to debug. But I just reproduced it and let me describe the symptom.

I have a daemon which already imports smartcard.System.readers() and it works well most time. But when error happens, the readers() will raise exception as I mentioned before(stacktrace is in my first post). Trying to access the readers again through the daemon will cause the same issue.

However, the pcscd is not dead:

ps aux|grep pcs root 8299 0.0 0.0 331280 2960 ? Sl 14:54 0:00 /usr/sbin/pcscd root 9191 0.0 0.0 10708 2200 pts/10 S+ 15:16 0:00 grep --color=auto pcs

And if I open another ipython, import the module and call readers() there, there is no problem:

In [1]: from smartcard.System import readers

In [2]: readers() Out[2]: ['SCM Microsystems Inc. SCR 3310 [CCID Interface] 00 00']

LudovicRousseau commented 7 years ago

I think the problem is already solved in a more recent version of pcsc-lite. Problems with SCardCancel() have been fixed in 1.4.16 and 1.4.21.

Please confirm upgrading pcsc-lite fixes your problem.

vbnmmnbv commented 7 years ago

I do not quite understand. I am using 1.8.14 and you said the suspected problem has been fixed in 1.4.16. Then which version should I try to upgrade? 1.8.21?

LudovicRousseau commented 7 years ago

Always use the latest version. Unless you have a very good reason not to upgrade.

LudovicRousseau commented 7 years ago

Try again using the latest pcsc-lite. If the bug is still present then open an new issue.