Skip to content
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

[24.0] Close install model session when request ends #18629

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/galaxy/web/framework/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def __init__(self):
self.transaction_factory = DefaultWebTransaction
# Set if trace logging is enabled
self.trace_logger = None
self.session_factories = []

def add_ui_controller(self, controller_name, controller):
"""
Expand Down Expand Up @@ -170,10 +171,12 @@ def __call__(self, environ, start_response):
path_info = environ.get("PATH_INFO", "")

try:
self._model.set_request_id(request_id) # Start SQLAlchemy session scope
for session_factory in self.session_factories:
session_factory.set_request_id(request_id) # Start SQLAlchemy session scope
return self.handle_request(request_id, path_info, environ, start_response)
finally:
self._model.unset_request_id(request_id) # End SQLAlchemy session scope
for session_factory in self.session_factories:
session_factory.unset_request_id(request_id) # End SQLAlchemy session scope
self.trace(message="Handle request finished")
if self.trace_logger:
self.trace_logger.context_remove("request_id")
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/webapps/base/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def __init__(

# We need this to set the REQUEST_ID contextvar in model.base *BEFORE* a GalaxyWebTransaction is created.
# This will ensure a SQLAlchemy session is request-scoped for legacy (non-fastapi) endpoints.
self._model = galaxy_app.model
self.session_factories.append(galaxy_app.model)

def build_apispec(self):
"""
Expand Down
2 changes: 2 additions & 0 deletions lib/galaxy/webapps/galaxy/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@ async def get_app_with_request_session() -> AsyncGenerator[StructuredApp, None]:
app = get_app()
request_id = request_context.data["X-Request-ID"]
app.model.set_request_id(request_id)
app.install_model.set_request_id(request_id)
try:
yield app
finally:
app.model.unset_request_id(request_id)
app.install_model.unset_request_id(request_id)
mvdbeek marked this conversation as resolved.
Show resolved Hide resolved


DependsOnApp = cast(StructuredApp, Depends(get_app_with_request_session))
Expand Down
8 changes: 8 additions & 0 deletions lib/galaxy/webapps/galaxy/buildapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sys
import threading
import traceback
from typing import Optional
from urllib.parse import urljoin

from paste import httpexceptions
Expand All @@ -20,6 +21,7 @@
import galaxy.webapps.base.webapp
from galaxy import util
from galaxy.security.validate_user_input import VALID_PUBLICNAME_RE
from galaxy.structured_app import MinimalApp
from galaxy.util import asbool
from galaxy.util.properties import load_app_properties
from galaxy.web.framework.middleware.error import ErrorMiddleware
Expand All @@ -34,6 +36,12 @@
class GalaxyWebApplication(galaxy.webapps.base.webapp.WebApplication):
injection_aware = True

def __init__(
self, galaxy_app: MinimalApp, session_cookie: str = "galaxysession", name: Optional[str] = None
) -> None:
super().__init__(galaxy_app, session_cookie, name)
self.session_factories.append(galaxy_app.install_model)


def app_factory(*args, **kwargs):
"""
Expand Down
Loading