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

   
   
   ### What changes were proposed in this pull request?
   
   Move the `TaskContext` registration out of `readObject` and into 
`readResolve`.
   
   `AccumulatorV2` currently registers from `readObject`, which runs at 
AccumulatorV2's own slot. Java serialization reads hierarchy data 
**superclass-first**, so no subclass field is assigned yet — while 
`registerAccumulator` publishes `this` into `TaskMetrics` where other threads 
can read it. The PR drops `readObject` (beyond registering, all it did was 
in.defaultReadObject(), which the framework does anyway) and registers from 
`final protected def readResolve()` instead, which runs only after every class 
in the hierarchy has been read.
   
   It's `final` for the same reason `writeReplace` already is: `readObject` 
must be `private` so it's invoked per class, whereas `readResolve` is inherited 
— a subclass defining one would silently stop registration.
   
   ### Why are the changes needed?
   
   Subclasses are published before their state exists, so a subclass holding 
state in a `val` exposes a `null`. The reader that hits it is the executor 
heartbeater, calling `isZero` on every registered accumulator. The NPE 
propagates out of a `scheduleAtFixedRate` task, so 
`ScheduledThreadPoolExecutor` stops rescheduling — the executor never 
heartbeats again, the driver removes it on timeout.
   
   Plus the JLS 17.5.3 aspect: a val is a final field written reflectively 
after publication, so a reader has no guarantee of seeing it and the JIT may 
fold the read.
   
   SPARK-20977 fixed this locally in CollectionAccumulator; every subclass 
since has re-derived the workaround and not all have — SetAccumulator in 
sql/execution/debug still dereferences private val _set from isZero, and 
EventTimeStatsAccum escapes the NPE only incidentally via Scala's null-safe ==, 
reporting the wrong answer instead.
   
   
   ### Does this PR introduce _any_ user-facing change?
   NO
   
   
   ### How was this patch tested?
   New test case
   
   ### Was this patch authored or co-authored using generative AI tooling?
   Yes. Claude Opus 5.0


-- 
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