This is an automated email from the ASF dual-hosted git repository.
zhengruifeng pushed a commit to branch branch-4.x
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/branch-4.x by this push:
new 6e8918755fdf [MINOR][PYTHON] Fix framed serializer length limit at 2
GiB
6e8918755fdf is described below
commit 6e8918755fdff7f748355c30b3edda666570b8d7
Author: Aditya Vaish <[email protected]>
AuthorDate: Wed Nov 11 09:12:57 2026 +0800
[MINOR][PYTHON] Fix framed serializer length limit at 2 GiB
### What changes were proposed in this pull request?
Reject serialized payload lengths greater than or equal to `2**31` in
`FramedSerializer._write_with_length`, rather than only lengths greater
than `2**31`.
Add a regression test covering `2**31 - 1`, `2**31`, and `2**31 + 1` using
mocked
payload lengths, without allocating multi-gigabyte objects. The test checks
the
largest valid frame header and verifies that rejected payloads write
nothing.
### Why are the changes needed?
The frame header uses `struct.pack("!i", length)`, whose largest valid
length is
`2**31 - 1`. A payload of exactly 2 GiB currently passes the explicit size
check
and raises an incidental `struct.error` instead of the existing descriptive
`ValueError`.
### Does this PR introduce _any_ user-facing change?
Yes. Exactly 2 GiB payloads now raise the existing
`ValueError: can not serialize object larger than 2G` instead of
`struct.error: argument out of range`. The supported payload size does not
change.
### How was this patch tested?
Added `SerializersTest.test_framed_serializer_length_limit`. Confirmed it
fails
with `struct.error` at exactly `2**31` using the previous size check.
All 11 tests in `SerializationTestCase` and `SerializersTest` pass on
Windows
with Python 3.11.9, using the repository's Python sources and bundled Py4J:
```python
import sys
import unittest
sys.path[:0] = ["python", "python/lib/py4j-0.10.9.9-src.zip"]
names = [
"pyspark.tests.test_serializers.SerializationTestCase",
"pyspark.tests.test_serializers.SerializersTest",
]
suite = unittest.TestSuite(
unittest.defaultTestLoader.loadTestsFromName(name) for name in names
)
result = unittest.TextTestRunner(verbosity=2).run(suite)
sys.exit(not result.wasSuccessful())
```
These are pure-Python tests; no Spark JVM was started.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: GitHub Copilot App 1.0.102-0.unstable.r40949665530.gab1eef5
Closes #60183 from adityavaish/adityavaish-small-spark-fix-7b1.
Lead-authored-by: Aditya Vaish
<[email protected]>
Co-authored-by: Aditya Vaish <[email protected]>
Signed-off-by: Ruifeng Zheng <[email protected]>
(cherry picked from commit 8b6b1ff8642d1a513402e539d3a2f8cf9dba45a9)
Signed-off-by: Ruifeng Zheng <[email protected]>
skip-checks: true
---
python/pyspark/serializers.py | 2 +-
python/pyspark/tests/test_serializers.py | 22 ++++++++++++++++++++++
2 files changed, 23 insertions(+), 1 deletion(-)
diff --git a/python/pyspark/serializers.py b/python/pyspark/serializers.py
index 20d7f5a2ee23..8cc009f4bf07 100644
--- a/python/pyspark/serializers.py
+++ b/python/pyspark/serializers.py
@@ -161,7 +161,7 @@ class FramedSerializer(Serializer):
serialized = self.dumps(obj)
if serialized is None:
raise ValueError("serialized value should not be None")
- if len(serialized) > (1 << 31):
+ if len(serialized) >= (1 << 31):
raise ValueError("can not serialize object larger than 2G")
write_int(len(serialized), stream)
stream.write(serialized)
diff --git a/python/pyspark/tests/test_serializers.py
b/python/pyspark/tests/test_serializers.py
index 10fb77b65b00..f22cf0c4c2fc 100644
--- a/python/pyspark/tests/test_serializers.py
+++ b/python/pyspark/tests/test_serializers.py
@@ -15,10 +15,12 @@
# limitations under the License.
#
import collections
+import io
import math
import pickle
import sys
import unittest
+from unittest.mock import MagicMock
from pyspark import serializers
from pyspark.serializers import (
@@ -220,6 +222,26 @@ class NumPyTests(PySparkTestCase):
class SerializersTest(unittest.TestCase):
+ def test_framed_serializer_length_limit(self):
+ ser = NoOpSerializer()
+ for length in [(1 << 31) - 1, 1 << 31, (1 << 31) + 1]:
+ with self.subTest(length=length):
+ payload = MagicMock()
+ payload.__len__.return_value = length
+ stream = MagicMock()
+ if length < 1 << 31:
+ ser.dump_stream([payload], stream)
+ self.assertEqual(stream.write.call_count, 2)
+ header = io.BytesIO(stream.write.call_args_list[0].args[0])
+ self.assertEqual(serializers.read_int(header), length)
+ stream.write.assert_called_with(payload)
+ else:
+ with self.assertRaisesRegex(
+ ValueError, "can not serialize object larger than 2G"
+ ):
+ ser.dump_stream([payload], stream)
+ stream.write.assert_not_called()
+
def test_chunked_stream(self):
original_bytes = bytearray(range(100))
for data_length in [1, 10, 100]:
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]