@@ -229,18 +229,15 @@ def stop_router():
229229 pid = read_pid_file ()
230230 if not pid :
231231 console .print ("[yellow]MCPRouter is not running.[/]" )
232+ try_clear_share ()
232233 return
233234
234235 # send termination signal
235236 try :
236- config_manager = ConfigManager ()
237- share_config = config_manager .read_share_config ()
238- if share_config .get ("pid" ):
239- console .print ("[green]Disabling share link...[/]" )
240- os .kill (share_config ["pid" ], signal .SIGTERM )
241- config_manager .save_share_config (share_url = None , share_pid = None , api_key = None )
242- console .print ("[bold green]Share link disabled[/]" )
237+ # stop share link first
238+ try_clear_share ()
243239
240+ # kill process
244241 os .kill (pid , signal .SIGTERM )
245242 console .print (f"[bold green]MCPRouter stopped (PID: { pid } )[/]" )
246243
@@ -276,7 +273,14 @@ def router_status():
276273 console .print (f"[bold green]MCPRouter is running[/] at http://{ host } :{ port } (PID: { pid } )" )
277274 share_config = ConfigManager ().read_share_config ()
278275 if share_config .get ("pid" ):
279- console .print (f"[bold green]MCPRouter is sharing[/] at { share_config ['url' ]} (PID: { share_config ['pid' ]} )" )
276+ if not is_process_running (share_config ["pid" ]):
277+ console .print ("[yellow]Share link is not active, cleaning.[/]" )
278+ ConfigManager ().save_share_config (share_url = None , share_pid = None , api_key = None )
279+ console .print ("[green]Share link cleaned[/]" )
280+ else :
281+ console .print (
282+ f"[bold green]MCPRouter is sharing[/] at { share_config ['url' ]} (PID: { share_config ['pid' ]} )"
283+ )
280284 else :
281285 console .print ("[yellow]MCPRouter is not running.[/]" )
282286
@@ -348,6 +352,25 @@ def share(address, profile):
348352 )
349353
350354
355+ def try_clear_share ():
356+ console .print ("[bold yellow]Clearing share config...[/]" )
357+ config_manager = ConfigManager ()
358+ share_config = config_manager .read_share_config ()
359+ if share_config ["url" ]:
360+ try :
361+ console .print ("[bold yellow]Disabling share link...[/]" )
362+ config_manager .save_share_config (share_url = None , share_pid = None , api_key = None )
363+ console .print ("[bold green]Share link disabled[/]" )
364+ if share_config ["pid" ]:
365+ os .kill (share_config ["pid" ], signal .SIGTERM )
366+ except OSError as e :
367+ if e .errno == 3 : # "No such process"
368+ console .print ("[yellow]Share process does not exist, cleaning up share config...[/]" )
369+ config_manager .save_share_config (share_url = None , share_pid = None , api_key = None )
370+ else :
371+ console .print (f"[bold red]Error:[/] Failed to stop share link: { e } " )
372+
373+
351374@router .command ("unshare" )
352375@click .help_option ("-h" , "--help" )
353376def stop_share ():
@@ -365,18 +388,4 @@ def stop_share():
365388 return
366389
367390 # send termination signal
368- try :
369- console .print (f"[bold yellow]Stopping share link at { share_config ['url' ]} (PID: { pid } )...[/]" )
370- os .kill (pid , signal .SIGTERM )
371- console .print (f"[bold green]Share process stopped (PID: { pid } )[/]" )
372-
373- # delete share config
374- config_manager .save_share_config (share_url = None , share_pid = None , api_key = None )
375- except OSError as e :
376- console .print (f"[bold red]Error:[/] Failed to stop share link: { e } " )
377-
378- # if process does not exist, clean up share config
379- if e .errno == 3 : # "No such process"
380- console .print ("[yellow]Share process does not exist, cleaning up share config...[/]" )
381- config_manager .save_share_config (share_url = None , share_pid = None , api_key = None )
382- console .print ("[bold green]Share link disabled[/]" )
391+ try_clear_share ()
0 commit comments