Skip to content

Commit 7a41244

Browse files
committed
code: add wait for remote CLI in X11 support
- and only do this when CLI was created non-async, rather than whenever we don't have a tty. Signed-off-by: Christian Hopps <chopps@labn.net>
1 parent f8dbefc commit 7a41244

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

munet/cli.py

+30-5
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ def flush(self):
745745
await writer.drain()
746746

747747

748-
async def remote_cli(unet, prompt, title, background):
748+
async def remote_cli(unet, prompt, title, background, remote_wait=False):
749749
"""Open a CLI in a new window."""
750750
try:
751751
if not unet.cli_sockpath:
@@ -756,7 +756,9 @@ async def remote_cli(unet, prompt, title, background):
756756
unet.cli_sockpath = sockpath
757757
logging.info("server created on :\n%s\n", sockpath)
758758

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", ""))
760762

761763
# Open a new window with a new CLI
762764
python_path = await unet.async_get_exec_path(["python3", "python"])
@@ -778,14 +780,21 @@ async def remote_cli(unet, prompt, title, background):
778780
if channel is not None:
779781
Commander.tmux_wait_gen += 1
780782

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+
)
782786

783787
if wait_tmux and channel:
784788
from .base import commander # pylint: disable=import-outside-toplevel
785789

790+
logger.debug("Waiting on TMUX CLI window")
786791
await commander.async_cmd_raises(
787792
[commander.get_exec_path("tmux"), "wait", channel]
788793
)
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
789798
except Exception as error:
790799
logging.error("cli server: unexpected exception: %s", error)
791800

@@ -926,8 +935,22 @@ def cli(
926935
prompt=None,
927936
background=True,
928937
):
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+
929943
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+
)
931954
)
932955

933956

@@ -939,12 +962,14 @@ async def async_cli(
939962
title=None,
940963
prompt=None,
941964
background=True,
965+
remote_wait=False,
942966
):
943967
if prompt is None:
944968
prompt = "munet> "
945969

946970
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
948973

949974
if not unet:
950975
logger.debug("client-cli using sockpath %s", sockpath)

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "munet"
3-
version = "0.14.13"
3+
version = "0.14.14"
44
description = "A package to facilitate network simulations"
55
authors = ["Christian Hopps <chopps@labn.net>"]
66
license = "GPL-2.0-or-later"

0 commit comments

Comments
 (0)