-
-
Notifications
You must be signed in to change notification settings - Fork 108
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
rmthread should be cleaned up when the last observer is removed #53
Comments
Your patch looks correct. I do not use |
import threading
import time
import smartcard.CardMonitoring
smartcard.CardMonitoring._START_ON_DEMAND_ = True
if __name__ == "__main__":
# a simple card observer that prints added/removed cards
class printobserver(smartcard.CardMonitoring.CardObserver):
def __init__(self, obsindex):
self.obsindex = obsindex
def update(self, observable, handlers):
addedcards, removedcards = handlers
print("%d - added: %s" % (self.obsindex, str(addedcards)))
print("%d - removed: %s" % (self.obsindex, str(removedcards)))
def show_running_threads():
for thread in threading.enumerate():
print("found", thread)
def run_test(testnum):
observer = printobserver(testnum)
readermonitor.addObserver(observer)
show_running_threads()
time.sleep(3)
readermonitor.deleteObserver(observer)
readermonitor = smartcard.CardMonitoring.CardMonitor()
run_test(1)
print("-"*80)
print("Deleting last observer. There is now nothing using the cardmonitor, but the thread still exists")
show_running_threads()
print("-"*80)
run_test(2) Output without fix:
Output with fix:
Note there is slightly different behaviour here, the Observer gets a callback as though it's been run for the first time. I had to make a slight change to the CardMonitoringThread to get this working as well: def join(self, *args, **kwargs):
if self.instance:
self.instance.join(*args, **kwargs)
CardMonitoringThread.instance = None I'll put up a pull request if you're happy with this behaviour. |
pyscard/smartcard/CardMonitoring.py
Line 116 in 55fc8f1
Currently when the last observer is removed the reference to rmthread is dropped:
Should the thread not be stopped and joined if it is no longer needed? A suggested fix would be:
I'd be happy to make a pull request with this fix if this change is acceptable.
The text was updated successfully, but these errors were encountered: