@@ -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,21 @@ 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
+ if hasattr (asyncio , "to_thread" ):
797
+ await asyncio .to_thread (pane_info .wait ) # pylint: disable=no-member
789
798
except Exception as error :
790
799
logging .error ("cli server: unexpected exception: %s" , error )
791
800
@@ -926,8 +935,22 @@ def cli(
926
935
prompt = None ,
927
936
background = True ,
928
937
):
938
+ # In the case of no tty a remote_cli will be used, and we want it to wait on finish
939
+ # of the spawned cli.py script, otherwise it returns back here and exits async loop
940
+ # which kills the server side CLI socket operation.
941
+ remote_wait = not sys .stdin .isatty ()
942
+
929
943
asyncio .run (
930
- async_cli (unet , histfile , sockpath , force_window , title , prompt , background )
944
+ async_cli (
945
+ unet ,
946
+ histfile ,
947
+ sockpath ,
948
+ force_window ,
949
+ title ,
950
+ prompt ,
951
+ background ,
952
+ remote_wait = remote_wait ,
953
+ )
931
954
)
932
955
933
956
@@ -939,12 +962,14 @@ async def async_cli(
939
962
title = None ,
940
963
prompt = None ,
941
964
background = True ,
965
+ remote_wait = False ,
942
966
):
943
967
if prompt is None :
944
968
prompt = "munet> "
945
969
946
970
if force_window or not sys .stdin .isatty ():
947
- await remote_cli (unet , prompt , title , background )
971
+ await remote_cli (unet , prompt , title , background , remote_wait )
972
+ return
948
973
949
974
if not unet :
950
975
logger .debug ("client-cli using sockpath %s" , sockpath )
0 commit comments