Skip to content

fix(dataset): remove extra Session in _update_external_knowledge_binding (#22263) #22265

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

xueyu888
Copy link

@xueyu888 xueyu888 commented Jul 11, 2025

Important

  1. I have read the contribution guidelines.
  2. There is an associated issue and I have been assigned to it.
  3. Fixes [Bug] Duplicate SQLAlchemy Session in _update_external_knowledge_binding can raise InvalidRequestError #22263

Summary

Remove the redundant Session(db.engine) in
DatasetService._update_external_knowledge_binding.

  • The binding row was queried with a temporary Session, closed (object becomes detached),
    then re-attached to the global db.session for update.
    This adds an unnecessary connection/transaction and can raise
    InvalidRequestError if the row is already present in the identity-map.
  • Now we reuse the surrounding db.session to query and update, ensuring the whole
    update flow stays in one atomic transaction.

No new dependencies introduced.

Screenshots

Before After

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed if there's no linked issue or discussion.
  • I've added/updated tests where reasonable, and kept the change atomic.
  • I've updated documentation if necessary (N/A for this bug fix).
  • I ran dev/reformat (backend) and cd web && npx lint-staged (frontend) with no lint errors.

@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jul 11, 2025
crazywoola
crazywoola previously approved these changes Jul 12, 2025
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jul 12, 2025
@crazywoola
Copy link
Member

Please fix the tests and lint errors.

@xueyu888
Copy link
Author

Please fix the tests and lint errors.

Thanks for the heads-up, @crazywoola.

Re-added from sqlalchemy.orm import Session # noqa: F401 so the unit tests that patch services.dataset_service.Session pass again.

crazywoola
crazywoola previously approved these changes Jul 13, 2025
@crazywoola
Copy link
Member

Please run ./dev/reformat to fix the style errors.

@crazywoola crazywoola removed the lgtm This PR has been approved by a maintainer label Jul 15, 2025
@xueyu888 xueyu888 force-pushed the fix/external-binding-session branch from 376862b to 4cf27c2 Compare July 15, 2025 09:12
@xueyu888 xueyu888 force-pushed the fix/external-binding-session branch from 4cf27c2 to f52d912 Compare July 15, 2025 09:59
@xueyu888
Copy link
Author

@crazywoola

I’ve identified that the remaining failures are due to the old Session patch in two external‑dataset unit tests. I haven’t had time to adjust those tests yet, so I’m going to fix them locally first. Once they pass, I’ll push a new commit and let you know so the checks can run again.

Thanks for your patience—I’ll update you when the revised tests are ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:S This PR changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] Duplicate SQLAlchemy Session in _update_external_knowledge_binding can raise InvalidRequestError
2 participants