Browse Source

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>
tags/1.8.0
Eric Guo 2 months ago
parent
commit
455f842785
No account linked to committer's email address

+ 1
- 2
api/core/indexing_runner.py View File

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
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,
) )

+ 4
- 6
api/models/workflow.py View File

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
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


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)
tenant_id = extract_tenant_id(current_user)
# Use workflow.tenant_id to avoid relying on request user in background threads
tenant_id = self.tenant_id


if not tenant_id: if not tenant_id:
return [] return []
self._environment_variables = "{}" self._environment_variables = "{}"
return return


# Get tenant_id from current_user (Account or EndUser)
tenant_id = extract_tenant_id(current_user)
# Use workflow.tenant_id to avoid relying on request user in background threads
tenant_id = self.tenant_id


if not tenant_id: if not tenant_id:
self._environment_variables = "{}" self._environment_variables = "{}"

+ 1
- 1
api/pyproject.toml View File

"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",

+ 0
- 16
api/tests/unit_tests/models/test_workflow.py View 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




{"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]
{"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]




# 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 = [

+ 4
- 4
api/uv.lock View File

{ 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" },


[[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" },
{ 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]]

Loading…
Cancel
Save