@@ -1314,11 +1314,20 @@ def test_command_reads_the_terminal_after_a_subprocess_wrote_to_a_slow_pipe(tmp_
13141314 producer = tmp_path / "producer.py"
13151315 producer .write_text ("import sys\n sys.stdout.write('x' * 262144)\n " , encoding = "utf-8" )
13161316 slow = tmp_path / "slow.py"
1317- slow .write_text ("import sys, time\n while sys.stdin.buffer.read(16384):\n time.sleep(0.15)\n " , encoding = "utf-8" )
1317+ slow_pid = tmp_path / "slow.pid"
1318+ slow .write_text (
1319+ "import os, pathlib, sys, time\n "
1320+ f"pathlib.Path({ str (slow_pid )!r} ).write_text(str(os.getpid()))\n "
1321+ "while sys.stdin.buffer.read(16384):\n "
1322+ " time.sleep(0.15)\n " ,
1323+ encoding = "utf-8" ,
1324+ )
13181325 application = tmp_path / "application.py"
1326+ application_pid = tmp_path / "application.pid"
13191327 application .write_text (
1320- "import os, subprocess, sys\n "
1328+ "import os, pathlib, subprocess, sys\n "
13211329 "from cmd2 import Cmd\n "
1330+ f"pathlib.Path({ str (application_pid )!r} ).write_text(str(os.getpid()))\n "
13221331 "class App(Cmd):\n "
13231332 " def do_produce(self, _):\n "
13241333 f" subprocess.run([sys.executable, { str (producer )!r} ], stdout=self.stdout, check=True)\n "
@@ -1376,6 +1385,12 @@ def wait_until(predicate):
13761385 )
13771386 assert "ANSWER=answer" in transcript [start :]
13781387 finally :
1388+ # Killing the session leader leaves cmd2 and the slow consumer running past the end of the test. Coverage would then
1389+ # write their data files after pytest-cov has combined them.
1390+ for pid_file in (application_pid , slow_pid ):
1391+ if pid_file .exists ():
1392+ with contextlib .suppress (ProcessLookupError ):
1393+ os .kill (int (pid_file .read_text ()), signal .SIGKILL )
13791394 os .close (master )
13801395 process .kill ()
13811396 process .wait (timeout = 5 )
0 commit comments