Skip to content

Commit 00e7709

Browse files
nedbatclaude
andcommitted
fix: keep the SIGINT exit status of a measured subprocess #2362
With `patch = subprocess` on Python 3.13, a child process stopped with SIGINT exited with status 1 instead of dying from the signal. This was a regression in 7.17.1, from 1ade056. SqliteDb reconnects for every outermost `with` block, and each read-write connection ran check_hot_journal(). So the atexit save probed the data file over and over, and the probe's pathlib.Path.as_uri() call during shutdown kept Python 3.13 from re-raising the unhandled KeyboardInterrupt as a SIGINT death. Now each SqliteDb probes only on its first read-write connection. A new data object for the same file still probes, so a hostile journal planted beside an existing data file is still refused. Fixes #2362 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 89e8aef commit 00e7709

3 files changed

Lines changed: 47 additions & 1 deletion

File tree

‎coverage/sqlitedb.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def __init__(
8383
self.filename = filename
8484
self.no_disk = no_disk
8585
self.read_only = read_only
86+
self.journal_checked = False
8687
self.nest = 0
8788
self.con: sqlite3.Connection | None = None
8889

@@ -111,8 +112,12 @@ def _connect(self) -> None:
111112
if not filename.startswith("file:"):
112113
if self.read_only:
113114
filename = read_only_uri(filename)
114-
else:
115+
elif not self.journal_checked:
116+
# We reconnect for every outermost `with` block, so only probe
117+
# the first time: the probe is costly, and it runs during
118+
# interpreter shutdown, when less work is safer. #2362
115119
check_hot_journal(filename)
120+
self.journal_checked = True
116121
try:
117122
# uri=True is needed for memory and read-only URIs, and is harmless
118123
# for plain file names. It also makes ATTACH honor URIs.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- Fix: with ``patch = subprocess`` on Python 3.13, a measured subprocess
2+
stopped with SIGINT exited with status 1 instead of dying from the signal.
3+
This was a regression in 7.17.1, and is now fixed, closing `issue 2362`_.
4+
5+
.. _issue 2362: https://github.com/coveragepy/coveragepy/issues/2362

‎tests/test_process.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,42 @@ def test_patch_subprocess(self) -> None:
15271527
assert line_counts(data)["main.py"] == 3
15281528
assert line_counts(data)["sub.py"] == 3
15291529

1530+
@pytest.mark.skipif(env.WINDOWS, reason="Windows doesn't deliver SIGINT this way")
1531+
def test_patch_subprocess_sigint(self) -> None:
1532+
# A measured subprocess killed by SIGINT should still die from the
1533+
# signal, as it would without coverage. This was a bug on 3.13. #2362
1534+
self.make_file(
1535+
"main.py",
1536+
"""\
1537+
import os, signal, subprocess, sys, time
1538+
proc = subprocess.Popen(
1539+
[sys.executable, "sub.py"], stderr=subprocess.DEVNULL,
1540+
)
1541+
deadline = time.monotonic() + 30
1542+
while not os.path.exists("ready.txt") and time.monotonic() < deadline:
1543+
time.sleep(0.05)
1544+
proc.send_signal(signal.SIGINT)
1545+
print(f"child status: {proc.wait()}")
1546+
""",
1547+
)
1548+
self.make_file(
1549+
"sub.py",
1550+
"""\
1551+
import time
1552+
open("ready.txt", "w").close()
1553+
time.sleep(30)
1554+
""",
1555+
)
1556+
self.make_file(
1557+
".coveragerc",
1558+
"""\
1559+
[run]
1560+
patch = subprocess
1561+
""",
1562+
)
1563+
out = self.run_command("coverage run main.py")
1564+
assert out == f"child status: {-signal.SIGINT}\n"
1565+
15301566
def test_subprocess_with_pth_files(self) -> None:
15311567
# An existing data file should not be read when a subprocess gets
15321568
# measured automatically. Create the data file here with bogus data in

0 commit comments

Comments
 (0)