From f494e2f4dd95595fd86f4f74e11462a35615586e Mon Sep 17 00:00:00 2001 From: Todd Leonhardt <2812720+tleonhardt@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:33:42 -0400 Subject: [PATCH 1/2] Wait for a running pipeline before Ctrl-Z in the job-control test test_pipeline_stops_with_cmd2_and_returns_terminal sent Ctrl-Z as soon as the pipeline owned the terminal. On macOS the pipeline can still be stopped then: when cmd2 takes the terminal back between lends, a pager blocked reading it gets SIGTTIN for its whole group, and the pipeline's watcher continues the group only once it next runs after the following lend. A Ctrl-Z in that gap is discarded by the processes already stopped, and the watcher's SIGCONT then undoes it for a shell producer, so cmd2 never suspends and the test times out waiting for the outer shell to get the terminal back. Wait until the consumer and pager are running before each Ctrl-Z. Refs #1791 --- tests/test_pipeline_job_control.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/tests/test_pipeline_job_control.py b/tests/test_pipeline_job_control.py index 0de2090c6..8479f302b 100644 --- a/tests/test_pipeline_job_control.py +++ b/tests/test_pipeline_job_control.py @@ -237,6 +237,15 @@ def wait_until(predicate): f"resizes: {resizes}\nterminal size at timeout: {terminal_size()}" ) + def states(*pids: int) -> list[str] | None: + """Each process's state as ps reports it, or None unless every process still exists.""" + pids = tuple(set(pids)) + listing = subprocess.run( + ["ps", "-o", "stat=", "-p", ",".join(map(str, pids))], capture_output=True, text=True, check=False + ) + found = listing.stdout.split() + return found if len(found) == len(pids) else None + def stopped(*pids: int) -> bool: """Whether every process is stopped, not merely deprived of the terminal. @@ -244,12 +253,17 @@ def stopped(*pids: int) -> bool: blocked in a one-byte terminal read is woken by the stop signal and, if a keystroke has arrived by then, consumes it before it stops. Typing has to wait for the whole job. """ - pids = tuple(set(pids)) - listing = subprocess.run( - ["ps", "-o", "stat=", "-p", ",".join(map(str, pids))], capture_output=True, text=True, check=False - ) - states = listing.stdout.split() - return len(states) == len(pids) and all(state.startswith("T") for state in states) + found = states(*pids) + return found is not None and all(state.startswith("T") for state in found) + + def running(*pids: int) -> bool: + """Whether no process is stopped, so that Ctrl-Z can stop each one. + + A process that is stopped already, such as a pager that read the terminal from the + background, discards the SIGTSTP of Ctrl-Z rather than keeping it until it goes on. + """ + found = states(*pids) + return found is not None and not any(state.startswith("T") for state in found) job_group = None pipeline_group = None @@ -331,6 +345,10 @@ def stopped(*pids: int) -> bool: wait_until(lambda: "PAGER_ALIVE\r\n" in transcript[start:]) assert os.tcgetpgrp(master) == pipeline_group for rows in (12, 24) if stop_job else (): + # Owning the terminal is not enough. When cmd2 takes it back between lends, a pager blocked reading it is stopped, + # with the rest of its group, and the pipeline's watcher continues them only some time after the next lend. A + # Ctrl-Z meanwhile is discarded by the processes stopped already, and that continue undoes it for the rest. + wait_until(lambda: running(pipeline_group, pager_process)) send("\x1a") wait_until(lambda: os.tcgetpgrp(master) == process.pid) wait_until(lambda: stopped(job_group, app_pid, pager_process)) From 3a793c248c515b25a4b370ce94bcaf1a0e537ca6 Mon Sep 17 00:00:00 2001 From: Todd Leonhardt <2812720+tleonhardt@users.noreply.github.com> Date: Thu, 29 Oct 2026 23:32:07 -0400 Subject: [PATCH 2/2] Wait for the simulated pager's keys in select() with a wakeup fd The orphaned-job-control variant intermittently timed out waiting for the first PAGER_INTERRUPT. The simulated pager handles SIGINT in Python while blocking in os.read(). A signal that arrives after Python last checked for one, but before the read() system call starts, is recorded without interrupting that read, so the handler only runs once the next key arrives. The test sends Ctrl-C as soon as it sees PAGER_ALIVE, which the pager writes just before it reads again. Wait for keys in select() on the terminal and a signal.set_wakeup_fd() pipe instead. Every handled signal makes the pipe readable, so select() returns even when the signal lands just before it. Fixes #1791 --- tests/test_pipeline_job_control.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/test_pipeline_job_control.py b/tests/test_pipeline_job_control.py index 8479f302b..cf6dcf4bb 100644 --- a/tests/test_pipeline_job_control.py +++ b/tests/test_pipeline_job_control.py @@ -82,7 +82,7 @@ def test_pipeline_stops_with_cmd2_and_returns_terminal( pager_pid = tmp_path / "pager.pid" interrupts = tmp_path / "interrupts" pager.write_text( - "import errno, os, pathlib, signal, sys, termios, tty\n" + "import errno, os, pathlib, select, signal, sys, termios, tty\n" f"if {launcher == 'exec'!r}: assert signal.getsignal(signal.SIGTSTP) == signal.SIG_IGN\n" # Interactive bash leaves TTIN/TTOU ignored when exec replaces it. Give the simulated pager normal terminal-access # stops: EOF can arrive before cmd2 lends it the terminal, and an ignored TTIN makes that read fail with EIO instead of @@ -102,6 +102,12 @@ def test_pipeline_stops_with_cmd2_and_returns_terminal( " return\n" " except termios.error as error:\n" " if error.args[0] != errno.EINTR: raise\n" + # A signal that lands as the pager goes back to a blocking read, after Python last checked for one, does not interrupt + # that read: its handler runs only once the next key arrives. So Ctrl-C sent just after the pager reports a key would go + # unanswered. Wait for keys in select() with a wakeup descriptor instead, which every handled signal makes readable. + " wakeup, wakeup_write = os.pipe()\n" + " os.set_blocking(wakeup_write, False)\n" + " signal.set_wakeup_fd(wakeup_write)\n" # os.write rather than print: a signal handler that uses buffered stdout raises "reentrant call inside # <_io.BufferedWriter>" when the signal lands mid-write, which happens when the job is stopped while still reporting # readiness. @@ -119,6 +125,9 @@ def test_pipeline_stops_with_cmd2_and_returns_terminal( " setcbreak()\n" " os.write(1, b'PAGER_READY\\n')\n" " while True:\n" + " ready = select.select([terminal, wakeup], [], [])[0]\n" + " if wakeup in ready: os.read(wakeup, 4096)\n" + " if terminal not in ready: continue\n" " key = os.read(terminal.fileno(), 1)\n" " if key == b'q': break\n" " if key == b'p': os.write(1, b'PAGER_ALIVE\\n')\n"