-
Notifications
You must be signed in to change notification settings - Fork 1.7k
allow leak and block detection on handlers #5620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d1cda3a
allow leak and block detection on handlers
Lendemor 25ffc2a
optional deps and simpler file tree
Lendemor 7f9e749
avoid repeating imports
Lendemor cd8967b
fix
Lendemor 410cd28
Handle async gen and regular gen
masenf 7c53e27
Merge remote-tracking branch 'origin/main' into lendemor/use_pyleak
masenf 7930ee2
temporarily disable thread leak detection
masenf dcf2c9a
temporarily disable task leak detection
masenf 01ed801
move monitoring to utils folder and fix precommit
Lendemor af7d2b8
adjust default threshold
Lendemor 85f2245
rename decorator
Lendemor ccaba49
prevent nested monitoring
Lendemor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
Lendemor marked this conversation as resolved.
Outdated
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| """Monitoring utilities for Reflex applications.""" | ||
|
|
||
| from reflex.monitoring.pyleak import ( | ||
| is_pyleak_enabled, | ||
| monitor_async, | ||
| monitor_leaks, | ||
| monitor_sync, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "is_pyleak_enabled", | ||
| "monitor_async", | ||
| "monitor_leaks", | ||
| "monitor_sync", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| """PyLeak integration for monitoring event loop blocking and resource leaks in Reflex applications.""" | ||
|
|
||
| import contextlib | ||
| from collections.abc import Callable | ||
|
|
||
| from pyleak import no_event_loop_blocking, no_task_leaks, no_thread_leaks | ||
| from pyleak.base import LeakAction | ||
|
|
||
| from reflex.config import get_config | ||
|
|
||
|
|
||
| def is_pyleak_enabled() -> bool: | ||
| """Check if PyLeak monitoring is enabled and available. | ||
|
|
||
| Returns: | ||
| True if PyLeak monitoring is enabled in config. | ||
| """ | ||
| config = get_config() | ||
| return config.enable_pyleak_monitoring | ||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def monitor_sync(): | ||
| """Sync context manager for PyLeak monitoring. | ||
|
|
||
| Yields: | ||
| None: Context for monitoring sync operations. | ||
| """ | ||
| if not is_pyleak_enabled(): | ||
| yield | ||
| return | ||
|
|
||
| config = get_config() | ||
| action = config.pyleak_action or LeakAction.WARN | ||
|
|
||
| with contextlib.ExitStack() as stack: | ||
| stack.enter_context( | ||
| no_thread_leaks( | ||
| action=action, | ||
| grace_period=config.pyleak_thread_grace_period, | ||
| ) | ||
| ) | ||
| stack.enter_context( | ||
| no_event_loop_blocking( | ||
| action=action, | ||
| threshold=config.pyleak_blocking_threshold, | ||
| ) | ||
| ) | ||
| yield | ||
|
|
||
|
|
||
| @contextlib.asynccontextmanager | ||
| async def monitor_async(): | ||
| """Async context manager for PyLeak monitoring. | ||
|
|
||
| Yields: | ||
| None: Context for monitoring async operations. | ||
| """ | ||
| if not is_pyleak_enabled(): | ||
| yield | ||
| return | ||
|
|
||
| config = get_config() | ||
| action = config.pyleak_action or LeakAction.WARN | ||
|
|
||
| async with contextlib.AsyncExitStack() as stack: | ||
| stack.enter_context( | ||
| no_thread_leaks( | ||
| action=action, | ||
| grace_period=config.pyleak_thread_grace_period, | ||
| ) | ||
| ) | ||
| stack.enter_context( | ||
| no_event_loop_blocking( | ||
| action=action, | ||
| threshold=config.pyleak_blocking_threshold, | ||
| ) | ||
| ) | ||
| await stack.enter_async_context(no_task_leaks(action=action)) | ||
| yield | ||
|
|
||
|
|
||
| def monitor_leaks(): | ||
| """Framework decorator using the monitoring module's context manager. | ||
|
|
||
| Returns: | ||
| Decorator function that applies PyLeak monitoring to sync/async functions. | ||
| """ | ||
| import asyncio | ||
| import functools | ||
|
Lendemor marked this conversation as resolved.
Outdated
|
||
|
|
||
| def decorator(func: Callable): | ||
| if asyncio.iscoroutinefunction(func): | ||
|
|
||
| @functools.wraps(func) | ||
| async def async_wrapper(*args, **kwargs): | ||
| async with monitor_async(): | ||
| return await func(*args, **kwargs) | ||
|
|
||
| return async_wrapper | ||
|
|
||
| @functools.wraps(func) | ||
| def sync_wrapper(*args, **kwargs): | ||
| with monitor_sync(): | ||
| return func(*args, **kwargs) | ||
|
|
||
| return sync_wrapper # pyright: ignore[reportReturnType] | ||
|
|
||
| return decorator | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.