Skip to content

gh-158121: Preserve instruction events when line monitoring is disabled - #158456

Open
limadog9 wants to merge 1 commit into
python:mainfrom
limadog9:gh-158121-monitoring-disable
Open

limadog9 wants to merge 1 commit into
python:mainfrom
limadog9:gh-158121-monitoring-disable

Conversation

@limadog9

@limadog9 limadog9 commented Sep 29, 2026 •

Copy link
Copy Markdown

Fixes #158121.

When the last LINE callback returns sys.monitoring.DISABLE, the line metadata is updated to the underlying opcode. Dispatching that saved opcode skips the INSTRUCTION event for the current instruction even though instruction monitoring remains enabled. Check for INSTRUMENTED_INSTRUCTION in the current bytecode before using the saved opcode, while retaining the existing fallback when callbacks change monitoring.

Also defer updating the shared line metadata until every thread-local bytecode copy has been restored. Otherwise, later copies lose their INSTRUCTION wrappers and keep missing events on subsequent calls in free-threaded builds.

Regression tests cover global/local events, the same/separate tools, repeated calls, restart_events(), and two existing worker-thread bytecode copies. The first regression fails on unmodified main in both builds; the threaded regression also fails with only the dispatch fix.

Validation on Windows x64, built from main at 596d923 with this patch:

  • Debug GIL build: 13 relevant test modules passed (1,599 test cases; expected skips).
  • Debug free-threaded build: the same modules plus test_free_threading.test_monitoring passed (1,613 test cases; expected skips).
  • Suites include monitoring, tracing, profiling, pdb/bdb, disassembly, frames, generators, coroutines, async generators, and threading.
  • python_d.exe -m test -R 3:3 test_monitoring passed with no reference leaks.
  • Additional callback reconfiguration and prewarmed worker-thread probes passed in both builds.
  • patchcheck, Ruff, NEWS reStructuredText lint, and git diff --check passed.

The complete CPython test suite and non-Windows builds were not run locally.

@python-cla-bot

python-cla-bot Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sys.monitoring: INSTRUCTION event skipped when another tool's LINE callback returns DISABLE at the same instruction

1 participant