Skip to content

fix(core): sync model_serve_count gauge on stream request completion - #5041

Merged
qinxuye merged 2 commits into
xorbitsai:mainfrom
m199369309:fix/model-serve-count-stream-gauge
Jun 17, 2026
Merged

qinxuye merged 2 commits into
xorbitsai:mainfrom
m199369309:fix/model-serve-count-stream-gauge

Conversation

@m199369309

Copy link
Copy Markdown
Collaborator

Summary

  • decrease_serve_count (called by restful_api.py for streaming requests) only decremented _serve_count without writing the new value back to the Prometheus registry.
  • This caused xinference:model_serve_count to 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.
  • Fix: 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 (lines 1222, 1365, 1617, 2468) already use await model.decrease_serve_count(), so no caller changes are needed.

Root cause

Path _serve_count Prometheus gauge
Request enters decorator +1 set ✓
Non-stream ends (decorator finally) -1 set ✓
Stream ends (restful_api finally → decrease_serve_count) -1 not set ✗

Test plan

  • Start xinference-local, load a streaming-capable LLM
  • Send non-stream requests → verify xinference:model_serve_count returns to 0 after each request
  • Send stream requests → verify gauge returns to 0 after each stream completes (primary verification)
  • Send concurrent stream requests (N=4) → verify gauge returns to 0 after all streams complete
  • Trigger client disconnect mid-stream → verify gauge returns to 0
  • Exceed request_limits → verify gauge does not inflate abnormally

🤖 Generated with Claude Code

@XprobeBot XprobeBot added the bug Something isn't working label Jun 16, 2026
@XprobeBot XprobeBot added this to the v2.x milestone Jun 16, 2026

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

Copy link
Copy Markdown
Contributor

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 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.

Comment thread xinference/core/model.py
m199369309 and others added 2 commits June 17, 2026 09:11
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
m199369309 force-pushed the fix/model-serve-count-stream-gauge branch from 75ea108 to 777d5a8 Compare June 17, 2026 01:11

@qinxuye qinxuye left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@qinxuye
qinxuye merged commit df0280d into xorbitsai:main Jun 17, 2026
8 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants