Prevent use-after-free when aborting multithreaded shuffle writes #17160
All Vouch review confidence checks passed
Residual risk 0.22 = Change Risk (0.52) × (1 − Review Confidence (0.57)).
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 #17160"
general
weight: 0.35
thirtiseven
Fixes #17081.
Description
When
RapidsShuffleWriter.stop(false)is called whileRapidsShuffleThreadedWriterBasestill has activeBatchMergertasks running,cleanupTempData()abortsRapidsLocalDiskShuffleMapOutputWriterinstances immediately, which callsSpillablePartialFileHandle.close()and frees the off-heapHostMemoryBuffer. BecauseSpillablePartialFileHandle.write()was unsynchronized againstclose()/doClose(), arapids-shuffle-mergerthread copying compressed bytes intohostviasetBytes()(Unsafe.copyMemory/jbyte_disjoint_arraycopy) could write into freed native memory and crash the JVM withSIGSEGV.This change fixes both layers of the race:
SpillablePartialFileHandle.write()withclose()/doClose()andfinishWrite()via awriteInProgressflag checked under brief entry/exit handle monitors (withActiveWrite), withdoClose()waiting onspillInProgress || writeInProgresssoHostMemoryBuffercannot be freed mid-write and post-close writes fail cleanly withIllegalStateException, while keepingexpandBuffer()/HostMemoryBuffer.allocate()outside the handle monitor so concurrent expansions under finite host memory cannot deadlock withSpillFrameworkspill selection (andspillableshort-circuits!protectedFromSpillbeforesuper.spillable).RapidsLocalDiskShuffleMapOutputWriter'sabortedstate andpartialFileHandleinitialization/abort/commitAllPartitionstransitions under brief writer-monitor state changes (committingflag withabort()waiting outside nested locks, and@volatilereads with non-optional handle lookup on the write path so normal writes add no extra writer lock), ensuring concurrentabort()cannot cause a non-empty disk commit to write an index with anulldata file while preserving legitimate empty commits.mapOutputWritersandstoppinginRapidsShuffleWriter(bothspark330andspark400db173shims), add anonAbort()hook implemented byRapidsShuffleThreadedWriterBase(tracking active batches in an $O(1)$HashMapbybatchId) to signal the limiter and cancel/drain every activeBatchMergerbefore aborting map output writers, and atomically transfer map-output writer ownership to the shuffle catalog viatransferMapOutputWritersIfNotStoppingsostop(false)cannot close published handles and a stopped writer cannot publish aborted handles.SpillablePartialFileHandleSuite(close()andfinishWrite()waiting on an in-flightwrite(), aCyclicBarrier(2)concurrent-expansion test under a 288 KiBHostAllocbudget with failure-safe daemon/store teardown verified to fail cleanly against86d28cdec, plus 40-round concurrentwrite+closestress coverage),RapidsLocalDiskShuffleMapOutputWriterSuite(stream, channel, partition-writer, andcommitAllPartitionsoperations afterabort()throwIllegalStateException, non-aborted empty commits succeed, andabort()waits for an in-flightcommitAllPartitions()without deleting temp data), andRapidsShuffleThreadedWriterSuite(two activeBatchMergerthreads held inOutputStream.write()verifyingstop(false)drains mergers beforeabort()and exits viaCancellationException/IOException,write()afterstop(false)aborting the newly created writer,stop(false)landing between second-writer registration and active-batch registration,stop(false)racing withcleanupBatchdraining an active merger, both orderings of concurrentstop(false)vs skip-merge catalog publication, and concurrentstop(false)before non-catalog disk commit).Validation (Java 17)
buildver=401, Scala 2.13, GPU):SpillablePartialFileHandleSuite,SpillablePartialFileHandleMetricSuite,RapidsLocalDiskShuffleMapOutputWriterSuite, andRapidsShuffleThreadedWriterSuitepassed 72/72 onfab06e199(plus the deadlock regression test verified to fail cleanly in 33s with exit code 1 against86d28cdec, and 3 consecutive parallel-mode-Drapids.parallelUnitTests=trueruns ofRapidsShuffleThreadedWriterSuitepassed 28/28 each onae85b3271).buildver=356, Scala 2.12, GPU):SpillablePartialFileHandleSuiteandRapidsShuffleThreadedWriterSuitepassed 55/55.base=139e06a62,head=fab06e199):reviewreturnedPASSacross 34sql-pluginbuildvers (including400db173and410db183, 4,442 checked requirements, 0 new findings).advisorynoted file overlap with open PRs #15150, #16041, #17099, #17131, and WIP #17142.spark400db173shim change).AI assistance from Devin was used to diagnose, implement, and validate this fix.
Checklists
Documentation
Testing
Performance
Measured before (
5f903a242) vs after (9b9f97200, using the finalwriteInProgressentry/exit monitor design and non-optionalcurrentPartialFileHandle()) write throughput onRapidsLocalDiskShuffleMapOutputWriter->PartitionWriterStream.write()->SpillablePartialFileHandle.write()(MEMORY_WITH_SPILL, 8 partitions, 64 MiB per iteration, 5 warmup + 15 measured iterations, 3 interleaved rounds, Java 17; exercises only the partition-writer write hot path, notRapidsShuffleThreadedWriterBasebatch cleanup):| Chunk Size | Writes / 64 MiB | Before (
5f903a242) | After (9b9f97200) | Delta || --- | ---: | ---: | ---: | ---: |
| 4 KiB | 16,384 | 27.199 ms (2.30 GiB/s) | 27.819 ms (2.25 GiB/s) | +0.62 ms (+2.3%, ~38 ns/write) |
| 64 KiB | 1,024 | 27.220 ms (2.30 GiB/s) | 27.343 ms (2.29 GiB/s) | +0.12 ms (+0.45%, noise) |
RapidsLocalDiskShuffleMapOutputWriteruses@volatilereads on the write path (no extra lock per write), andactiveBatchesuses anO(1)HashMapkeyed bybatchId. The NDS SF10 run below was collected on86d28cdec(prior single-monitorwrite()design) on a single local node and is scoped to that workload.End-to-end NDS SF10 GPU benchmark (19 shuffle-heavy queries:
q4, q11, q14a, q14b, q17, q23a, q23b, q25, q29, q50, q64, q67, q72, q74, q75, q78, q80, q93, q95) comparing back-to-back Spark 4.0.1 / Scala 2.13 distribution JARs built from5f903a242(before) vs86d28cdec(after) with identical cuDF (5dfab722b) and JNI (560a5ce8c) snapshots,spark.rapids.shuffle.mode=MULTITHREADED(writer.threads=4,reader.threads=4,pinnedPool.size=2g,local[8], RTX 5880 Ada, ABBA orderbefore_1 -> after_1 -> after_2 -> before_2, 1 warmup + 2 measured passes per JVM; all 19 queries matched exact row counts and output SHA-256 digests, andGpuColumnarExchangeplans were verified):| Variant | Warmup passes (ms) | Measured 19-query passes (ms) | Median (ms) | Mean (ms) | Delta (After - Before) |
| --- | --- | --- | ---: | ---: | ---: |
| Before (
5f903a242) | 51,270.0, 51,386.8 | 41,511.3, 41,508.9, 41,432.7, 40,547.5 | 41,470.8 | 41,250.1 | — || After (
86d28cdec) | 51,479.3, 50,860.4 | 41,306.7, 40,541.7, 41,507.0, 40,615.4 | 40,961.1 | 40,992.7 | -509.7 ms (-1.23%) median / -257.4 ms (-0.62%) mean |Paired ABBA deltas are -585.9 ms (pair 1) and +71.1 ms (pair 2), with the heaviest shuffle queries (
q72: +0.18%,q64: -0.96%,q4: -1.12%) all within local run-to-run variance (no measurable regression; local single-node scope).