Skip to content

Flask 3.1.2 upgrade fix by Avoids using current_user in background thread #24290

Merged
laipz8200 merged 5 commits intolanggenius:mainfrom
Eric-Guo:flask_bump
Aug 22, 2025
Merged

Flask 3.1.2 upgrade fix by Avoids using current_user in background thread #24290
laipz8200 merged 5 commits intolanggenius:mainfrom
Eric-Guo:flask_bump

Conversation

@Eric-Guo
Copy link
Copy Markdown
Contributor

@Eric-Guo Eric-Guo commented Aug 21, 2025

Summary

I found new Flask 3.1.2 not working in some Chatflow DSL app and GPT 5 high propose the fix.

Tested manually in below chatflow:

image

Why this fixes it

workflow.tenant_id is always available and correct for encryption/decryption. It avoids touching current_user.current_tenant_id, which was the detached model access in the worker thread.

Status

  • Implemented the change in api/models/workflow.py; linter clean.
  • Cancelling the idea to “reload user in worker” since using workflow.tenant_id is safer and simpler.
  • Fixed DetachedInstanceError by switching env var tenant resolution to workflow.tenant_id in models/workflow.py.
  • Avoids using current_user in background thread; lints pass.

@dosubot dosubot bot added size:XS This PR changes 0-9 lines, ignoring generated files. dependencies Pull requests that update a dependency file labels Aug 21, 2025
@Eric-Guo Eric-Guo marked this pull request as draft August 21, 2025 09:35
@Eric-Guo Eric-Guo changed the title Flask 3.1.2 known break Dify, just trigger CI to see if will fail. Flask 3.1.2 upgrade fix by Avoids using current_user in background thread Aug 22, 2025
@Eric-Guo Eric-Guo marked this pull request as ready for review August 22, 2025 04:44
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Aug 22, 2025
@crazywoola crazywoola requested a review from Copilot August 22, 2025 04:51
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

Updates the Dify backend to be compatible with Flask 3.1.2 by resolving DetachedInstanceError that occurs when accessing current_user in background threads. The fix prevents Flask from trying to refresh detached SQLAlchemy model instances when the session context changes between request and background execution.

  • Replaces current_user access with direct tenant_id usage in workflow environment variable encryption/decryption
  • Updates tool invocation to load users by ID instead of relying on request-bound current_user
  • Adds defensive handling for timezone access in message transformation and agent services

Reviewed Changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
api/models/workflow.py Switches environment variable tenant resolution from current_user to workflow.tenant_id
api/core/tools/workflow_as_tool/tool.py Loads user by ID with fallback stub for background contexts instead of using current_user
api/core/tools/utils/message_transformer.py Wraps timezone access in helper function with safe fallback to UTC
api/core/indexing_runner.py Uses method parameter tenant_id instead of current_user.current_tenant_id
api/services/agent_service.py Adds safe fallback to UTC when current_user.timezone is unavailable
api/pyproject.toml Updates Flask dependency from 3.1.0 to 3.1.2
api/tests/unit_tests/models/test_workflow.py Removes mocking of current_user since it's no longer accessed

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Aug 22, 2025
@laipz8200 laipz8200 merged commit 455f842 into langgenius:main Aug 22, 2025
9 checks passed
@Eric-Guo Eric-Guo deleted the flask_bump branch August 22, 2025 07:13
Gnomeek added a commit to Gnomeek/dify that referenced this pull request Aug 22, 2025
ci

add assertion

docs: format all md files (langgenius#24195)

Signed-off-by: yihong0618 <[email protected]>

hotfix: fix multiple case match syntax (langgenius#24204)

feat: notice of the expire of education verify (langgenius#24210)

Co-authored-by: Copilot <[email protected]>

Feat: Education (langgenius#24208)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

chore: translate i18n files (langgenius#24211)

Co-authored-by: iamjoel <[email protected]>

Httpx example (langgenius#24151)

add tyck tool (currently ignore the error) (langgenius#22592)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

Fix the bug of automatically appending basepath to image resource. (langgenius#24201)

fix: keep idempotent when init AnalyticdbVectorBySql (langgenius#24239)

Co-authored-by: xiaozeyu <[email protected]>

fix: dataset doc-form compatible (langgenius#24177)

Co-authored-by: huangzhuo <[email protected]>

feature: add test containers base tests for saved message service (langgenius#24259)

Mcp support resource discovery (langgenius#24223)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

fix: loop exit condition accepts variables from nodes inside the loop  langgenius#24183: (langgenius#24257)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

feat: show the start time with seconds of the app logs (langgenius#24267)

fix(api):Fix the issue of empty and not empty operations failing in k… (langgenius#24276)

Co-authored-by: liumin <[email protected]>

fix: rollback when AnalyticDB create zhparser failed (langgenius#24260)

Co-authored-by: xiaozeyu <[email protected]>

fix: value_type check failed when updating variables (langgenius#24274)

Co-authored-by: me <[email protected]>

[Test] add unit tests for ProviderConfigEncrypter encrypt/mask/decrypt (langgenius#24280)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

refactor: replace try-except blocks with contextlib.suppress for cleaner exception handling (langgenius#24284)

style: replace `h-[1px]` with `h-px` to unify the writing format of Tailwind CSS (langgenius#24146)

Fix: safe defaults for BaseModel dict fields (langgenius#24098)

Co-authored-by: Yongtao Huang <[email protected]>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

[CHORE]: x: T = None to x: Optional[T] = None (langgenius#24217)

feature: add test containers base tests for tag service (langgenius#24313)

[Test] add unit tests for web_reader_tool.py (langgenius#24309)

Co-authored-by: Yongtao Huang <[email protected]>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

Annotations example (langgenius#24304)

an example of sessionmaker (langgenius#24246)

feat: implement TooltipManager for managing tooltip lifecycle (langgenius#24236)

Co-authored-by: crazywoola <[email protected]>

Flask 3.1.2 upgrade fix by Avoids using current_user in background thread  (langgenius#24290)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

fix: Optimize scrolling experience on plugin page (langgenius#24314) (langgenius#24322)

auto format md files (langgenius#24242)

Feat/chat message image first for agent and advanced_chat APP (langgenius#23796)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

Update knowledge_retrieval_node.py (langgenius#24111)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

example of next(, None) (langgenius#24345)

feat: Add default value support for all workflow start node variable types (langgenius#24129)

Co-authored-by: crazywoola <[email protected]>
Co-authored-by: Copilot <[email protected]>

refactor: simplify repository factory with Django-style import_string (langgenius#24354)

Fix missing database commit in provider update handler (langgenius#24357)

Node search supports model and name search (langgenius#24331)

[Chore/Refactor] except StopIteration -> next( , None)
qiaofenlin pushed a commit to qiaofenlin/dify that referenced this pull request Aug 24, 2025
…read (langgenius#24290)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
ZeroZ-lab pushed a commit to ZeroZ-lab/dify that referenced this pull request Aug 25, 2025
…read (langgenius#24290)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
qiqizjl pushed a commit to qiqizjl/dify that referenced this pull request Aug 27, 2025
…read (langgenius#24290)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
HarryReidx pushed a commit to HarryReidx/dify that referenced this pull request Sep 1, 2025
…read (langgenius#24290)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
(cherry picked from commit 455f842)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Pull requests that update a dependency file 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.

3 participants