Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(vdb): bound the TiDB Cloud API timeouts (#39953)

Yufeng Hef6852205d2a7
diff --git a/api/providers/vdb/vdb-tidb-on-qdrant/src/dify_vdb_tidb_on_qdrant/tidb_on_qdrant_vector.py b/api/providers/vdb/vdb-tidb-on-qdrant/src/dify_vdb_tidb_on_qdrant/tidb_on_qdrant_vector.py
index 6c027d1d267..be6df15cae1 100644
--- a/api/providers/vdb/vdb-tidb-on-qdrant/src/dify_vdb_tidb_on_qdrant/tidb_on_qdrant_vector.py
+++ b/api/providers/vdb/vdb-tidb-on-qdrant/src/dify_vdb_tidb_on_qdrant/tidb_on_qdrant_vector.py
@@ -46,6 +46,11 @@ if TYPE_CHECKING:
type MetadataFilter = DictFilter | common_types.Filter
+# Bounded connect/read timeout so a slow or hanging TiDB Cloud API call
+# cannot block a cluster provisioning or password rotation forever.
+_TIDB_CLOUD_REQUEST_TIMEOUT = httpx.Timeout(30.0, connect=5.0)
+
+
class TidbOnQdrantConfig(BaseModel):
endpoint: str
api_key: str | None = None
@@ -551,6 +556,7 @@ class TidbOnQdrantVectorFactory(AbstractVectorFactory):
f"{tidb_config.api_url}/clusters",
json=cluster_data,
auth=DigestAuth(tidb_config.public_key, tidb_config.private_key),
+ timeout=_TIDB_CLOUD_REQUEST_TIMEOUT,
)
if response.status_code == 200:
@@ -574,6 +580,7 @@ class TidbOnQdrantVectorFactory(AbstractVectorFactory):
f"{tidb_config.api_url}/clusters/{cluster_id}/password",
json=body,
auth=DigestAuth(tidb_config.public_key, tidb_config.private_key),
+ timeout=_TIDB_CLOUD_REQUEST_TIMEOUT,
)
if response.status_code == 200:
diff --git a/api/providers/vdb/vdb-tidb-on-qdrant/tests/unit_tests/test_tidb_on_qdrant_vector.py b/api/providers/vdb/vdb-tidb-on-qdrant/tests/unit_tests/test_tidb_on_qdrant_vector.py
index 76802de62ef..f94c6496897 100644
--- a/api/providers/vdb/vdb-tidb-on-qdrant/tests/unit_tests/test_tidb_on_qdrant_vector.py
+++ b/api/providers/vdb/vdb-tidb-on-qdrant/tests/unit_tests/test_tidb_on_qdrant_vector.py
@@ -226,3 +226,40 @@ class TestInitVectorEndpointSelection:
qdrant_url = binding_endpoint or global_url or ""
assert qdrant_url == "https://qdrant-global.tidb.com"
+
+
+class TestTidbCloudRequestTimeouts:
+ """The TiDB Cloud API calls must be bounded: a hanging endpoint must not
+ block cluster provisioning or password rotation forever."""
+
+ @patch("dify_vdb_tidb_on_qdrant.tidb_on_qdrant_vector.httpx.post")
+ def test_create_cluster_passes_bounded_timeout(self, mock_post):
+ from dify_vdb_tidb_on_qdrant.tidb_on_qdrant_vector import (
+ TidbConfig,
+ TidbOnQdrantVectorFactory,
+ )
+
+ mock_post.return_value.status_code = 200
+ mock_post.return_value.json.return_value = {}
+ factory = TidbOnQdrantVectorFactory()
+ config = TidbConfig(api_url="https://api.tidbcloud.test", public_key="pub", private_key="priv")
+
+ factory.create_tidb_serverless_cluster(config, "display", "us-east-1")
+
+ assert mock_post.call_args.kwargs["timeout"] is not None
+
+ @patch("dify_vdb_tidb_on_qdrant.tidb_on_qdrant_vector.httpx.put")
+ def test_change_password_passes_bounded_timeout(self, mock_put):
+ from dify_vdb_tidb_on_qdrant.tidb_on_qdrant_vector import (
+ TidbConfig,
+ TidbOnQdrantVectorFactory,
+ )
+
+ mock_put.return_value.status_code = 200
+ mock_put.return_value.json.return_value = {}
+ factory = TidbOnQdrantVectorFactory()
+ config = TidbConfig(api_url="https://api.tidbcloud.test", public_key="pub", private_key="priv")
+
+ factory.change_tidb_serverless_root_password(config, "c-1", "new-password")
+
+ assert mock_put.call_args.kwargs["timeout"] is not None