Skip to content

Conversation

@liranbg
Copy link
Member

@liranbg liranbg commented Dec 11, 2025

📝 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

  • Added get_db_session_async() async context manager for proper database session management in async contexts
  • Refactored _initiate_logs_collection() to use async session context manager instead of manual session handling
  • Improved log collection handling with proper session commit/rollback semantics
  • Added comprehensive unit tests for async session management and log collection functionality
  • Updated run_async_function_with_new_db_session() to support both coroutine functions and sync functions in async contexts
  • Enhanced TimeWindowTracker to work with async session context managers
  • Removed spammy get kfp runs

✅ Checklist

  • I updated the documentation (if applicable)
  • I have tested the changes in this PR
  • I confirmed whether my changes are covered by system tests
    • If yes, I ran all relevant system tests and ensured they passed before submitting this PR
    • I updated existing system tests and/or added new ones if needed to cover my changes
  • If I introduced a deprecation:

🧪 Testing

  • Added comprehensive unit tests in test_collect_runs_logs.py covering:
    • Async session context manager usage in log collection
    • Verification of log collection started with last update time
    • Proper session lifecycle management
  • All existing unit tests pass
  • Manual testing verified proper session commit/rollback behavior

🔗 References


🚨 Breaking Changes?

  • Yes (explain below)
  • No

Copy link

Copilot AI left a 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() and get_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_threadpool calls with mlrun.utils.run_in_threadpool for 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.

Copy link
Member

@TomerShor TomerShor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good overall!

Comment on lines 123 to 130
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,
)
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will update

async def _base_handler(
self,
request: fastapi.Request,
request,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove type hint?

Copy link
Member Author

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

@liranbg liranbg changed the title [API] Improve async function [API] Improve async functions Dec 12, 2025
@liranbg liranbg requested review from TomerShor and Copilot December 14, 2025 09:22
Copy link

Copilot AI left a 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.

@liranbg liranbg merged commit e06bd6e into mlrun:development Dec 14, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants