LudovicRousseau / pyscard

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

Why does hcontext as returned by PCSCContext.getContext() change everytime on windows? #95

Closed stamfest closed 4 years ago

stamfest commented 4 years ago

I stumbled across the following phenomenon. When running this minimal program:

from smartcard.pcsc.PCSCContext import PCSCContext

print(PCSCContext().getContext())
print(PCSCContext().getContext())

I see differences between Linux and Windows. On Linux the two numbers output are equal. On windows they aren't. When looking at the code, I see that the returned context handle wrapper object is created as a singleton and the handle is created using a call to SCardEstablishContext.. It now makes me nervous that there is a different handle returned on every call to getContext(), especially since I cannot find a call to SCardReleaseContext, indicating that there might be some resource leak because of this. This likely would have a negative effect on long running programs. My question is: What is going on here? And: Is this a problem?

LudovicRousseau commented 4 years ago

Exact. I will have a look.

LudovicRousseau commented 4 years ago

Note that .releaseContext() is called internally https://github.com/LudovicRousseau/pyscard/blob/master/smartcard/pcsc/PCSCContext.py#L68

I don't find any memory leak.

LudovicRousseau commented 4 years ago

I also tried again with Python 3.8 and the latest build (from today) and I can't reproduce the problem. I don't know why the code provided in the previous Windows package version was incorrect. Very strange. https://ci.appveyor.com/project/LudovicRousseau/pyscard/build/job/mog60l88k1o6mwlq/artifacts

I now have the same handle.

stamfest commented 4 years ago

Just tried this again using Python 3.8.2 and pyscard-1.9.9 on a win10 machine. Two different handles. I just wonder what's going on here, since the one use of the singleton pattern I see here is to provide one context handle. And I just cannot see the code path that should lead to two invocations of SCardEstablishContext and similarly I cannot see the code path to the .releaseContext method in my super trivial 3-liner.

LudovicRousseau commented 4 years ago

Where did you get pyscard? rebuild from source code?

stamfest commented 4 years ago

I downloaded pyscard-1.9.9.win-amd64-py3.8.exe on June 14th from https://ci.appveyor.com/project/LudovicRousseau/pyscard/

LudovicRousseau commented 4 years ago

Download and install it again.

The package 1.9.9 I used also had not the correct code in https://github.com/LudovicRousseau/pyscard/blob/master/smartcard/pcsc/PCSCContext.py compared to the official repository. The code looks correct now. I have no idea how the generated package could have a wrong code. Maybe appveyor can't be trusted?

stamfest commented 4 years ago

Damn. You are right. The new version does not show this phenomenon. This smells extremely fishy.

stamfest commented 4 years ago

Just to add why I went to appveyor: Other parts of the project require python 3.7 but SF only carries windows packages for 3.6...

LudovicRousseau commented 4 years ago

I added the files for Python 3.7 and 3.8 at https://sourceforge.net/projects/pyscard/files/pyscard/pyscard%201.9.9/

You can also get the binaries from PyPI at https://pypi.org/project/pyscard/#files