Skip to content

fix: do not deadlock sysmon debug logging when covering stdlib - #2260

Open
DSeaStar wants to merge 3 commits into
coveragepy:mainfrom
DSeaStar:fix-sysmon-log-reentrancy-2087
Open

fix: do not deadlock sysmon debug logging when covering stdlib#2260
DSeaStar wants to merge 3 commits into
coveragepy:mainfrom
DSeaStar:fix-sysmon-log-reentrancy-2087

Conversation

@DSeaStar

Copy link
Copy Markdown

Fixes #2087.

COVERAGE_SYSMON_LOG=1 writes debug output from log() with ordinary file I/O and a stack walk. Those operations re-enter sys.monitoring while the sysmon core is measuring the standard library, so test_stdlib[True] hangs.

log() now uses a thread-local guard and drops nested calls on the same thread. The first-thread banner is written in the same call instead of recursing into log().

A subprocess regression test runs a cover_pylib=True measurement with COVERAGE_SYSMON_LOG=1 and COVERAGE_CORE=sysmon.

DSeaStar and others added 3 commits August 14, 2026 20:26
COVERAGE_SYSMON_LOG=1 re-entered sys.monitoring from log() file I/O,
which hung test_stdlib[True]. Ignore nested log calls on the same thread.
When COVERAGE_SYSMON_LOG=1, sys.monitoring is replaced by a
LoggingWrapper whose __getattr__ wrapped every attribute access as a
function. Reading constants like COVERAGE_ID then returned a function
instead of the int, so SysMonitor.start() crashed with
"TypeError: '<=' not supported between instances of 'function' and
'int'". Return non-callable attributes unwrapped; only callables are
logged. This is required by the new stdlib-logging test that enables
the debug log (coveragepy#2087).

Signed-off-by: SeaStar Deng <172368758@qq.com>
With COVERAGE_SYSMON_LOG=1, logging a sys.monitoring call inside
SysMonitor.start() (which holds self.lock, added for free-threading in
8b9cecc) triggers PY_START events; the sysmon_py_start callback then
re-enters the same non-reentrant Lock in the same thread and deadlocks.
An RLock lets the callback proceed in the lock-holding thread while
still excluding other threads. Verified with the new
test_sysmon_log_does_not_deadlock_on_stdlib on CPython 3.14.

Signed-off-by: SeaStar Deng <172368758@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

COVERAGE_SYSMON_LOG=1 causes a deadlock in test_stdlib[True]

1 participant