@@ -21,6 +21,7 @@ def __init__(self, namespace: str):
2121 self .port_forward_process = None
2222 self .namespace = namespace
2323 self .stop_event = threading .Event ()
24+ self .output_threads = []
2425
2526 if self .namespace == "astronomy-shop" :
2627 # No NodePort in astronomy shop
@@ -150,26 +151,31 @@ def start_port_forward(self):
150151 # time.sleep(3) # Wait a bit for the port-forward to establish
151152
152153 def stop_port_forward (self ):
153- """Stops the kubectl port-forward command."""
154154 if self .port_forward_process :
155- self .port_forward_process .terminate () # Send SIGTERM
156- self .port_forward_process .wait () # Wait for the process to terminate
157- self .stop_event .set ()
158- print ("Set the stop event." )
159- self .port_forward_process .stdout .close ()
160- self .port_forward_process .stderr .close ()
161- print ("Port forwarding stopped." )
155+ self .stop_event .set () # Signal threads to exit
156+ try :
157+ self .port_forward_process .terminate ()
158+ self .port_forward_process .wait (timeout = 5 )
159+ except Exception as e :
160+ print ("Error terminating port-forward process:" , e )
161+
162+ try :
163+ if self .port_forward_process .stdout :
164+ self .port_forward_process .stdout .close ()
165+ if self .port_forward_process .stderr :
166+ self .port_forward_process .stderr .close ()
167+ except Exception as e :
168+ print ("Error closing process streams:" , e )
169+ self .port_forward_process = None
170+
162171
163172 def cleanup (self ):
164- """Clean up resources."""
165173 self .stop_port_forward ()
166- for thread in threading .enumerate ():
167- if thread != threading .current_thread ():
168- thread .join (timeout = 5 )
169- if thread .is_alive ():
170- print (
171- f"Thread { thread .name } could not be joined and may need to be stopped forcefully."
172- )
174+ for thread in self .output_threads :
175+ thread .join (timeout = 5 )
176+ if thread .is_alive ():
177+ print (f"Thread { thread .name } could not be joined and may need to be stopped forcefully." )
178+ self .output_threads .clear ()
173179 print ("Cleanup completed." )
174180
175181 def get_services (self ) -> list :
0 commit comments