Skip to content

Commit

Permalink
Merge pull request #18629 from mvdbeek/fix_install_model_session_scope
Browse files Browse the repository at this point in the history
[24.0] Close install model session when request ends
  • Loading branch information
mvdbeek authored Aug 5, 2024
2 parents 42829a5 + d20b333 commit f6c8c63
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
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
9 changes: 6 additions & 3 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)


DependsOnApp = cast(StructuredApp, Depends(get_app_with_request_session))
Expand All @@ -118,9 +120,10 @@ def __init__(self, callable, dep_type):
self.galaxy_type_depends = dep_type


def depends(dep_type: Type[T], get_app=get_app) -> T:
def _do_resolve(request: Request):
return get_app().resolve(dep_type)
def depends(dep_type: Type[T], app=get_app_with_request_session) -> T:
async def _do_resolve(request: Request):
async for _dep in app():
yield _dep.resolve(dep_type)

return cast(T, GalaxyTypeDepends(_do_resolve, dep_type))

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
2 changes: 1 addition & 1 deletion lib/tool_shed/webapp/api2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ async def get_app_with_request_session() -> AsyncGenerator[ToolShedApp, None]:


def depends(dep_type: Type[T]) -> T:
return framework_depends(dep_type, get_app=get_app)
return framework_depends(dep_type, app=get_app_with_request_session)


def get_api_user(
Expand Down

0 comments on commit f6c8c63

Please sign in to comment.