sadikovi opened a new pull request, #60691:
URL: https://github.com/apache/spark/pull/60691

   ### What changes were proposed in this pull request?
   
   `MemoryStore.putIteratorAsBytes` creates a `SerializedValuesHolder`, whose 
constructor opens a serialization stream wrapped by 
`SerializerManager.wrapForCompression`. That stream was closed only when the 
block was fully unrolled (`getBuilder()`) or when it was handed to a 
`PartiallySerializedBlock`. `putIterator` had no failure handling, so when 
`values.next()` or `storeValue()` threw, nothing closed the stream or released 
the buffers written so far.
   
   `putIteratorAsBytes` now catches any `Throwable` from `putIterator`, calls 
the new `SerializedValuesHolder.closeOnFailure()`, and rethrows the original 
error unchanged; a cleanup failure is added to it as a suppressed exception. 
`closeOnFailure()` redirects the stream to a null sink (as 
`PartiallySerializedBlock.discard()` already does), closes it, then closes and 
disposes the unrolled buffers. It tracks what `getBuilder()` and `build()` 
already handed off, so it never closes the stream twice or frees buffers owned 
by a stored entry. The success and partial-unroll paths are unchanged.
   
   ### Why are the changes needed?
   
   With the default zstd codec the compressed stream is a 
`ZstdOutputStreamNoFinalizer`, whose native compression context and buffers are 
released only by `close()`, not by GC. Every task that fails while caching a 
block as serialized bytes (for example `MEMORY_ONLY_SER`) leaks one native 
context on the executor; common triggers are an exception from the RDD's input 
iterator, a `NotSerializableException`, or a `KryoException`. The task fails 
but the executor survives, so the leak grows across retries and long-running 
executors, outside the JVM heap limits.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No. The same exception propagates; only resources held by the failed put are 
now released.
   
   ### How was this patch tested?
   
   New `MemoryStoreSuite` tests use a test-only pass-through 
`CloseTrackingCompressionCodec` that counts opened and closed compressed 
streams:
   - The input iterator throws (on-heap and off-heap): the stream is closed, 
the original error propagates unchanged, and the block is not stored.
   - Serializing a non-serializable value throws `NotSerializableException`: 
the stream is closed.
   - Closing the stream also fails: the original error is kept, with the close 
failure as its only suppressed exception.
   - Success and partial unroll: the stream is closed after a successful put, 
stays open after a partial unroll, and is closed by 
`PartiallySerializedBlock.discard()`, which also releases the unroll memory.
   
   `build/sbt "core/testOnly org.apache.spark.storage.MemoryStoreSuite"` 
passes; with the `MemoryStore` change reverted, the first three tests fail. 
Release of off-heap buffers on failure is not asserted directly.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Yes. Generated-by: Claude Opus 5.5
   
   This pull request and its description were written by Isaac.
   


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to