You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "dongjoon-hyun (via GitHub)" <gi...@apache.org> on 2026/10/17 02:30:30 UTC
[PR] [SPARK-60310][SQL] Fix `PythonForeachWriter` reading freed buffer pages after its task is killed [spark]
dongjoon-hyun opened a new pull request, #59568:
URL: https://github.com/apache/spark/pull/59568
### What changes were proposed in this pull request?
This PR fixes `PythonForeachWriter.UnsafeRowBuffer` so that `close()` cannot free memory that the `WriterThread` still reads.
- `remove()` returns a copy of the row instead of a row pointing into a `HybridRowQueue` page.
- `close()` frees the queue under the buffer's lock, taken uninterruptibly, and makes any later or waiting `remove()` throw.
### Why are the changes needed?
`PythonForeachWriter` sends the buffered rows to the Python worker from its own `WriterThread`. If the task is killed while `close()` waits for that thread, for example by `query.stop()`, the task completes without stopping it. The task completion listener then frees the queue's pages while the `WriterThread` may still be reading a row from them, which can crash the executor when `spark.memory.offHeap.enabled=true`. In addition, if `allRowsAdded()` is interrupted, a `WriterThread` waiting for rows never wakes up.
This bug has existed since SPARK-44705 ([v4.0.0](https://github.com/apache/spark/releases/tag/v4.0.0), 2025-05-19).
### Does this PR introduce _any_ user-facing change?
Yes, this fixes a possible executor crash and thread leak when a task with a Python `foreach` writer is killed.
### How was this patch tested?
Added two test cases to `PythonForeachWriterSuite`. Both fail without the fix: a row taken out before `close()` reads the `spark.memory.debugFill` pattern of a freed page, and a thread waiting for rows stays blocked after `close()`.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-60310][SQL] Fix `PythonForeachWriter` reading freed buffer pages after its task is killed [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #59568:
URL: https://github.com/apache/spark/pull/59568#issuecomment-6252949984
Since this is a correctness issue, I set this as a blocker of Apache Spark 4.3.0 (whose RC2 failed recently). For the other releases, it's not a release blocker because this exists since 4.0.0.
Just FYI, @HeartSaVioR , @szehon-ho , @uros-b
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-60310][SQL] Fix `PythonForeachWriter` reading freed buffer pages after its task is killed [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #59568:
URL: https://github.com/apache/spark/pull/59568#issuecomment-6229208393
Could you review this PR when you have some time, @viirya ? I found this while reviewing your PR.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-60310][SQL] Fix `PythonForeachWriter` reading freed buffer pages after its task is killed [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #59568:
URL: https://github.com/apache/spark/pull/59568#issuecomment-6252881710
Thank you, @viirya. I added the on-heap case to the PR description.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-60310][SQL] Fix `PythonForeachWriter` reading freed buffer pages after its task is killed [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #59568:
URL: https://github.com/apache/spark/pull/59568#issuecomment-6253024524
**Merge Summary:**
- merged into master https://github.com/apache/spark/commit/695b0a2d243b832a0fc42813b6b1188eef07ad46
- merged into branch-4.x https://github.com/apache/spark/commit/ea48121395d59c19a3c966eacb708cc891840293
- merged into branch-4.3 https://github.com/apache/spark/commit/acfa9967dd623212d8d0ba6c1ccd3634e13c077f
- merged into branch-4.2 https://github.com/apache/spark/commit/b7101d630bd2fc56def4fcc9758db809cff17629
- merged into branch-4.1 https://github.com/apache/spark/commit/e72f7b6ea78f09b481af633114b164c1ebb05970
- merged into branch-4.0 https://github.com/apache/spark/commit/83365d516934ee44b340f35a8b960307af0d01e8
*Posted by `merge_spark_pr.py`*
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-60310][SQL] Fix `PythonForeachWriter` reading freed buffer pages after its task is killed [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #59568:
URL: https://github.com/apache/spark/pull/59568#discussion_r4307965026
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/python/streaming/PythonForeachWriterSuite.scala:
##########
@@ -66,6 +68,66 @@ class PythonForeachWriterSuite extends SparkFunSuite with Eventually with Mockit
b.assertOutput(1 to 2000)
}
+ test("SPARK-60310: UnsafeRowBuffer: close does not free a row that the reading thread uses") {
+ val buffer = createBuffer(4 << 10)
+ val intProj = UnsafeProjection.create(Array[DataType](IntegerType))
+ buffer.add(intProj(new GenericInternalRow(Array[Any](1))))
+ val rowRead = new CountDownLatch(1)
+ val bufferClosed = new CountDownLatch(1)
+ @volatile var value = 0
+ // Like the WriterThread converting a row while the task completion listener closes the
+ // buffer, e.g. after the task was killed while waiting for the WriterThread.
+ val reader = new Thread() {
+ override def run(): Unit = {
+ val row = buffer.iterator.next()
+ rowRead.countDown()
+ bufferClosed.await()
+ value = row.getInt(0)
+ }
+ }
+ reader.start()
+ try {
+ assert(rowRead.await(60, TimeUnit.SECONDS))
+ buffer.close()
+ } finally {
+ bufferClosed.countDown()
+ reader.join()
+ }
+ // A row in a freed page reads 0x5a5a5a5a, the pattern of spark.memory.debugFill.
Review Comment:
Thank you. I updated the comment.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-60310][SQL] Fix `PythonForeachWriter` reading freed buffer pages after its task is killed [spark]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #59568:
URL: https://github.com/apache/spark/pull/59568#discussion_r4301610651
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/python/streaming/PythonForeachWriterSuite.scala:
##########
@@ -66,6 +68,66 @@ class PythonForeachWriterSuite extends SparkFunSuite with Eventually with Mockit
b.assertOutput(1 to 2000)
}
+ test("SPARK-60310: UnsafeRowBuffer: close does not free a row that the reading thread uses") {
+ val buffer = createBuffer(4 << 10)
+ val intProj = UnsafeProjection.create(Array[DataType](IntegerType))
+ buffer.add(intProj(new GenericInternalRow(Array[Any](1))))
+ val rowRead = new CountDownLatch(1)
+ val bufferClosed = new CountDownLatch(1)
+ @volatile var value = 0
+ // Like the WriterThread converting a row while the task completion listener closes the
+ // buffer, e.g. after the task was killed while waiting for the WriterThread.
+ val reader = new Thread() {
+ override def run(): Unit = {
+ val row = buffer.iterator.next()
+ rowRead.countDown()
+ bufferClosed.await()
+ value = row.getInt(0)
+ }
+ }
+ reader.start()
+ try {
+ assert(rowRead.await(60, TimeUnit.SECONDS))
+ buffer.close()
+ } finally {
+ bufferClosed.countDown()
+ reader.join()
+ }
+ // A row in a freed page reads 0x5a5a5a5a, the pattern of spark.memory.debugFill.
Review Comment:
nit: This test only catches the regression because the build sets `-Dspark.memory.debugFill=true` for tests (SparkBuild and the pom). Without it, the freed page keeps its old contents and the test passes without the fix too. Maybe say so here, e.g. "...the pattern of spark.memory.debugFill, which the build enables for tests."
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-60310][SQL] Fix `PythonForeachWriter` reading freed buffer pages after its task is killed [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun closed pull request #59568: [SPARK-60310][SQL] Fix `PythonForeachWriter` reading freed buffer pages after its task is killed
URL: https://github.com/apache/spark/pull/59568
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org