[SPARK-56203][SQL] Fix race condition in SortExec.rowSorter#55006
Closed
peter-toth wants to merge 1 commit intoapache:masterfrom
Closed
[SPARK-56203][SQL] Fix race condition in SortExec.rowSorter#55006peter-toth wants to merge 1 commit intoapache:masterfrom
SortExec.rowSorter#55006peter-toth wants to merge 1 commit intoapache:masterfrom
Conversation
### What changes were proposed in this pull request? Replace `private[sql] var rowSorter: ThreadLocal[UnsafeExternalRowSorter]` with `@transient private[sql] lazy val rowSorter: ThreadLocal[UnsafeExternalRowSorter]` in `SortExec`. Remove the `rowSorter = new ThreadLocal()` reassignment that was inside `createSorter()`. ### Why are the changes needed? `SortExec` is a shared plan object: the same instance is used by all tasks that execute different partitions of the same stage. In the original code, `createSorter()` would write `rowSorter = new ThreadLocal()` — an unsynchronised write to a shared `var`. If two tasks (threads T0 and T1) called `createSorter()` concurrently: 1. T0 writes `rowSorter = ThreadLocal_0`, sets `ThreadLocal_0.set(sorter_0)` 2. T1 writes `rowSorter = ThreadLocal_1`, sets `ThreadLocal_1.set(sorter_1)` 3. T0's `cleanupResources()` reads `rowSorter` — now points to `ThreadLocal_1` — calls `ThreadLocal_1.get()` on thread T0 → `null` → `sorter_0` is leaked With a stable `lazy val`, the `ThreadLocal` object is created once (Scala lazy-val initialisation is thread-safe). Every call to `createSorter()` just calls `rowSorter.set(newSorter)` on the same object. Because `ThreadLocal` gives each thread an independent slot, T0's slot and T1's slot are completely separate; neither task can observe or clobber the other's sorter reference. `@transient` is required because `ThreadLocal` is not `Serializable`; after deserialisation the lazy val re-initialises to a fresh `ThreadLocal` on first access, which is the correct behaviour on an executor. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? Added `SortSuite` test "cleanupResources is safe when createSorter was never called" which verifies that `cleanupResources()` is a no-op when called before any sorter is created (the empty-partition case that previously required a `rowSorter \!= null` guard on the now-removed reassignable `var`). Existing `SortSuite` tests cover correct sort output and spill behaviour. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Sonnet 4.6
dongjoon-hyun
approved these changes
Mar 25, 2026
Member
dongjoon-hyun
left a comment
There was a problem hiding this comment.
+1, LGTM.
I guess this was the root cause of SortSuite flakiness, @peter-toth ?
Contributor
Author
Could be, but I found the bug in a different way. |
Member
|
All tests passed. Merged to master for Apache Spark 4.2.0. |
Contributor
Author
|
Thank you for the prompt review @dongjoon-hyun. |
Member
|
Thank you. Feel free to backport this if you want to use this in the live release branches. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
Replace
private[sql] var rowSorter: ThreadLocal[UnsafeExternalRowSorter]with@transient private[sql] lazy val rowSorter: ThreadLocal[UnsafeExternalRowSorter]inSortExec.Remove the
rowSorter = new ThreadLocal()reassignment that was insidecreateSorter().Why are the changes needed?
SortExecis a shared plan object: the same instance is used by all tasks that execute different partitions of the same stage. In the original code,createSorter()would writerowSorter = new ThreadLocal()— an unsynchronised write to a sharedvar. If two tasks (threads T0 and T1) calledcreateSorter()concurrently:rowSorter = ThreadLocal_0, setsThreadLocal_0.set(sorter_0)rowSorter = ThreadLocal_1, setsThreadLocal_1.set(sorter_1)cleanupResources()readsrowSorter— now points toThreadLocal_1— callsThreadLocal_1.get()on thread T0 →null→sorter_0is leakedWith a stable
lazy val, theThreadLocalobject is created once (Scala lazy-val initialisation is thread-safe). Every call tocreateSorter()just callsrowSorter.set(newSorter)on the same object. BecauseThreadLocalgives each thread an independent slot, T0's slot and T1's slot are completely separate; neither task can observe or clobber the other's sorter reference.@transientis required becauseThreadLocalis notSerializable; after deserialisation the lazy val re-initialises to a freshThreadLocalon first access, which is the correct behaviour on an executor.Does this PR introduce any user-facing change?
No.
How was this patch tested?
Existing
SortSuitetests cover correct sort output and spill behaviour.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Sonnet 4.6