Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Revert "[3.15] gh-156545: Fix flamegraph export RecursionError on dee…
…ply recursive programs (GH-156546) (#158851)"

This reverts commit 0ac7217.
  • Loading branch information
pablogsal committed Oct 5, 2026
commit ebe784edaae37ea849975061f8712227b7e8ffb9
57 changes: 23 additions & 34 deletions Lib/profiling/sampling/stack_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ def export(self, filename):
return True


# Bounded by the unwinder's maximum captured stack depth (MAX_FRAMES).
_FLAMEGRAPH_RECURSION_MARGIN = 2000


class FlamegraphCollector(StackTraceCollector):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down Expand Up @@ -170,41 +166,34 @@ def set_mode(self, mode):
self.stats["mode"] = mode

def export(self, filename):
# Converting the call tree recurses to the sampled stack depth.
old_limit = sys.getrecursionlimit()
sys.setrecursionlimit(old_limit + _FLAMEGRAPH_RECURSION_MARGIN)
try:
flamegraph_data = self._convert_to_flamegraph_format()

# Debug output with string table statistics
num_functions = len(flamegraph_data.get("children", []))
total_time = flamegraph_data.get("value", 0)
string_count = len(self._string_table)
s1 = "" if num_functions == 1 else "s"
s2 = "" if total_time == 1 else "s"
s3 = "" if string_count == 1 else "s"
flamegraph_data = self._convert_to_flamegraph_format()

# Debug output with string table statistics
num_functions = len(flamegraph_data.get("children", []))
total_time = flamegraph_data.get("value", 0)
string_count = len(self._string_table)
s1 = "" if num_functions == 1 else "s"
s2 = "" if total_time == 1 else "s"
s3 = "" if string_count == 1 else "s"
print(
f"Flamegraph data: {num_functions} root function{s1}, "
f"{total_time} total sample{s2}, "
f"{string_count} unique string{s3}"
)

if num_functions == 0:
print(
f"Flamegraph data: {num_functions} root function{s1}, "
f"{total_time} total sample{s2}, "
f"{string_count} unique string{s3}"
"Warning: No functions found in profiling data. Check if sampling captured any data."
)
return False

if num_functions == 0:
print(
"Warning: No functions found in profiling data. "
"Check if sampling captured any data."
)
return False

html_content = self._create_flamegraph_html(flamegraph_data)
html_content = self._create_flamegraph_html(flamegraph_data)

with open(filename, "w", encoding="utf-8") as f:
f.write(html_content)
with open(filename, "w", encoding="utf-8") as f:
f.write(html_content)

print(f"Flamegraph saved to: {filename}")
return True
finally:
sys.setrecursionlimit(old_limit)
print(f"Flamegraph saved to: {filename}")
return True

@staticmethod
@functools.lru_cache(maxsize=None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
import marshal
import opcode
import os
import sys
import tempfile
import unittest

from test.support import is_emscripten, set_recursion_limit
from test.support import is_emscripten

try:
import _remote_debugging # noqa: F401
Expand Down Expand Up @@ -611,38 +610,6 @@ def test_flamegraph_collector_empty_export_fails(self):
self.assertFalse(export_ok)
self.assertEqual(os.path.getsize(flamegraph_out.name), 0)

def test_flamegraph_deep_stack_export(self):
flamegraph_out = tempfile.NamedTemporaryFile(
suffix=".html", delete=False
)
self.addCleanup(close_and_unlink, flamegraph_out)

collector = FlamegraphCollector(1000)
# Deeper than the default recursion limit.
frames = [MockFrameInfo("f.py", i + 1, f"f{i}") for i in range(1536)]
collector.collect(
[MockInterpreterInfo(0, [MockThreadInfo(1, frames)])])

with set_recursion_limit(1000), captured_stdout(), captured_stderr():
export_ok = collector.export(flamegraph_out.name)
self.assertEqual(sys.getrecursionlimit(), 1000)

self.assertTrue(export_ok)
self.assertGreater(os.path.getsize(flamegraph_out.name), 0)

def test_flamegraph_export_restores_recursion_limit(self):
collector = FlamegraphCollector(1000)
frame = MockFrameInfo("f.py", 1, "f")
with set_recursion_limit(500), captured_stdout(), captured_stderr():
self.assertFalse(collector.export(None))
self.assertEqual(sys.getrecursionlimit(), 500)
collector.collect([
MockInterpreterInfo(0, [MockThreadInfo(1, [
frame, MockFrameInfo("f.py", 2, "caller")])])])
with self.assertRaises(TypeError):
collector.export(None)
self.assertEqual(sys.getrecursionlimit(), 500)

def test_gecko_collector_basic(self):
"""Test basic GeckoCollector functionality."""
collector = GeckoCollector(1000)
Expand Down

This file was deleted.