fix: reject negative resource slot quantity#11365
Merged
HyeockJinKim merged 2 commits intomainfrom Apr 28, 2026
Merged
Conversation
…e_value
Negative values for resource slots (e.g. cpu: -1) previously passed
through ResourceSlot.from_user_input/from_policy without complaint and
were only rejected later by the scheduler. Add a finite-negative check
to _normalize_value so the API boundary fails fast with HTTP 400 via
the new InvalidResourceSlotQuantity exception. Positive infinity
("unlimited") semantics are preserved by gating the check on
is_finite().
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR makes the API reject negative resource slot quantities earlier (at ResourceSlot._normalize_value) by introducing a dedicated InvalidResourceSlotQuantity HTTP 400 error, with accompanying unit tests.
Changes:
- Add
InvalidResourceSlotQuantity(BackendAIError+web.HTTPBadRequest) for negative slot quantities. - Enforce a finite-negative guard in
ResourceSlot._normalize_value. - Add unit tests to ensure negative quantities are rejected.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tests/unit/common/test_types.py | Adds unit tests asserting negative resource slot quantities raise InvalidResourceSlotQuantity. |
| src/ai/backend/common/types.py | Adds finite-negative validation in ResourceSlot._normalize_value and wires in the new exception. |
| src/ai/backend/common/exception.py | Introduces InvalidResourceSlotQuantity as a structured HTTP 400 Backend.AI error. |
| changes/11365.fix.md | Adds a changelog entry describing the new early rejection behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| class InvalidResourceSlotQuantity(BackendAIError, web.HTTPBadRequest): | ||
| error_type = "https://api.backend.ai/probs/invalid-resource-slot-quantity" | ||
| error_title = "Invalid resource slot quantity." |
|
|
||
|
|
||
| def test_resource_slot_rejects_negative_bytes() -> None: | ||
| with pytest.raises(InvalidResourceSlotQuantity, match="cannot be negative"): |
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
cpu: -1) previously slipped pastResourceSlot.from_user_input/from_policyand were only rejected by the scheduler later in the pipeline.ResourceSlot._normalize_valueso the API boundary fails fast with HTTP 400 via the newInvalidResourceSlotQuantityexception (BackendAIError+web.HTTPBadRequest).inf("unlimited") semantics preserved by gating the check onis_finite().Test plan
pants test tests/unit/common/test_types.py(added 3 negative-rejection tests)pants test --changed-since=HEAD --changed-dependents=transitive(all dependent tests pass)pants fmt / lint / checkcleancpu: -1returns HTTP 400 instead of being scheduled