-
Notifications
You must be signed in to change notification settings - Fork 293
[API] Improve async functions #9039
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
Conversation
…d improve log collection handling
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR improves async function handling and database session lifecycle management by introducing async context managers for database sessions. The main goal is to prevent potential resource leaks and improve code maintainability through proper session management patterns.
Key Changes:
- Added
get_db_session_async()andget_db_session()context managers for automatic session commit/rollback/cleanup - Refactored
run_async_function_with_new_db_session()to support both coroutine functions and sync functions in async contexts using the new context managers - Replaced all
fastapi.concurrency.run_in_threadpoolcalls withmlrun.utils.run_in_threadpoolfor better standardization - Added
run_with_time_window_tracker_sync()to handle synchronous periodic functions with time window tracking
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
server/py/framework/db/session.py |
Introduces async and sync context managers for database sessions; refactors session management functions to use context managers |
server/py/framework/utils/time_window_tracker.py |
Refactors async time window tracker to use new session management; adds sync version of time window tracker function |
server/py/services/api/main.py |
Refactors log collection and monitoring functions to use async/sync context managers; replaces fastapi.concurrency with mlrun.utils; converts _monitor_runs from async to sync with proper session handling |
server/py/services/alerts/main.py |
Converts _generate_events from async to sync to work with new sync time window tracker |
server/py/framework/service/__init__.py |
Updates imports to use mlrun.utils.run_in_threadpool instead of fastapi.concurrency |
server/py/services/api/tests/unit/test_collect_runs_logs.py |
Adds comprehensive test coverage for async session usage in log collection and verification functions |
server/py/framework/tests/unit/db/test_session.py |
New test file with comprehensive tests for async session management functions |
pipeline-adapters/mlrun-pipelines-kfp-v1-8/src/mlrun_pipelines/client.py |
Removes verbose debug logging statements |
pipeline-adapters/mlrun-pipelines-kfp-v1-8/pyproject.toml |
Version bump to 0.6.2 |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
TomerShor
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good overall!
| await framework.db.session.run_async_function_with_new_db_session( | ||
| callback, last_update_time, *args, **kwargs | ||
| ) | ||
| await run_in_threadpool( | ||
| framework.db.session.run_function_with_new_db_session, | ||
| cycle_tracker.update_window, | ||
| now, | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't both of those calls run with the same db session? why do we need a new one for each?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will update
server/py/services/api/main.py
Outdated
| async def _base_handler( | ||
| self, | ||
| request: fastapi.Request, | ||
| request, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why remove type hint?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed the import, I will add for type checking
…indow_update parameter and improve async handling
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 9 out of 9 changed files in this pull request and generated 5 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
📝 Description
This PR addresses ML-11432 by ensuring proper session lifecycle management in async contexts, preventing potential resource leaks and improving code maintainability.
🛠️ Changes Made
get_db_session_async()async context manager for proper database session management in async contexts_initiate_logs_collection()to use async session context manager instead of manual session handlingrun_async_function_with_new_db_session()to support both coroutine functions and sync functions in async contextsTimeWindowTrackerto work with async session context managers✅ Checklist
🧪 Testing
test_collect_runs_logs.pycovering:🔗 References
🚨 Breaking Changes?