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

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Eric Guo
2025-08-22 14:47:13 +08:00
committed by GitHub
parent 6b01b0b165
commit 455f842785
5 changed files with 10 additions and 29 deletions

View File

@@ -9,7 +9,6 @@ import uuid
from typing import Any, Optional, cast from typing import Any, Optional, cast
from flask import current_app from flask import current_app
from flask_login import current_user
from sqlalchemy.orm.exc import ObjectDeletedError from sqlalchemy.orm.exc import ObjectDeletedError
from configs import dify_config from configs import dify_config
@@ -295,7 +294,7 @@ class IndexingRunner:
text_docs, text_docs,
embedding_model_instance=embedding_model_instance, embedding_model_instance=embedding_model_instance,
process_rule=processing_rule.to_dict(), process_rule=processing_rule.to_dict(),
tenant_id=current_user.current_tenant_id, tenant_id=tenant_id,
doc_language=doc_language, doc_language=doc_language,
preview=True, preview=True,
) )

View File

@@ -7,7 +7,6 @@ from typing import TYPE_CHECKING, Any, Optional, Union
from uuid import uuid4 from uuid import uuid4
import sqlalchemy as sa import sqlalchemy as sa
from flask_login import current_user
from sqlalchemy import DateTime, orm from sqlalchemy import DateTime, orm
from core.file.constants import maybe_file_object from core.file.constants import maybe_file_object
@@ -18,7 +17,6 @@ from core.workflow.constants import CONVERSATION_VARIABLE_NODE_ID, SYSTEM_VARIAB
from core.workflow.nodes.enums import NodeType from core.workflow.nodes.enums import NodeType
from factories.variable_factory import TypeMismatchError, build_segment_with_type from factories.variable_factory import TypeMismatchError, build_segment_with_type
from libs.datetime_utils import naive_utc_now from libs.datetime_utils import naive_utc_now
from libs.helper import extract_tenant_id
from ._workflow_exc import NodeNotFoundError, WorkflowDataError from ._workflow_exc import NodeNotFoundError, WorkflowDataError
@@ -351,8 +349,8 @@ class Workflow(Base):
if self._environment_variables is None: if self._environment_variables is None:
self._environment_variables = "{}" self._environment_variables = "{}"
# Get tenant_id from current_user (Account or EndUser) # Use workflow.tenant_id to avoid relying on request user in background threads
tenant_id = extract_tenant_id(current_user) tenant_id = self.tenant_id
if not tenant_id: if not tenant_id:
return [] return []
@@ -382,8 +380,8 @@ class Workflow(Base):
self._environment_variables = "{}" self._environment_variables = "{}"
return return
# Get tenant_id from current_user (Account or EndUser) # Use workflow.tenant_id to avoid relying on request user in background threads
tenant_id = extract_tenant_id(current_user) tenant_id = self.tenant_id
if not tenant_id: if not tenant_id:
self._environment_variables = "{}" self._environment_variables = "{}"

View File

