Skip to content

Fixed memory leak caused by metrics remaining due to race conditions#4822

Merged
volcano-sh-bot merged 1 commit intovolcano-sh:masterfrom
halcyon-r:master
Dec 19, 2025
Merged

Fixed memory leak caused by metrics remaining due to race conditions#4822
volcano-sh-bot merged 1 commit intovolcano-sh:masterfrom
halcyon-r:master

Conversation

@halcyon-r
Copy link
Copy Markdown
Contributor

Verify that the job still exists before set job's metrics.

What type of PR is this?

/kind bug

What this PR does / why we need it:

Before writing the unschedule_task_count, job_share, and job_retry_counts metrics, check if the job exists to prevent leftover metrics.

Which issue(s) this PR fixes:

Fixes #4821

Special notes for your reviewer:

Does this PR introduce a user-facing change?

NONE

@volcano-sh-bot volcano-sh-bot added the kind/bug Categorizes issue or PR as related to a bug. label Dec 16, 2025
@volcano-sh-bot volcano-sh-bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Dec 16, 2025
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @halcyon-r, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a bug where scheduler metrics, specifically job_share, job_retry_counts, and unschedule_task_count, could continue to be updated or registered for jobs that had already terminated. This race condition led to the accumulation of stale metric data, effectively causing a memory leak. The solution introduces a new mechanism to verify a job's active status before any metric updates, ensuring that only relevant, existing jobs contribute to the reported metrics and maintaining a cleaner system state.

Highlights

  • New Job Termination Check: Introduced a new IsJobTerminated function within the scheduler cache to reliably determine if a job has been terminated or no longer exists, preventing operations on stale job data.
  • Metric Update Safeguards: Implemented conditional checks in the DRF (Dominant Resource Fairness) and Gang scheduling plugins to ensure that job_share, job_retry_counts, and unschedule_task_count metrics are only updated for active, non-terminated jobs.
  • API Exposure: The IsJobTerminated functionality is exposed through the Cache interface and the Session framework, making it accessible for consistent use across different scheduler components.
  • Memory Leak Fix: This change directly addresses a memory leak by preventing the accumulation of metric data for jobs that have already completed or been removed, ensuring cleaner state management and more accurate reporting.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request aims to fix a memory leak by preventing metric updates for terminated jobs. The approach of adding a check before updating metrics is correct and has been applied to drf and gang plugins. However, I've identified a critical data race in the new IsJobTerminated function that needs to be addressed to ensure thread safety.

@halcyon-r halcyon-r force-pushed the master branch 3 times, most recently from d695610 to 9c29153 Compare December 16, 2025 08:50
Copy link
Copy Markdown
Member

@hajnalmt hajnalmt left a comment

Choose a reason for hiding this comment

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

Looks good to me, I had one question only.

Checked the occurancies too and the commit is fine (we don't update these metrics elsewehere which is surprising).

SharedDRAManager() framework.SharedDRAManager

// IsJobTerminated returns if the job was terminated
IsJobTerminated(jobId string) bool
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.

Why haven't you used (jobId api.jobId) here too?
You would have spared the casting in cache.go

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are right. I‘ll optimize this part.

}

func (sc *SchedulerCache) IsJobTerminated(jobId string) bool {
sc.Mutex.Lock()
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.

I have created an enhacement request for this as this should be a Read Lock on the long run.
See: #4824

Verify that the job still exists before set job's metrics.

Signed-off-by: hairuiyang <hairuiyang@deeproute.ai>
Copy link
Copy Markdown
Member

@hajnalmt hajnalmt left a comment

Choose a reason for hiding this comment

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

/lgtm

Thank you for the contribution and the optimization!

@volcano-sh-bot volcano-sh-bot added the lgtm Indicates that a PR is ready to be merged. label Dec 17, 2025
Copy link
Copy Markdown
Member

@hzxuzhonghu hzxuzhonghu left a comment

Choose a reason for hiding this comment

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

Good catch

@JesseStutler Should we backport to previous release

@volcano-sh-bot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: hajnalmt, hzxuzhonghu

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@volcano-sh-bot volcano-sh-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Dec 19, 2025
@volcano-sh-bot volcano-sh-bot merged commit 5a81d45 into volcano-sh:master Dec 19, 2025
22 checks passed
@JesseStutler
Copy link
Copy Markdown
Member

@halcyon-r So it's the same issue here but with different solutions? #4760 I prefer to check if the job still exist rather than use ttl way, but also let @fengruotj takes a look

SharedDRAManager() framework.SharedDRAManager

// IsJobTerminated returns if the job was terminated
IsJobTerminated(jobId api.JobID) bool
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.

I feel that we are too arbitrary to add a method to the cache interface here, the cache is not like the design that an interface should have

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. kind/bug Categorizes issue or PR as related to a bug. lgtm Indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory leak caused by leftover metrics data

5 participants