This is an automated email from the ASF dual-hosted git repository.

amoghrajesh pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 311e43933f4 Fail fast when common-io xcom object storage path has no 
url scheme (#77370)
311e43933f4 is described below

commit 311e43933f45d5c105ab5d27c81833e37bea948e
Author: Amogh Desai <[email protected]>
AuthorDate: Sun Nov 22 21:47:34 2026 +0530

    Fail fast when common-io xcom object storage path has no url scheme (#77370)
---
 providers/common/io/docs/changelog.rst             |  6 ++++
 providers/common/io/provider.yaml                  |  2 ++
 .../providers/common/io/get_provider_info.py       |  2 +-
 .../airflow/providers/common/io/xcom/backend.py    | 21 ++++++++----
 .../io/tests/unit/common/io/xcom/test_backend.py   | 37 ++++++++++++++++++++++
 5 files changed, 60 insertions(+), 8 deletions(-)

diff --git a/providers/common/io/docs/changelog.rst 
b/providers/common/io/docs/changelog.rst
index 1f7d87db302..1799e1ee642 100644
--- a/providers/common/io/docs/changelog.rst
+++ b/providers/common/io/docs/changelog.rst
@@ -25,6 +25,12 @@
 Changelog
 ---------
 
+.. note::
+    ``XComObjectStorageBackend`` now raises ``ValueError`` when it has to 
write a value to object storage
+    and ``[common.io] xcom_objectstorage_path`` has no URL scheme (for example 
``""`` or ``/tmp/xcom``).
+    Before, the write succeeded but reads returned the file path instead of 
the value. Set the path to a
+    URL such as ``s3://conn_id@bucket/path``, or ``file:///path/to/xcom`` for 
local storage.
+
 1.11.1
 ......
 
diff --git a/providers/common/io/provider.yaml 
b/providers/common/io/provider.yaml
index af31ef906dc..d6294d423bf 100644
--- a/providers/common/io/provider.yaml
+++ b/providers/common/io/provider.yaml
@@ -97,6 +97,8 @@ config:
       xcom_objectstorage_path:
         description: |
           Path to a location on object storage where XComs can be stored in 
url format.
+          Must include a URL scheme when ``xcom_objectstorage_threshold`` is 0 
or higher;
+          use ``file:///path/to/xcom`` for local storage.
         version_added: 1.3.0
         type: string
         example: "s3://conn_id@bucket/path"
diff --git 
a/providers/common/io/src/airflow/providers/common/io/get_provider_info.py 
b/providers/common/io/src/airflow/providers/common/io/get_provider_info.py
index f225d71c9f9..79cd8dc86ae 100644
--- a/providers/common/io/src/airflow/providers/common/io/get_provider_info.py
+++ b/providers/common/io/src/airflow/providers/common/io/get_provider_info.py
@@ -64,7 +64,7 @@ def get_provider_info():
                 "description": "Common IO configuration section",
                 "options": {
                     "xcom_objectstorage_path": {
-                        "description": "Path to a location on object storage 
where XComs can be stored in url format.\n",
+                        "description": "Path to a location on object storage 
where XComs can be stored in url format.\nMust include a URL scheme when 
``xcom_objectstorage_threshold`` is 0 or higher;\nuse ``file:///path/to/xcom`` 
for local storage.\n",
                         "version_added": "1.3.0",
                         "type": "string",
                         "example": "s3://conn_id@bucket/path",
diff --git 
a/providers/common/io/src/airflow/providers/common/io/xcom/backend.py 
b/providers/common/io/src/airflow/providers/common/io/xcom/backend.py
index 3328e341d8b..18bd957291d 100644
--- a/providers/common/io/src/airflow/providers/common/io/xcom/backend.py
+++ b/providers/common/io/src/airflow/providers/common/io/xcom/backend.py
@@ -66,7 +66,14 @@ def _get_compression_suffix(compression: str) -> str:
 
 @cache
 def _get_base_path() -> ObjectStoragePath:
-    return ObjectStoragePath(conf.get_mandatory_value(SECTION, 
"xcom_objectstorage_path"))
+    path = conf.get_mandatory_value(SECTION, "xcom_objectstorage_path")
+    # Reads only resolve URLs, so a schemeless path would read back as the 
path string.
+    if not urlsplit(path).scheme:
+        raise ValueError(
+            f"[{SECTION}] xcom_objectstorage_path must be a URL with a scheme, 
such as "
+            f"s3://conn_id@bucket/path or file:///path/to/xcom; got {path!r}."
+        )
+    return ObjectStoragePath(path)
 
 
 @cache
@@ -114,18 +121,18 @@ class XComObjectStorageBackend(BaseXCom):
 
     @classmethod
     def store_value(cls, coordinate: XComCoordinate, key: str, value: Any) -> 
str:
-        base = _get_base_path()
-        path = cls._write_large_value(
-            value,
-            lambda suffix: base.joinpath(
+        def get_path(suffix: str) -> ObjectStoragePath:
+            base = _get_base_path()
+            return base.joinpath(
                 coordinate.build_path(
                     "",
                     key,
                     suffix=f".json{suffix}",
                     max_length=_MAX_PATH_LENGTH - len(str(base).encode()) - 1,
                 )
-            ),
-        )
+            )
+
+        path = cls._write_large_value(value, get_path)
         return BaseXCom.serialize_value(value if path is None else str(path))
 
     @staticmethod
diff --git a/providers/common/io/tests/unit/common/io/xcom/test_backend.py 
b/providers/common/io/tests/unit/common/io/xcom/test_backend.py
index 9fd26f2187c..fd6918ec58c 100644
--- a/providers/common/io/tests/unit/common/io/xcom/test_backend.py
+++ b/providers/common/io/tests/unit/common/io/xcom/test_backend.py
@@ -569,3 +569,40 @@ class TestXComObjectStorageBackend:
             deserialized_data = XCom.deserialize_value(mock_xcom_ser)
 
             assert deserialized_data == expected_value
+
+    @pytest.mark.parametrize("path", ["", "relative/xcom", "/abs/xcom"])
+    def test_serialize_value_rejects_a_path_without_a_scheme(self, path):
+        with conf_vars(
+            {
+                ("common.io", "xcom_objectstorage_path"): path,
+                ("common.io", "xcom_objectstorage_threshold"): "0",
+            }
+        ):
+            with pytest.raises(ValueError, match="xcom_objectstorage_path must 
be a URL with a scheme"):
+                XComObjectStorageBackend.serialize_value("value")
+
+    @pytest.mark.skipif(not AIRFLOW_V_3_4_PLUS, reason="store_value is called 
from Airflow 3.4")
+    @pytest.mark.parametrize("path", ["", "relative/xcom", "/abs/xcom"])
+    def test_store_value_rejects_a_path_without_a_scheme(self, path):
+        coordinate = XComCoordinate("dag", "run", "task", uuid.uuid4())
+        with conf_vars(
+            {
+                ("common.io", "xcom_objectstorage_path"): path,
+                ("common.io", "xcom_objectstorage_threshold"): "0",
+            }
+        ):
+            with pytest.raises(ValueError, match="xcom_objectstorage_path must 
be a URL with a scheme"):
+                XComObjectStorageBackend.store_value(coordinate, "key", 
"value")
+
+    @pytest.mark.skipif(not AIRFLOW_V_3_4_PLUS, reason="store_value is called 
from Airflow 3.4")
+    def 
test_store_value_without_a_path_keeps_values_in_the_database_when_nothing_is_offloaded(self):
+        coordinate = XComCoordinate("dag", "run", "task", uuid.uuid4())
+        with conf_vars(
+            {
+                ("common.io", "xcom_objectstorage_path"): "",
+                ("common.io", "xcom_objectstorage_threshold"): "-1",
+            }
+        ):
+            stored = XComObjectStorageBackend.store_value(coordinate, "key", 
"value")
+
+            assert XComObjectStorageBackend.load_value(stored) == "value"

Reply via email to