Skip to contentSkip to conversation

KVM test: kvm_subprocess.py: do not start tail thread by default

PATCH New Michael Goldish 1 participant 3 messages Oct 13, 2009 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 KVM test: kvm_subprocess.py: do not start tail thread by default

Start the tail thread only if the user specifies a non-None output_func or termination_func.

---
 client/tests/kvm/kvm_subprocess.py |   14 ++++++++++++--
 1 files changed, 12 insertions(+), 2 deletions(-)
client/tests/kvm/kvm_subprocess.py +12 -2
index 2ac062a..ede8081 100755
@@ -596,9 +596,10 @@ class kvm_tail(kvm_spawn):
         self.output_prefix = output_prefix
 
         # Start the thread in the background
+        self.tail_thread = None
         self.__thread_kill_requested = False
-        self.tail_thread = threading.Thread(None, self._tail)
-        self.tail_thread.start()
+        if termination_func or output_func:
+            self._start_thread()
 
 
     def __getinitargs__(self):
@@ -617,6 +618,8 @@ class kvm_tail(kvm_spawn):
                 Must take a single parameter -- the exit status.
         """
         self.termination_func = termination_func
+        if termination_func and not self.tail_thread:
+            self._start_thread()
 
 
     def set_termination_params(self, termination_params):
@@ -637,6 +640,8 @@ class kvm_tail(kvm_spawn):
                 output from the process.  Must take a single string parameter.
         """
         self.output_func = output_func
+        if output_func and not self.tail_thread:
+            self._start_thread()
 
 
     def set_output_params(self, output_params):
@@ -726,6 +731,11 @@ class kvm_tail(kvm_spawn):
             pass
 
 
+    def _start_thread(self):
+        self.tail_thread = threading.Thread(None, self._tail)
+        self.tail_thread.start()
+
+
     def _join_thread(self):
         # Wait for the tail thread to exit
         if self.tail_thread:
Signature · 1 line
1.5.4.1
#1 lore raw

PATCH 2/3 KVM test: kvm_subprocess.py: use only unbound methods as close() hooks

close() will pass 'self' as a parameter to the hook functions, i.e. it will call hook(self) instead of just hook(), thus allowing the use of unbound methods rather than bound ones.

This allows us to avoid self referencing: if a bound method is used, a reference to it is kept in the class instance, and if the method is bound to the same instance then we have a self-reference that prevents garbage collection.

---
 client/tests/kvm/kvm_subprocess.py |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
client/tests/kvm/kvm_subprocess.py +2 -2
index ede8081..a625315 100755
@@ -490,7 +490,7 @@ class kvm_spawn:
         _wait(self.lock_server_running_filename)
         # Call all cleanup routines
         for hook in self.close_hooks:
-            hook()
+            hook(self)
         # Close reader file descriptors
         for fd in self.reader_fds.values():
             try:
@@ -583,7 +583,7 @@ class kvm_tail(kvm_spawn):
         """
         # Add a reader and a close hook
         self._add_reader("tail")
-        self._add_close_hook(self._join_thread)
+        self._add_close_hook(kvm_tail._join_thread)
 
         # Init the superclass
         kvm_spawn.__init__(self, command, id, echo, linesep)
Signature · 1 line
1.5.4.1
#2 lore raw

PATCH 3/3 KVM test: kvm_subprocess.py: automatically close unreferenced shell sessions

Note that if a session has a tracking thread (i.e. if output_func or termination_func are set to something other than None) then the session will not be garbage collected (it must be closed explicitly by the test).

---
 client/tests/kvm/kvm_subprocess.py |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
client/tests/kvm/kvm_subprocess.py +4 -0
index a625315..859aa2b 100755
@@ -1010,6 +1010,10 @@ class kvm_shell_session(kvm_expect):
                                                    self.status_test_command)
 
 
+    def __del__(self):
+        self.close()
+
+
     def set_prompt(self, prompt):
         """
         Set the prompt attribute for later use by read_up_to_prompt.
Signature · 1 line
1.5.4.1