Skip to content

Commit

Permalink
debug: better welcome message for new processes
Browse files Browse the repository at this point in the history
The old one didn't work because pid=0 could have persisted through forks, and
all of the forked children thought they were new instead of forked.
  • Loading branch information
nedbat committed Nov 22, 2023
1 parent 9208fc0 commit 17bc309
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions coverage/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,24 +369,28 @@ def filter(self, text: str) -> str:
class ProcessTracker:
"""Track process creation for debug logging."""
def __init__(self) -> None:
self.pid: int = 0
self.pid: int = os.getpid()
self.did_welcome = False

def filter(self, text: str) -> str:
"""Add a message about how new processes came to be."""
welcome = ""
pid = os.getpid()
if pid != self.pid:
if self.pid == 0:
argv = getattr(sys, "argv", None)
premsg = (
f"New process: {pid=}, executable: {sys.executable!r}\n"
+ f"New process: cmd: {argv!r}\n"
)
if hasattr(os, "getppid"):
premsg += f"New process parent pid: {os.getppid()!r}\n"
else:
premsg = f"New process: forked {self.pid} -> {pid}\n"
if self.pid != pid:
welcome = f"New process: forked {self.pid} -> {pid}\n"
self.pid = pid
return premsg + text
elif not self.did_welcome:
argv = getattr(sys, "argv", None)
welcome = (
f"New process: {pid=}, executable: {sys.executable!r}\n"
+ f"New process: cmd: {argv!r}\n"
)
if hasattr(os, "getppid"):
welcome += f"New process parent pid: {os.getppid()!r}\n"

if welcome:
self.did_welcome = True
return welcome + text
else:
return text

Expand Down

0 comments on commit 17bc309

Please sign in to comment.