Skip to content

fix: release memory in Spark before lowering CometTaskMemoryManager.used - #7871

Merged
sunchao merged 2 commits into
apache:mainfrom
adityavaish:adityavaish-fix-task-memory-manager-release
Dec 5, 2026
Merged

sunchao merged 2 commits into
apache:mainfrom
adityavaish:adityavaish-fix-task-memory-manager-release

Conversation

@adityavaish

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Closes #7176.

Rationale for this change

CometTaskMemoryManager.releaseMemory decremented used before calling internal.releaseExecutionMemory(size, nativeMemoryConsumer). If Spark threw during the release, native code still treated the bytes as held while used (and NativeMemoryConsumer.getUsed()) had already been lowered, skewing task-end leak checks and Spark's spill ordering.

What changes are included in this PR?

  • spark/src/main/java/org/apache/spark/CometTaskMemoryManager.java: Call internal.releaseExecutionMemory(size, nativeMemoryConsumer) before updating used.addAndGet(-size) and trace logging in releaseMemory(long size).
  • spark/src/test/scala/org/apache/spark/CometTaskMemoryManagerSuite.scala: Added a failed release leaves used memory unchanged, overriding TaskMemoryManager.releaseExecutionMemory to throw once and asserting manager.getUsed, NativeMemoryConsumer.getUsed, and getMemoryConsumptionForThisTask remain unchanged on failure and decrement properly on subsequent releases.

How are these changes tested?

  • Confirmed the new test fails before the fix (0 did not equal 128, 10/11 passed).
  • .\mvnw.cmd -B test -Dtest=none "-Dsuites=org.apache.spark.CometTaskMemoryManagerSuite" "-Dscalastyle.skip=true" "-Dspotless.check.skip=true" — passed (11/11 tests).
  • .\mvnw.cmd -B spotless:check "-DspotlessFiles=.*\.java" — passed (0 violations).
  • .\mvnw.cmd -B spotless:check "-DspotlessFiles=.*CometTaskMemoryManagerSuite\.scala" — 0 scalafmt body violations (only the pre-existing Windows symlink header diff).
  • python dev/ci/check-suites.py — passed.

adivaish and others added 2 commits December 2, 2026 20:25
Call TaskMemoryManager.releaseExecutionMemory before decrementing `used`
in CometTaskMemoryManager.releaseMemory so that if Spark throws, `used`
and NativeMemoryConsumer.getUsed() remain unchanged rather than
prematurely dropping while native code still counts the bytes as held.

Add a regression test in CometTaskMemoryManagerSuite overriding
TaskMemoryManager.releaseExecutionMemory to throw once and verifying
that getUsed() and NativeMemoryConsumer.getUsed() are unchanged on
failure and properly decremented on subsequent normal releases.

Refs apache#7176

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

@andygrove andygrove left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @adityavaish

@andygrove
andygrove enabled auto-merge December 5, 2026 12:42
@github-actions github-actions Bot added bug Something isn't working area:memory Memory pools, reservations, OOM handling labels Dec 5, 2026
@andygrove
andygrove added this pull request to the merge queue Dec 5, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Dec 5, 2026
@sunchao
sunchao added this pull request to the merge queue Dec 5, 2026
Merged via the queue into apache:main with commit 78adfbf Dec 5, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:memory Memory pools, reservations, OOM handling bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CometTaskMemoryManager.releaseMemory lowers used before Spark accepts the release

4 participants