[SPARK-60466][CORE] Fix `ThreadUtils.shutdown` to call `shutdownNow` after the grace period #59707
All Vouch review confidence checks passed
Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 506 lines diff across 6 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.373
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #59707"
general
weight: 0.35
sarutak
What changes were proposed in this pull request?
This PR aims to fix
ThreadUtils.shutdownto callshutdownNow()when the executor does not terminate within the grace period.Currently,
ThreadUtils.shutdownchecksisShutdownto decide whether to callshutdownNow(). However,isShutdownis alwaystrueaftershutdown(), soshutdownNow()is never called.This PR uses the return value of
awaitTerminationinstead, as in the usage example of ExecutorService. In addition, if the calling thread is interrupted while waiting, this PR callsshutdownNow()before rethrowing theInterruptedException.Why are the changes needed?
The
shutdownNow()branch has been dead code sinceThreadUtils.shutdownwas added by SPARK-24248 (apache/spark#21366). When tasks outlive the grace period, they keep running and the queued tasks are still executed. For aScheduledThreadPoolExecutor, pending delayed tasks are kept until their delays elapse because it executes existing delayed tasks aftershutdown()by default.This became a regression in Apache Spark v4.0.0 (2025-05-19) because SPARK-46929 (apache/spark#44962) replaced the explicit
if (!awaitTermination(...)) shutdownNow()logic of the following call sites withThreadUtils.shutdown.JobScheduler.stop: A non-gracefulStreamingContext.stopno longer interrupts running streaming jobs after 2 seconds.CheckpointWriter.stop: Pending checkpoint writes are no longer discarded after 10 seconds. Since its executor uses a non-daemon thread, the JVM cannot exit until all of them are written.ElementTrackingStore.close: Queued asynchronous cleanup tasks are no longer discarded after 5 seconds and can run against the closed store.This also contributed to the JVM hang of SPARK-49479 (apache/spark#50020). The non-daemon
BarrierCoordinatortimer thread had a pending task with the 365-dayspark.barrier.sync.timeout, and the thread was never terminated becauseshutdownNow()was not called after the 30-second grace period.BarrierCoordinatornow cancels its timer tasks before shutting down, butThreadUtils.shutdownitself still has the issue.Does this PR introduce _any_ user-facing change?
Yes. When an executor does not terminate within the grace period of
ThreadUtils.shutdown, its running tasks are interrupted and its queued tasks are dropped. The same happens right away when the calling thread is interrupted while waiting. For the above three call sites, this restores the behavior of Apache Spark 3.5 in the grace period case. For the other callers, this is a new behavior becauseshutdownNow()was never called for them. All callers are in stop or close paths. Executors that terminate within the grace period without interruption are not affected.How was this patch tested?
Pass the CIs with the newly added test cases.
Without this fix, three of the four new test cases fail.
SPARK-60466: shutdown interrupts running tasks and drops queued tasks after the grace periodSPARK-60466: shutdown drops pending delayed tasks after the grace periodSPARK-60466: shutdown forces termination when the calling thread is interruptedThe remaining one,
SPARK-60466: shutdown waits for tasks that finish within the grace period, passes with and without this fix to make sure that the graceful path is unchanged.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5