Skip to content

Fix: Add thread-safe locking to NVIDIA GPU detection to prevent metrics endpoint hanging#8

Open
zhukai242 wants to merge 2 commits intogpustack:mainfrom
zhukai242:main
Open

Fix: Add thread-safe locking to NVIDIA GPU detection to prevent metrics endpoint hanging#8
zhukai242 wants to merge 2 commits intogpustack:mainfrom
zhukai242:main

Conversation

@zhukai242
Copy link

Problem:
The metrics endpoint would hang after repeated calls, particularly under concurrent load.
Analysis revealed that pynvml.nvmlInit() was getting stuck,
causing the generate_latest() function to block indefinitely.

Root Cause:
The NVML library is not designed to handle frequent initialization/shutdown cycles.
The original detect() method invoked pynvml.nvmlInit() and pynvml.nvmlShutdown() on every request,
which destabilized the library and led to hanging behavior under load.

Solution:

  1. Implement a class-level thread-safe locking mechanism using threading.Lock()
  2. Refactor the detect() method into a wrapper function that acquires the lock before execution
  3. Rename the original detect() implementation to _detect_impl() (per Python private method conventions)
  4. Enforce exclusive access to GPU detection logic, ensuring only one thread can execute it at a time

This prevents concurrent initialization of the NVML library and resolves the hanging issue in the metrics endpoint.

@gemini-code-assist
Copy link

Summary of Changes

Hello, 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 addresses a critical issue where the metrics endpoint would hang under concurrent load due to pynvml.nvmlInit() getting stuck. The core problem was the frequent initialization and shutdown of the NVML library, which is not designed for such rapid cycles. The solution introduces a thread-safe locking mechanism around the GPU detection process, ensuring that the NVML library is accessed exclusively by one thread at a time, thereby stabilizing its operation and resolving the hanging behavior.

Highlights

  • Thread-Safe Locking: Implemented a class-level thread-safe locking mechanism using threading.Lock() to manage access to NVIDIA GPU detection logic.
  • Method Refactoring: Refactored the detect() method into a wrapper function that acquires the lock before execution, and renamed the original implementation to _detect_impl() following Python private method conventions.
  • Concurrent Access Prevention: Enforced exclusive access to the GPU detection logic, ensuring only one thread can execute it at a time to prevent concurrent initialization of the NVML library.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • gpustack_runtime/detector/nvidia.py
    • Imported the threading module.
    • Added a class-level _detect_lock using threading.Lock().
    • Renamed the existing detect method to _detect_impl.
    • Created a new detect method that acquires _detect_lock before calling _detect_impl and includes a new docstring.
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

@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 address a critical hanging issue in the metrics endpoint by introducing a thread-safe lock for NVIDIA GPU detection. However, the current implementation is vulnerable to Denial of Service (DoS) due to incomplete locking. The threading.Lock used can lead to deadlocks in nested calls and should be replaced with threading.RLock. Furthermore, the fix is incomplete as other methods like is_supported() and get_topology() that interact with the NVML library are not protected, leaving them susceptible to the same concurrency issues.

Comment on lines +96 to +99
with self._detect_lock:
result = self._detect_impl()

return result

Choose a reason for hiding this comment

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

medium

This block can be simplified by returning the result of _detect_impl() directly from within the with statement. This makes the code more concise and avoids an unnecessary intermediate variable.

Suggested change
with self._detect_lock:
result = self._detect_impl()
return result
with self._detect_lock:
return self._detect_impl()

@thxCode
Copy link
Contributor

thxCode commented Mar 10, 2026

  1. Make lint green. You can do make lint on the laptop at first.
  2. Check the review comments generated by Gemini.
  3. Provide evidence and the circumstances under which this problem occurred. How much has the corresponding solution been optimized?
  4. If the issue stems from GPUStack calls, you can increase the GPUSTACK_WORKER_STATUS_SYNC_INTERVAL env in the GPUStack worker to avoid this. If the problem is caused by excessive metric scraping frequency, you can adjust your metric scraping settings.

@thxCode
Copy link
Contributor

thxCode commented Mar 13, 2026

Since we also receive reports of the NVML calling hang: gpustack/gpustack#4862, gpustack/gpustack#4878, gpustack/gpustack#4825, we have fixed this in HEAD. Can you have a try?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants