This is an automated email from the ASF dual-hosted git repository.
cloud-fan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/master by this push:
new f4c53e24cadc [SPARK-61455][PYTHON] Escape lone surrogates when
reporting Python worker exceptions
f4c53e24cadc is described below
commit f4c53e24cadc5cfa299d891508c8d6ddbb67bc13
Author: Constantin-Tiberiu Craiu
<[email protected]>
AuthorDate: Thu Nov 12 00:20:57 2026 +0000
[SPARK-61455][PYTHON] Escape lone surrogates when reporting Python worker
exceptions
### What changes were proposed in this pull request?
Encode the worker error report with `backslashreplace` in
`handle_worker_exception`. This covers every worker that reports errors through
handle_worker_exception: UDF/data source /UDTF workers, streaming source
runners, TWS, FEB listener/workers.
### Why are the changes needed?
A UDF exception whose message contains a lone surrogate (e.g.
`b"/data/\xff.csv".decode("utf-8", "surrogateescape")`) makes the strict UTF-8
encode raise inside the handler, so the user sees `"Python worker exited
unexpectedly (crashed)"` instead of their error.
### Does this PR introduce _any_ user-facing change?
Yes: such exceptions are now reported to the user (with the surrogate
escaped) instead of surfacing as a worker crash.
### How was this patch tested?
Added `test_exception_with_lone_surrogate`.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Kimi K3
Closes #60340 from craiuconstantintiberiu/udf-surrogate-error-report.
Authored-by: Constantin-Tiberiu Craiu
<[email protected]>
Signed-off-by: Wenchen Fan <[email protected]>
skip-checks: true
---
python/pyspark/tests/test_util.py | 15 +++++++++++++++
python/pyspark/util.py | 2 +-
2 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/python/pyspark/tests/test_util.py
b/python/pyspark/tests/test_util.py
index c3347d2cfaf8..5e7609278763 100644
--- a/python/pyspark/tests/test_util.py
+++ b/python/pyspark/tests/test_util.py
@@ -218,6 +218,21 @@ class HandleWorkerExceptionTests(unittest.TestCase):
handle_worker_exception(e, stream, hide_traceback)
return stream.getvalue()
+ def test_exception_with_lone_surrogate(self):
+ import io
+
+ from pyspark.util import handle_worker_exception
+
+ message = "bad path: " + b"/data/\xff.csv".decode("utf-8",
"surrogateescape")
+ try:
+ raise ValueError(message)
+ except ValueError as e:
+ with io.BytesIO() as stream:
+ handle_worker_exception(e, stream)
+ result = stream.getvalue()
+
+ self.assertIn(b"/data/\\udcff.csv", result)
+
@patch.dict(os.environ, {"SPARK_SIMPLIFIED_TRACEBACK": "",
"SPARK_HIDE_TRACEBACK": ""})
def test_env_full(self):
result = self.run_handle_worker_exception()
diff --git a/python/pyspark/util.py b/python/pyspark/util.py
index 7e3dc37b2b32..83a767205027 100644
--- a/python/pyspark/util.py
+++ b/python/pyspark/util.py
@@ -566,7 +566,7 @@ def handle_worker_exception(
try:
exc_info = format_worker_exception(e, hide_traceback)
write_int(SpecialLengths.PYTHON_EXCEPTION_THROWN, outfile)
- write_with_length(exc_info.encode("utf-8"), outfile)
+ write_with_length(exc_info.encode("utf-8", "backslashreplace"),
outfile)
except IOError:
# JVM close the socket
pass
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]