@@ -212,6 +212,7 @@ def _aks_bastion_restore_shell(shell_cmd):
212212
213213async def _aks_bastion_launch_subshell (kubeconfig_path , port ):
214214 """Launch a subshell with the KUBECONFIG environment variable set to the provided path."""
215+ subshell_process = None
215216 try :
216217 if await _aks_bastion_validate_tunnel (port ):
217218 logger .debug ("Bastion tunnel is set up successfully." )
@@ -243,16 +244,18 @@ async def _aks_bastion_launch_subshell(kubeconfig_path, port):
243244 logger .debug ("Subshell exited with code: %s" , subshell_process .returncode )
244245 except asyncio .CancelledError :
245246 # attempt to terminate the subshell process gracefully
246- if " subshell_process" in locals () :
247+ if subshell_process is not None :
247248 logger .info ("Subshell was cancelled. Terminating..." )
249+ if subshell_process .stdin :
250+ subshell_process .stdin .close ()
248251 subshell_process .terminate ()
249252 try :
250253 await asyncio .wait_for (subshell_process .wait (), timeout = 5 )
251254 logger .info ("Subshell exited cleanly after termination." )
252255 except asyncio .TimeoutError :
253256 logger .warning ("Subshell did not exit after SIGTERM. Sending SIGKILL..." )
254257 subshell_process .kill ()
255- await subshell_process .wait ()
258+ await asyncio . wait_for ( subshell_process .wait (), timeout = 5 )
256259 logger .warning (
257260 "Subshell forcefully killed with code %s" , subshell_process .returncode
258261 )
@@ -264,6 +267,7 @@ async def _aks_bastion_launch_subshell(kubeconfig_path, port):
264267async def _aks_bastion_launch_tunnel (nrg , bastion , port , mc_id ):
265268 """Launch the bastion tunnel using the provided parameters."""
266269
270+ tunnel_proces = None
267271 try :
268272 cmd = (
269273 f"az network bastion tunnel --resource-group { nrg } "
@@ -288,18 +292,20 @@ async def _aks_bastion_launch_tunnel(nrg, bastion, port, mc_id):
288292 logger .error ("Tunnel process stderr: %s" , stderr .decode ("utf-8" ))
289293 except asyncio .CancelledError :
290294 # attempt to terminate the tunnel process gracefully
291- if " tunnel_proces" in locals () :
295+ if tunnel_proces is not None :
292296 logger .info ("Tunnel process was cancelled. Terminating..." )
297+ if tunnel_proces .stdin :
298+ tunnel_proces .stdin .close ()
293299 tunnel_proces .terminate ()
294300 try :
295301 await asyncio .wait_for (tunnel_proces .wait (), timeout = 5 )
296302 logger .info ("Tunnel process exited cleanly after termination." )
297303 except asyncio .TimeoutError :
298304 logger .warning ("Tunnel process did not exit after SIGTERM. Sending SIGKILL..." )
299305 tunnel_proces .kill ()
300- await tunnel_proces .wait ()
306+ await asyncio . wait_for ( tunnel_proces .wait (), timeout = 5 )
301307 logger .warning (
302- "Subshell forcefully killed with code %s" , tunnel_proces .returncode
308+ "Tunnel process forcefully killed with code %s" , tunnel_proces .returncode
303309 )
304310 else :
305311 logger .warning ("Tunnel process was cancelled before it could be launched." )
0 commit comments