|
| 1 | +"""PyLeak integration for monitoring event loop blocking and resource leaks in Reflex applications.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import contextlib |
| 5 | +import functools |
| 6 | +import inspect |
| 7 | +import threading |
| 8 | +from collections.abc import AsyncGenerator, Awaitable, Callable, Generator |
| 9 | +from typing import TypeVar, overload |
| 10 | + |
| 11 | +from reflex.config import get_config |
| 12 | + |
| 13 | +try: |
| 14 | + from pyleak import no_event_loop_blocking, no_task_leaks, no_thread_leaks |
| 15 | + from pyleak.base import LeakAction |
| 16 | + |
| 17 | + PYLEAK_AVAILABLE = True |
| 18 | +except ImportError: |
| 19 | + PYLEAK_AVAILABLE = False |
| 20 | + no_event_loop_blocking = no_task_leaks = no_thread_leaks = None # pyright: ignore[reportAssignmentType] |
| 21 | + LeakAction = None # pyright: ignore[reportAssignmentType] |
| 22 | + |
| 23 | + |
| 24 | +# Thread-local storage to track if monitoring is already active |
| 25 | +_thread_local = threading.local() |
| 26 | + |
| 27 | + |
| 28 | +def is_pyleak_enabled() -> bool: |
| 29 | + """Check if PyLeak monitoring is enabled and available. |
| 30 | +
|
| 31 | + Returns: |
| 32 | + True if PyLeak monitoring is enabled in config and PyLeak is available. |
| 33 | + """ |
| 34 | + if not PYLEAK_AVAILABLE: |
| 35 | + return False |
| 36 | + config = get_config() |
| 37 | + return config.enable_pyleak_monitoring |
| 38 | + |
| 39 | + |
| 40 | +@contextlib.contextmanager |
| 41 | +def monitor_sync(): |
| 42 | + """Sync context manager for PyLeak monitoring. |
| 43 | +
|
| 44 | + Yields: |
| 45 | + None: Context for monitoring sync operations. |
| 46 | + """ |
| 47 | + if not is_pyleak_enabled(): |
| 48 | + yield |
| 49 | + return |
| 50 | + |
| 51 | + # Check if monitoring is already active in this thread |
| 52 | + if getattr(_thread_local, "monitoring_active", False): |
| 53 | + yield |
| 54 | + return |
| 55 | + |
| 56 | + config = get_config() |
| 57 | + action = config.pyleak_action or LeakAction.WARN # pyright: ignore[reportOptionalMemberAccess] |
| 58 | + |
| 59 | + # Mark monitoring as active |
| 60 | + _thread_local.monitoring_active = True |
| 61 | + try: |
| 62 | + with contextlib.ExitStack() as stack: |
| 63 | + # Thread leak detection has issues with background tasks (no_thread_leaks) |
| 64 | + stack.enter_context( |
| 65 | + no_event_loop_blocking( # pyright: ignore[reportOptionalCall] |
| 66 | + action=action, |
| 67 | + threshold=config.pyleak_blocking_threshold, |
| 68 | + ) |
| 69 | + ) |
| 70 | + yield |
| 71 | + finally: |
| 72 | + _thread_local.monitoring_active = False |
| 73 | + |
| 74 | + |
| 75 | +@contextlib.asynccontextmanager |
| 76 | +async def monitor_async(): |
| 77 | + """Async context manager for PyLeak monitoring. |
| 78 | +
|
| 79 | + Yields: |
| 80 | + None: Context for monitoring async operations. |
| 81 | + """ |
| 82 | + if not is_pyleak_enabled(): |
| 83 | + yield |
| 84 | + return |
| 85 | + |
| 86 | + # Check if monitoring is already active in this thread |
| 87 | + if getattr(_thread_local, "monitoring_active", False): |
| 88 | + yield |
| 89 | + return |
| 90 | + |
| 91 | + config = get_config() |
| 92 | + action = config.pyleak_action or LeakAction.WARN # pyright: ignore[reportOptionalMemberAccess] |
| 93 | + |
| 94 | + # Mark monitoring as active |
| 95 | + _thread_local.monitoring_active = True |
| 96 | + try: |
| 97 | + async with contextlib.AsyncExitStack() as stack: |
| 98 | + # Thread leak detection has issues with background tasks (no_thread_leaks) |
| 99 | + # Re-add thread leak later. |
| 100 | + |
| 101 | + # Block detection for event loops |
| 102 | + stack.enter_context( |
| 103 | + no_event_loop_blocking( # pyright: ignore[reportOptionalCall] |
| 104 | + action=action, |
| 105 | + threshold=config.pyleak_blocking_threshold, |
| 106 | + ) |
| 107 | + ) |
| 108 | + # Task leak detection has issues with background tasks (no_task_leaks) |
| 109 | + |
| 110 | + yield |
| 111 | + finally: |
| 112 | + _thread_local.monitoring_active = False |
| 113 | + |
| 114 | + |
| 115 | +YieldType = TypeVar("YieldType") |
| 116 | +SendType = TypeVar("SendType") |
| 117 | +ReturnType = TypeVar("ReturnType") |
| 118 | + |
| 119 | + |
| 120 | +@overload |
| 121 | +def monitor_loopblocks( |
| 122 | + func: Callable[..., AsyncGenerator[YieldType, ReturnType]], |
| 123 | +) -> Callable[..., AsyncGenerator[YieldType, ReturnType]]: ... |
| 124 | + |
| 125 | + |
| 126 | +@overload |
| 127 | +def monitor_loopblocks( |
| 128 | + func: Callable[..., Generator[YieldType, SendType, ReturnType]], |
| 129 | +) -> Callable[..., Generator[YieldType, SendType, ReturnType]]: ... |
| 130 | + |
| 131 | + |
| 132 | +@overload |
| 133 | +def monitor_loopblocks( |
| 134 | + func: Callable[..., Awaitable[ReturnType]], |
| 135 | +) -> Callable[..., Awaitable[ReturnType]]: ... |
| 136 | + |
| 137 | + |
| 138 | +def monitor_loopblocks(func: Callable) -> Callable: |
| 139 | + """Framework decorator using the monitoring module's context manager. |
| 140 | +
|
| 141 | + Args: |
| 142 | + func: The function to be monitored for leaks. |
| 143 | +
|
| 144 | + Returns: |
| 145 | + Decorator function that applies PyLeak monitoring to sync/async functions. |
| 146 | + """ |
| 147 | + if inspect.isasyncgenfunction(func): |
| 148 | + |
| 149 | + @functools.wraps(func) |
| 150 | + async def async_gen_wrapper(*args, **kwargs): |
| 151 | + async with monitor_async(): |
| 152 | + async for item in func(*args, **kwargs): |
| 153 | + yield item |
| 154 | + |
| 155 | + return async_gen_wrapper |
| 156 | + |
| 157 | + if asyncio.iscoroutinefunction(func): |
| 158 | + |
| 159 | + @functools.wraps(func) |
| 160 | + async def async_wrapper(*args, **kwargs): |
| 161 | + async with monitor_async(): |
| 162 | + return await func(*args, **kwargs) |
| 163 | + |
| 164 | + return async_wrapper |
| 165 | + |
| 166 | + if inspect.isgeneratorfunction(func): |
| 167 | + |
| 168 | + @functools.wraps(func) |
| 169 | + def gen_wrapper(*args, **kwargs): |
| 170 | + with monitor_sync(): |
| 171 | + yield from func(*args, **kwargs) |
| 172 | + |
| 173 | + return gen_wrapper |
| 174 | + |
| 175 | + @functools.wraps(func) |
| 176 | + def sync_wrapper(*args, **kwargs): |
| 177 | + with monitor_sync(): |
| 178 | + return func(*args, **kwargs) |
| 179 | + |
| 180 | + return sync_wrapper # pyright: ignore[reportReturnType] |
0 commit comments