@@ -745,7 +745,7 @@ def flush(self):
745
745
await writer .drain ()
746
746
747
747
748
- async def remote_cli (unet , prompt , title , background ):
748
+ async def remote_cli (unet , prompt , title , background , remote_wait = False ):
749
749
"""Open a CLI in a new window."""
750
750
try :
751
751
if not unet .cli_sockpath :
@@ -756,7 +756,9 @@ async def remote_cli(unet, prompt, title, background):
756
756
unet .cli_sockpath = sockpath
757
757
logging .info ("server created on :\n %s\n " , sockpath )
758
758
759
- wait_tmux = bool (os .getenv ("TMUX" , "" )) and not sys .stdin .isatty ()
759
+ if remote_wait :
760
+ wait_tmux = bool (os .getenv ("TMUX" , "" ))
761
+ wait_x11 = not wait_tmux and bool (os .getenv ("DISPLAY" , "" ))
760
762
761
763
# Open a new window with a new CLI
762
764
python_path = await unet .async_get_exec_path (["python3" , "python" ])
@@ -778,14 +780,20 @@ async def remote_cli(unet, prompt, title, background):
778
780
if channel is not None :
779
781
Commander .tmux_wait_gen += 1
780
782
781
- unet .run_in_window (cmd , title = title , background = False , wait_for = channel )
783
+ pane_info = unet .run_in_window (
784
+ cmd , title = title , background = False , wait_for = channel
785
+ )
782
786
783
787
if wait_tmux and channel :
784
788
from .base import commander # pylint: disable=import-outside-toplevel
785
789
790
+ logger .debug ("Waiting on TMUX CLI window" )
786
791
await commander .async_cmd_raises (
787
792
[commander .get_exec_path ("tmux" ), "wait" , channel ]
788
793
)
794
+ elif wait_x11 and isinstance (pane_info , subprocess .Popen ):
795
+ logger .debug ("Waiting on xterm CLI process %s" , pane_info )
796
+ await asyncio .to_thread (pane_info .wait )
789
797
except Exception as error :
790
798
logging .error ("cli server: unexpected exception: %s" , error )
791
799
@@ -926,8 +934,22 @@ def cli(
926
934
prompt = None ,
927
935
background = True ,
928
936
):
937
+ # In the case of no tty a remote_cli will be used, and we want it to wait on finish
938
+ # of the spawned cli.py script, otherwise it returns back here and exits async loop
939
+ # which kills the server side CLI socket operation.
940
+ remote_wait = not sys .stdin .isatty ()
941
+
929
942
asyncio .run (
930
- async_cli (unet , histfile , sockpath , force_window , title , prompt , background )
943
+ async_cli (
944
+ unet ,
945
+ histfile ,
946
+ sockpath ,
947
+ force_window ,
948
+ title ,
949
+ prompt ,
950
+ background ,
951
+ remote_wait = remote_wait ,
952
+ )
931
953
)
932
954
933
955
@@ -939,12 +961,14 @@ async def async_cli(
939
961
title = None ,
940
962
prompt = None ,
941
963
background = True ,
964
+ remote_wait = False ,
942
965
):
943
966
if prompt is None :
944
967
prompt = "munet> "
945
968
946
969
if force_window or not sys .stdin .isatty ():
947
- await remote_cli (unet , prompt , title , background )
970
+ await remote_cli (unet , prompt , title , background , remote_wait )
971
+ return
948
972
949
973
if not unet :
950
974
logger .debug ("client-cli using sockpath %s" , sockpath )
0 commit comments