@@ -13,7 +13,7 @@ dependencies = [
"cachetools~=5.3.0", "cachetools~=5.3.0",
"celery~=5.5.2", "celery~=5.5.2",
"chardet~=5.1.0", "chardet~=5.1.0",
"flask~=3.1.0", "flask~=3.1.2",
"flask-compress~=1.17", "flask-compress~=1.17",
"flask-cors~=6.0.0", "flask-cors~=6.0.0",
"flask-login~=0.6.3", "flask-login~=0.6.3",

View File

@@ -9,7 +9,6 @@ from core.file.models import File
from core.variables import FloatVariable, IntegerVariable, SecretVariable, StringVariable from core.variables import FloatVariable, IntegerVariable, SecretVariable, StringVariable
from core.variables.segments import IntegerSegment, Segment from core.variables.segments import IntegerSegment, Segment
from factories.variable_factory import build_segment from factories.variable_factory import build_segment
from models.model import EndUser
from models.workflow import Workflow, WorkflowDraftVariable, WorkflowNodeExecutionModel, is_system_variable_editable from models.workflow import Workflow, WorkflowDraftVariable, WorkflowNodeExecutionModel, is_system_variable_editable
@@ -43,14 +42,9 @@ def test_environment_variables():
{"name": "var4", "value": 3.14, "id": str(uuid4()), "selector": ["env", "var4"]} {"name": "var4", "value": 3.14, "id": str(uuid4()), "selector": ["env", "var4"]}
) )
# Mock current_user as an EndUser
mock_user = mock.Mock(spec=EndUser)
mock_user.tenant_id = "tenant_id"
with ( with (
mock.patch("core.helper.encrypter.encrypt_token", return_value="encrypted_token"), mock.patch("core.helper.encrypter.encrypt_token", return_value="encrypted_token"),
mock.patch("core.helper.encrypter.decrypt_token", return_value="secret"), mock.patch("core.helper.encrypter.decrypt_token", return_value="secret"),
mock.patch("models.workflow.current_user", mock_user),
): ):
# Set the environment_variables property of the Workflow instance # Set the environment_variables property of the Workflow instance
variables = [variable1, variable2, variable3, variable4] variables = [variable1, variable2, variable3, variable4]
@@ -90,14 +84,9 @@ def test_update_environment_variables():
{"name": "var4", "value": 3.14, "id": str(uuid4()), "selector": ["env", "var4"]} {"name": "var4", "value": 3.14, "id": str(uuid4()), "selector": ["env", "var4"]}
) )
# Mock current_user as an EndUser
mock_user = mock.Mock(spec=EndUser)
mock_user.tenant_id = "tenant_id"
with ( with (
mock.patch("core.helper.encrypter.encrypt_token", return_value="encrypted_token"), mock.patch("core.helper.encrypter.encrypt_token", return_value="encrypted_token"),
mock.patch("core.helper.encrypter.decrypt_token", return_value="secret"), mock.patch("core.helper.encrypter.decrypt_token", return_value="secret"),
mock.patch("models.workflow.current_user", mock_user),
): ):
variables = [variable1, variable2, variable3, variable4] variables = [variable1, variable2, variable3, variable4]
@@ -136,14 +125,9 @@ def test_to_dict():
# Create some EnvironmentVariable instances # Create some EnvironmentVariable instances
# Mock current_user as an EndUser
mock_user = mock.Mock(spec=EndUser)
mock_user.tenant_id = "tenant_id"
with ( with (
mock.patch("core.helper.encrypter.encrypt_token", return_value="encrypted_token"), mock.patch("core.helper.encrypter.encrypt_token", return_value="encrypted_token"),
mock.patch("core.helper.encrypter.decrypt_token", return_value="secret"), mock.patch("core.helper.encrypter.decrypt_token", return_value="secret"),
mock.patch("models.workflow.current_user", mock_user),
): ):
# Set the environment_variables property of the Workflow instance # Set the environment_variables property of the Workflow instance
workflow.environment_variables = [ workflow.environment_variables = [

8
api/uv.lock generated
View File

@@ -1436,7 +1436,7 @@ requires-dist = [
{ name = "cachetools", specifier = "~=5.3.0" }, { name = "cachetools", specifier = "~=5.3.0" },
{ name = "celery", specifier = "~=5.5.2" }, { name = "celery", specifier = "~=5.5.2" },
{ name = "chardet", specifier = "~=5.1.0" }, { name = "chardet", specifier = "~=5.1.0" },
{ name = "flask", specifier = "~=3.1.0" }, { name = "flask", specifier = "~=3.1.2" },
{ name = "flask-compress", specifier = "~=1.17" }, { name = "flask-compress", specifier = "~=1.17" },
{ name = "flask-cors", specifier = "~=6.0.0" }, { name = "flask-cors", specifier = "~=6.0.0" },
{ name = "flask-login", specifier = "~=0.6.3" }, { name = "flask-login", specifier = "~=0.6.3" },
@@ -1790,7 +1790,7 @@ wheels = [
[[package]] [[package]]
name = "flask" name = "flask"
version = "3.1.1" version = "3.1.2"
source = { registry = "https://pypi.org/simple" } source = { registry = "https://pypi.org/simple" }
dependencies = [ dependencies = [
{ name = "blinker" }, { name = "blinker" },
@@ -1800,9 +1800,9 @@ dependencies = [
{ name = "markupsafe" }, { name = "markupsafe" },
{ name = "werkzeug" }, { name = "werkzeug" },
] ]
sdist = { url = "https://files.pythonhosted.org/packages/c0/de/e47735752347f4128bcf354e0da07ef311a78244eba9e3dc1d4a5ab21a98/flask-3.1.1.tar.gz", hash = "sha256:284c7b8f2f58cb737f0cf1c30fd7eaf0ccfcde196099d24ecede3fc2005aa59e", size = 753440, upload-time = "2025-05-13T15:01:17.447Z" } sdist = { url = "https://files.pythonhosted.org/packages/dc/6d/cfe3c0fcc5e477df242b98bfe186a4c34357b4847e87ecaef04507332dab/flask-3.1.2.tar.gz", hash = "sha256:bf656c15c80190ed628ad08cdfd3aaa35beb087855e2f494910aa3774cc4fd87", size = 720160, upload-time = "2025-08-19T21:03:21.205Z" }
wheels = [ wheels = [
{ url = "https://files.pythonhosted.org/packages/3d/68/9d4508e893976286d2ead7f8f571314af6c2037af34853a30fd769c02e9d/flask-3.1.1-py3-none-any.whl", hash = "sha256:07aae2bb5eaf77993ef57e357491839f5fd9f4dc281593a81a9e4d79a24f295c", size = 103305, upload-time = "2025-05-13T15:01:15.591Z" }, { url = "https://files.pythonhosted.org/packages/ec/f9/7f9263c5695f4bd0023734af91bedb2ff8209e8de6ead162f35d8dc762fd/flask-3.1.2-py3-none-any.whl", hash = "sha256:ca1d8112ec8a6158cc29ea4858963350011b5c846a414cdb7a954aa9e967d03c", size = 103308, upload-time = "2025-08-19T21:03:19.499Z" },
] ]
[[package]] [[package]]