Skip to content

Commit 9fd3a6f

Browse files
authored
gh-159865: Fix profiling.sampling exception mode dropping blocked handlers (#159866)
1 parent 727f716 commit 9fd3a6f

3 files changed

Lines changed: 70 additions & 8 deletions

File tree

‎Lib/profiling/sampling/cli.py‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,15 @@ def _parse_mode(mode_string):
213213
return mode_map[mode_string]
214214

215215

216+
def _skip_idle_for_mode(mode):
217+
"""Return whether collectors should discard idle threads for a mode."""
218+
return mode not in (
219+
PROFILING_MODE_WALL,
220+
PROFILING_MODE_ALL,
221+
PROFILING_MODE_EXCEPTION,
222+
)
223+
224+
216225
def _check_process_died(process):
217226
"""Check if process died and raise an error with stderr if available."""
218227
if process.poll() is None:
@@ -1186,9 +1195,7 @@ def _handle_attach(args):
11861195
)
11871196

11881197
# Determine skip_idle based on mode
1189-
skip_idle = (
1190-
mode != PROFILING_MODE_WALL if mode != PROFILING_MODE_ALL else False
1191-
)
1198+
skip_idle = _skip_idle_for_mode(mode)
11921199

11931200
output_file = None
11941201
if args.format == "binary":
@@ -1294,9 +1301,7 @@ def _handle_run(args):
12941301
)
12951302

12961303
# Determine skip_idle based on mode
1297-
skip_idle = (
1298-
mode != PROFILING_MODE_WALL if mode != PROFILING_MODE_ALL else False
1299-
)
1304+
skip_idle = _skip_idle_for_mode(mode)
13001305

13011306
output_file = None
13021307
if args.format == "binary":
@@ -1344,7 +1349,7 @@ def _handle_live_attach(args, pid):
13441349
mode = _parse_mode(args.mode)
13451350

13461351
# Determine skip_idle based on mode
1347-
skip_idle = mode != PROFILING_MODE_WALL
1352+
skip_idle = _skip_idle_for_mode(mode)
13481353

13491354
# Create live collector with default settings
13501355
collector = LiveStatsCollector(
@@ -1391,7 +1396,7 @@ def _handle_live_run(args):
13911396
mode = _parse_mode(args.mode)
13921397

13931398
# Determine skip_idle based on mode
1394-
skip_idle = mode != PROFILING_MODE_WALL
1399+
skip_idle = _skip_idle_for_mode(mode)
13951400

13961401
# Create live collector with default settings
13971402
collector = LiveStatsCollector(

‎Lib/test/test_profiling/test_sampling_profiler/test_cli.py‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,72 @@
3030
_generate_output_filename,
3131
_handle_output,
3232
_replay_with_reader,
33+
_skip_idle_for_mode,
3334
main,
3435
)
3536
from profiling.sampling.constants import (
3637
PROFILING_MODE_ALL,
3738
PROFILING_MODE_CPU,
39+
PROFILING_MODE_EXCEPTION,
40+
PROFILING_MODE_GIL,
3841
PROFILING_MODE_WALL,
3942
)
4043
from profiling.sampling.errors import SamplingScriptNotFoundError, SamplingModuleNotFoundError, SamplingUnknownProcessError
4144
from profiling.sampling.jsonl_collector import JsonlCollector
4245

4346
class TestSampleProfilerCLI(unittest.TestCase):
47+
def test_skip_idle_mode_mapping(self):
48+
self.assertFalse(_skip_idle_for_mode(PROFILING_MODE_WALL))
49+
self.assertTrue(_skip_idle_for_mode(PROFILING_MODE_CPU))
50+
self.assertTrue(_skip_idle_for_mode(PROFILING_MODE_GIL))
51+
self.assertFalse(_skip_idle_for_mode(PROFILING_MODE_ALL))
52+
self.assertFalse(_skip_idle_for_mode(PROFILING_MODE_EXCEPTION))
53+
# Preserve the previous default for modes unknown to this CLI.
54+
self.assertTrue(_skip_idle_for_mode(999))
55+
56+
@requires_remote_subprocess_debugging()
57+
def test_exception_mode_captures_blocked_handler(self):
58+
script = """
59+
import time
60+
61+
try:
62+
raise ValueError("boom")
63+
except ValueError:
64+
time.sleep(30)
65+
"""
66+
with tempfile.TemporaryDirectory() as temp_dir:
67+
script_path = os.path.join(temp_dir, "blocked_handler.py")
68+
output_path = os.path.join(temp_dir, "output.txt")
69+
with open(script_path, "w", encoding="utf-8") as file:
70+
file.write(script)
71+
72+
result = subprocess.run(
73+
[
74+
sys.executable,
75+
"-m",
76+
"profiling.sampling",
77+
"run",
78+
"--mode",
79+
"exception",
80+
"--collapsed",
81+
"-r",
82+
"1000",
83+
"-d",
84+
"1",
85+
"-o",
86+
output_path,
87+
script_path,
88+
],
89+
capture_output=True,
90+
text=True,
91+
timeout=30,
92+
)
93+
94+
self.assertEqual(result.returncode, 0, result.stderr)
95+
with open(output_path, encoding="utf-8") as file:
96+
output = file.read()
97+
self.assertIn("blocked_handler.py", output)
98+
4499
def _setup_sync_mocks(self, mock_socket, mock_popen):
45100
"""Helper to set up socket and process mocks for coordinator tests."""
46101
# Mock the sync socket with context manager support
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :mod:`profiling.sampling` dropping exception-mode samples when a thread
2+
blocks inside an exception handler.

0 commit comments

Comments
 (0)