Skip to content

Commit 014d5f3

Browse files
committed
Limit stack traces in debug mode to make it faster.
Mirrors asyncio PR: python/cpython#4322
1 parent de9fd79 commit 014d5f3

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

uvloop/cbhandles.pyx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ cdef class Handle:
1212
loop._debug_cb_handles_total += 1
1313
loop._debug_cb_handles_count += 1
1414
if loop._debug:
15-
self._source_traceback = tb_extract_stack(sys_getframe(0))
15+
self._source_traceback = extract_stack()
1616

1717
def __dealloc__(self):
1818
if UVLOOP_DEBUG and self.loop is not None:
@@ -133,7 +133,7 @@ cdef class TimerHandle:
133133
self.closed = 0
134134

135135
if loop._debug:
136-
self._source_traceback = tb_extract_stack(sys_getframe(0))
136+
self._source_traceback = extract_stack()
137137

138138
self.timer = UVTimer.new(
139139
loop, <method_t>self._run, self, delay)
@@ -310,3 +310,22 @@ cdef new_MethodHandle3(Loop loop, str name, method3_t callback, object ctx,
310310
handle.arg4 = arg3
311311

312312
return handle
313+
314+
315+
cdef extract_stack():
316+
"""Replacement for traceback.extract_stack() that only does the
317+
necessary work for asyncio debug mode.
318+
"""
319+
f = sys_getframe()
320+
if f is None:
321+
return
322+
323+
try:
324+
stack = tb_StackSummary.extract(tb_walk_stack(f),
325+
limit=DEBUG_STACK_DEPTH,
326+
lookup_lines=False)
327+
finally:
328+
f = None
329+
330+
stack.reverse()
331+
return stack

uvloop/handles/handle.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ cdef class UVHandle:
113113
self._inited = 1
114114
self._handle.data = <void*>self
115115
if self._loop._debug:
116-
self._source_traceback = tb_extract_stack(sys_getframe(0))
116+
self._source_traceback = extract_stack()
117117
if UVLOOP_DEBUG:
118118
self._loop._debug_uv_handles_total += 1
119119

uvloop/includes/consts.pxi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ DEF FLOW_CONTROL_LOW_WATER = 16384
55

66
DEF DEFAULT_FREELIST_SIZE = 250
77

8+
DEF DEBUG_STACK_DEPTH = 10
9+
810

911
DEF __PROCESS_DEBUG_SLEEP_AFTER_FORK = 1

uvloop/includes/stdlib.pxi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ cdef signal_SIG_DFL = std_signal.SIG_DFL
121121
cdef time_sleep = time.sleep
122122
cdef time_monotonic = time.monotonic
123123

124-
cdef tb_extract_stack = traceback.extract_stack
124+
cdef tb_StackSummary = traceback.StackSummary
125+
cdef tb_walk_stack = traceback.walk_stack
125126
cdef tb_format_list = traceback.format_list
126127

127128
cdef warnings_warn = warnings.warn

0 commit comments

Comments
 (0)