diff --git a/tests/test_pipeline_job_control.py b/tests/test_pipeline_job_control.py index 0de2090c6..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" @@ -237,6 +246,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 +262,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 +354,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))