Skip to content

Conversation

@QuantumGhost
Copy link
Collaborator

@QuantumGhost QuantumGhost commented Aug 29, 2025

Summary

Closes #24706
Close #24722

The Account._current_tenant object is loaded by a database session (typically db.session) whose lifetime is not aligned
with the Account model instance. This misalignment causes a DetachedInstanceError to be raised when accessing
attributes of Account._current_tenant after the original session has been closed.

To resolve this issue, we now reload the tenant object with expire_on_commit=False, ensuring the tenant remains
accessible even after the session is closed.

Screenshots

Before After
image image

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods

…t_tenant_id

The `Account._current_tenant` object is loaded by a database session
(typically `db.session`) whose lifetime is not aligned with the Account
model instance. This misalignment causes a `DetachedInstanceError` to be
raised when accessing attributes of `Account._current_tenant` after the
original session has been closed.

To resolve this issue, we now reload the tenant object with
`expire_on_commit=False`, ensuring the tenant remains accessible even
after the session is closed.
@QuantumGhost QuantumGhost force-pushed the fix/detached-instance branch from 44c3887 to d80868e Compare August 29, 2025 09:19
laipz8200
laipz8200 previously approved these changes Aug 29, 2025
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Aug 29, 2025
laipz8200
laipz8200 previously approved these changes Aug 29, 2025
@QuantumGhost QuantumGhost marked this pull request as ready for review August 29, 2025 09:56
@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Aug 29, 2025
@QuantumGhost QuantumGhost force-pushed the fix/detached-instance branch from 8032c3e to eec9a02 Compare August 29, 2025 09:57
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Aug 29, 2025
@QuantumGhost QuantumGhost merged commit d9eb1a7 into langgenius:main Aug 29, 2025
7 checks passed
Cluas pushed a commit to Cluas/dify that referenced this pull request Sep 1, 2025
…anggenius#24789)

The `Account._current_tenant` object is loaded by a database session (typically `db.session`) whose lifetime 
is not aligned with the Account model instance. This misalignment causes a `DetachedInstanceError` to be raised
when accessing attributes of `Account._current_tenant` after the original session has been closed.

To resolve this issue, we now reload the tenant object with `expire_on_commit=False`, ensuring the tenant remains
accessible even after the session is closed.
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 size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When calling a Workflow in ChatFlow, an error occurs when the process reaches the Workflow during preview debugging.

2 participants