Skip to content

refactor: use sessionmaker().begin() in web and mcp controllers#34281

Merged
asukaminato0721 merged 3 commits intolanggenius:mainfrom
Desel72:refactor/use-sessionmaker-web-controllers
Mar 31, 2026
Merged

refactor: use sessionmaker().begin() in web and mcp controllers#34281
asukaminato0721 merged 3 commits intolanggenius:mainfrom
Desel72:refactor/use-sessionmaker-web-controllers

Conversation

@Desel72
Copy link
Copy Markdown
Contributor

@Desel72 Desel72 commented Mar 30, 2026

Summary

Part of #24245.

Refactor web and MCP controllers to use sessionmaker().begin() instead of Session() with explicit commit(), following SQLAlchemy best practices.

Files changed

  • api/controllers/web/conversation.py
  • api/controllers/web/forgot_password.py
  • api/controllers/web/wraps.py
  • api/controllers/mcp/mcp.py

@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. refactor labels Mar 30, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

No changes detected.

@Desel72
Copy link
Copy Markdown
Contributor Author

Desel72 commented Mar 30, 2026

Hi @asukaminato0721 Do I need to addressing CI test failure?

Copy link
Copy Markdown
Contributor

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

Refactors Web and MCP API controllers to use sessionmaker(...).begin() transaction scoping instead of constructing Session(...) objects with explicit commit(), aligning controller session usage with SQLAlchemy 2.0 session framing guidance.

Changes:

  • Replaced Session(db.engine, ...) context managers with sessionmaker(db.engine, ...).begin() in web and MCP controllers.
  • Removed an explicit session.commit() path in web forgot-password reset flow, relying on the surrounding begin() scope to commit.
  • Simplified MCP request handling session framing for end-user retrieval/creation.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
api/controllers/web/wraps.py Switches JWT decode DB lookups to sessionmaker(...).begin() with expire_on_commit=False.
api/controllers/web/forgot_password.py Updates forgot-password flows to sessionmaker(...).begin() and removes explicit commit from password update helper.
api/controllers/web/conversation.py Uses sessionmaker(db.engine).begin() for conversation list pagination query.
api/controllers/mcp/mcp.py Moves MCP controller session usage to sessionmaker(...).begin() and refactors end-user retrieval/creation session scopes.
Comments suppressed due to low confidence (1)

api/controllers/web/forgot_password.py:88

  • sessionmaker(db.engine).begin() will commit on exit, and with SQLAlchemy defaults that expires loaded ORM instances. Here account is used after the context manager (passed to AccountService.send_reset_password_email), which can trigger DetachedInstanceError when accessing account.email after the commit/close. Either avoid committing for this read-only lookup (e.g., use a plain session context without begin()), or set expire_on_commit=False, or pass primitives (email/id) out of the session instead of the ORM instance.
        with sessionmaker(db.engine).begin() as session:
            account = AccountService.get_account_by_email_with_case_fallback(request_email, session=session)
        token = None
        if account is None:
            raise AuthenticationFailedError()

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@asukaminato0721
Copy link
Copy Markdown
Contributor

Hi @asukaminato0721 Do I need to addressing CI test failure?

for the failed unit test, i suggest just port to container.
and fix the container errs

@Desel72
Copy link
Copy Markdown
Contributor Author

Desel72 commented Mar 30, 2026

@asukaminato0721 Shall I open more PR for migrating unit test to container or just address them in this PR?

@asukaminato0721
Copy link
Copy Markdown
Contributor

open more pr,keep each pr small to easy to review

@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

No changes detected.

@Desel72 Desel72 force-pushed the refactor/use-sessionmaker-web-controllers branch from e97d229 to eb8e0af Compare March 31, 2026 11:57
@Desel72 Desel72 requested a review from laipz8200 as a code owner March 31, 2026 11:57
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Mar 31, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

No changes detected.

@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

No changes detected.

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Mar 31, 2026
@asukaminato0721 asukaminato0721 added this pull request to the merge queue Mar 31, 2026
Merged via the queue into langgenius:main with commit 2c8b47c Mar 31, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer refactor size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants