[SPARK-60737][SQL][PYTHON] Close the Arrow input of Python UDFs when the task ends before the input is exhausted #59946
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #59946"
general
weight: 0.35
HyukjinKwon
What changes were proposed in this pull request?
This PR closes the Arrow buffers of the input to Python workers when the task completes before all of the input has been written.
1.
PythonArrowInputadds a task completion listener innewWriterthat closesrootandallocator. The listener andclose(), which runs once the input is exhausted, sharecloseRootAndAllocator(). It closes them only once, and only ifwriter, which creates them, has been created, so that the listener does not create the lazyrootandallocator(SPARK-55285). The listener does not endwriter, since the stream that it writes to may be closed already, and nothing sends it to the Python worker anymore. As inclose(), closing the allocator throws if any of its buffers is still allocated.2.
ArrowWriterWrapper.createAndStartArrowWriter, which the grouped and cogrouped runners call for each group, creates the wrapper, which adds the task completion listener that closes itsrootandallocator, before anything that can fail. It used to allocate the vectors and write the schema first, so a failure there, e.g. an interrupt while writing the schema, left them open.Why are the changes needed?
PythonArrowInputclosesrootand its child allocator ofArrowUtils.rootAllocatoronly at the end of the input. If the task completes before that, nothing closes them, and the buffers ofrootstay allocated for the lifetime of the executor: Arrow does not release buffers on GC, and the root allocator keeps a reference to each child allocator until it is closed. A task completes before the end of its input when itLIMITabove a filter on the UDF result.ArrowWriter.reset()keeps the capacity of the vectors, sorootholds the buffers of a batch from the first batch on. In local mode withspark.sql.execution.arrow.maxRecordsPerBatch=100and 1 KiB strings, each such task left 147,456 bytes allocated and its child allocator (stdout writer for <python>) open.This affects the runners that use
PythonArrowInputdirectly: scalar pandas and Arrow UDFs (ArrowEvalPythonExec), Arrow Python UDTFs,mapInPandasandmapInArrow,applyInPandasWithState, andtransformWithStatein PySpark, as well as the partial aggregation ofPythonIncrementalAggregateExecand the columnar input enabled byspark.sql.execution.arrow.pythonUDF.columnarInput.enabled. It is a regression from SPARK-44705 (Apache Spark 4.0.0): before it, the writer thread closed them in afinallyblock.The grouped and cogrouped runners already close the
ArrowWriterWrapperof each group in a task completion listener, butcreateAndStartArrowWriteradded the listener only after allocating the vectors and writing the schema. Withspark.python.udf.pipelined.enabled, a grouped aggregate pandas UDF under aLIMITsometimes left 49,152 bytes, when the writer was interrupted there at task completion.The new listener does not race with the writer, since task completion listeners run after the writer is done. The writer runs on the task thread, except in the pipelined mode, where the pre-completion listener added by SPARK-60113 stops the writer thread and waits for it to exit before any completion listener runs.
SPARK-55351 also closed them in a task completion listener but was reverted. Unlike it, this PR keeps
rootandallocatorlazy, and the listener does not end the writer.Does this PR introduce _any_ user-facing change?
No, except that the Arrow memory of the input to Python workers is released when a task completes before the end of its input.
How was this patch tested?
PythonUDFSuite. It runs a scalar pandas UDF over 100,000 rows of 1 KiB strings, more than the socket to the Python worker can buffer, in two jobs: one whose input fails after 1,000 rows, and one that stops reading the output early with aLIMITabove a filter on the UDF result. It runs both with and withoutspark.python.udf.pipelined.enabled, and checks that no allocator of the Arrow input is left open. Without this PR, it fails with two allocators left open, each holding 147,456 bytes.ArrowWriterSuitein which writing the schema fails inArrowWriterWrapper.createAndStartArrowWriter. Without this PR, the allocator stays open after the task completes.StatefulPythonRunnerDataSentSuite, which drivesnewWriterdirectly, passesTaskContext.empty()instead ofnull, since the writer now adds a task completion listener to it.org.apache.spark.sql.execution.python,FlatMapGroupsInPandasWithStateSuite,FlatMapGroupsInPandasWithStateDistributionSuite, andArrowConvertersSuite.The
PythonUDFSuitetest needs a Python with pandas and PyArrow, and is skipped otherwise.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5