← Back to Pull requests

[SPARK-60466][CORE] Fix `ThreadUtils.shutdown` to call `shutdownNow` after the grace period #59707

Closed
unknown closed this pull request without merging on Oct 22, 2026 22:12 UTC
View on GitHub
unknown unknown commented on Oct 22, 2026 13:54 UTC
Author

What changes were proposed in this pull request?

This PR aims to fix ThreadUtils.shutdown to call shutdownNow() when the executor does not terminate within the grace period.

Currently, ThreadUtils.shutdown checks isShutdown to decide whether to call shutdownNow(). However, isShutdown is always true after shutdown(), so shutdownNow() is never called.

executor.shutdown()
executor.awaitTermination(gracePeriod.toMillis, TimeUnit.MILLISECONDS)
if (!executor.isShutdown) { // Always false after `shutdown()`
  executor.shutdownNow()
}

This PR uses the return value of awaitTermination instead, as in the usage example of ExecutorService. In addition, if the calling thread is interrupted while waiting, this PR calls shutdownNow() before rethrowing the InterruptedException.

Why are the changes needed?

The shutdownNow() branch has been dead code since ThreadUtils.shutdown was added by SPARK-24248 (apache/spark#21366). When tasks outlive the grace period, they keep running and the queued tasks are still executed. For a ScheduledThreadPoolExecutor, pending delayed tasks are kept until their delays elapse because it executes existing delayed tasks after shutdown() 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 with ThreadUtils.shutdown.

  • JobScheduler.stop: A non-graceful StreamingContext.stop no 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 BarrierCoordinator timer thread had a pending task with the 365-day spark.barrier.sync.timeout, and the thread was never terminated because shutdownNow() was not called after the 30-second grace period. BarrierCoordinator now cancels its timer tasks before shutting down, but ThreadUtils.shutdown itself 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 because shutdownNow() 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 period

  • SPARK-60466: shutdown drops pending delayed tasks after the grace period

  • SPARK-60466: shutdown forces termination when the calling thread is interrupted
  • The 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.

    $ build/sbt "core/testOnly org.apache.spark.util.ThreadUtilsSuite"
    ...
    [info] Tests: succeeded 15, failed 0, canceled 0, ignored 0, pending 0
    [info] All tests passed.
    

    Was this patch authored or co-authored using generative AI tooling?

    Generated-by: Claude Opus 5.5

    Diff: 506 lines (+379 / -127) across 6 files Reviewer: sarutak Review duration: 498m
    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
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.6% (MEDIUM) · Higher = larger blast radius
    47.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.8% · Safe to merge
    18.8%
    Repository