Fix: Add thread-safe locking to NVIDIA GPU detection to prevent metrics endpoint hanging#8
Fix: Add thread-safe locking to NVIDIA GPU detection to prevent metrics endpoint hanging#8zhukai242 wants to merge 2 commits intogpustack:mainfrom
Conversation
Summary of ChangesHello, 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
🧠 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
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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.
| with self._detect_lock: | ||
| result = self._detect_impl() | ||
|
|
||
| return result |
There was a problem hiding this comment.
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.
| with self._detect_lock: | |
| result = self._detect_impl() | |
| return result | |
| with self._detect_lock: | |
| return self._detect_impl() |
|
|
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? |
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:
This prevents concurrent initialization of the NVML library and resolves the hanging issue in the metrics endpoint.