Repository navigation
fix(core): sync model_serve_count gauge on stream request completion - #5041
Merged
qinxuye merged 2 commits intoJun 17, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the decrease_serve_count method in xinference/core/model.py to be asynchronous and records the updated model_serve_count metric. The reviewer suggested guarding the decrement operation with max(0, self._serve_count - 1) to prevent the serve count from becoming negative, which could otherwise bypass rate limiting checks.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
The `decrease_serve_count` method (called by restful_api for streaming requests) only decremented `_serve_count` without writing back to the Prometheus registry. This caused `xinference:model_serve_count` to monotonically increase with every streaming request and never decrease, requiring a worker restart to reset. Make `decrease_serve_count` async and call `record_metrics` after decrementing, mirroring the non-stream path in the decorator's finally block (model.py:147-153). All four call sites in restful_api.py already use `await`, so no caller changes are needed. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Prevent _serve_count from going negative on both the non-stream decorator path and the stream decrease_serve_count path. A negative value would cause the rate limiter check (1 + count <= limit) to incorrectly pass under high load. Addresses review feedback from gemini-code-assist. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
m199369309
force-pushed
the
fix/model-serve-count-stream-gauge
branch
from
June 17, 2026 01:11
75ea108 to
777d5a8
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
decrease_serve_count(called byrestful_api.pyfor streaming requests) only decremented_serve_countwithout writing the new value back to the Prometheus registry.xinference:model_serve_countto monotonically increase with every streaming request and never decrease, making the gauge diverge from the actual in-flight request count over time. A worker restart was the only way to reset it.decrease_serve_countasync and callrecord_metricsafter decrementing, mirroring the non-stream path in the decorator'sfinallyblock (model.py:147-153).All four call sites in
restful_api.py(lines 1222, 1365, 1617, 2468) already useawait model.decrease_serve_count(), so no caller changes are needed.Root cause
_serve_countset✓finally)set✓restful_apifinally→decrease_serve_count)Test plan
xinference-local, load a streaming-capable LLMxinference:model_serve_countreturns to 0 after each requestrequest_limits→ verify gauge does not inflate abnormally🤖 Generated with Claude Code