From 30830232d8481ebb49ec978d25727c930042aa6c Mon Sep 17 00:00:00 2001 From: Nash Aschenbath Date: Wed, 25 Nov 2026 16:10:59 +0800 Subject: [PATCH] Validate reprioritization weights before creating implicit parents --- src/priority/priority.py | 26 ++++++++++++++++---------- test/test_priority.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/src/priority/priority.py b/src/priority/priority.py index 5352d02..6fd0be5 100644 --- a/src/priority/priority.py +++ b/src/priority/priority.py @@ -81,6 +81,15 @@ class PseudoStreamError(PriorityError): pass +def _validate_weight(value: int) -> None: + # RFC 7540 § 5.3.2: "All dependent streams are allocated an integer + # weight between 1 and 256 (inclusive)." + if not isinstance(value, int): + raise BadWeightError("Stream weight should be an integer") + elif not (1 <= value <= 256): + raise BadWeightError("Stream weight must be between 1 and 256 (inclusive)") + + class Stream: """ Priority information for a given stream. @@ -105,12 +114,7 @@ def weight(self) -> int: @weight.setter def weight(self, value: int) -> None: - # RFC 7540 § 5.3.2: "All dependent streams are allocated an integer - # weight between 1 and 256 (inclusive)." - if not isinstance(value, int): - raise BadWeightError("Stream weight should be an integer") - elif not (1 <= value <= 256): - raise BadWeightError("Stream weight must be between 1 and 256 (inclusive)") + _validate_weight(value) self._weight = value def add_child(self, child: "Stream") -> None: @@ -406,15 +410,17 @@ def reprioritize( except KeyError: raise MissingStreamError("Stream %d not in tree" % stream_id) + if depends_on == stream_id: + raise PriorityLoop("Stream %d must not depend on itself" % stream_id) + + _validate_weight(weight) + # Update things in a specific order to make sure the calculation - # behaves properly. Specifically, we first update the weight. Then, + # behaves properly. Specifically, we first validate the weight. Then, # we check whether this stream is being made dependent on one of its # own dependents. Then, we remove this stream from its current parent # and move it to its new parent, taking its children with it. if depends_on: - if depends_on == stream_id: - raise PriorityLoop("Stream %d must not depend on itself" % stream_id) - new_parent = self._get_or_insert_parent(depends_on) cycle = _stream_cycle(new_parent, current_stream) else: diff --git a/test/test_priority.py b/test/test_priority.py index a05e54c..f217e09 100644 --- a/test/test_priority.py +++ b/test/test_priority.py @@ -443,6 +443,37 @@ def test_stream_depending_on_self_is_error(self, stream_id, exclusive): stream_id=stream_id, depends_on=stream_id, exclusive=exclusive ) + @pytest.mark.parametrize("weight", (0, 257, None, "invalid")) + @pytest.mark.parametrize("exclusive", (True, False)) + def test_invalid_reprioritization_does_not_create_parent(self, weight, exclusive): + """Rejected weights leave the streams and scheduling tree unchanged.""" + tree = priority.PriorityTree() + tree.insert_stream(1, weight=32) + tree.insert_stream(5, depends_on=1) + tree.block(5) + + with pytest.raises(priority.BadWeightError): + tree.reprioritize(1, depends_on=3, weight=weight, exclusive=exclusive) + + assert set(tree._streams) == {0, 1, 5} + assert tree._streams[1].weight == 32 + assert tree._streams[1].parent is tree._streams[0] + assert tree._streams[5].parent is tree._streams[1] + assert [next(tree) for _ in range(4)] == [1] * 4 + + @pytest.mark.parametrize("exclusive", (True, False)) + def test_reprioritization_capacity_error_preserves_weight(self, exclusive): + """A valid new weight is not applied when the parent cannot fit.""" + tree = priority.PriorityTree(maximum_streams=2) + tree.insert_stream(1, weight=32) + + with pytest.raises(priority.TooManyStreamsError): + tree.reprioritize(1, depends_on=3, weight=64, exclusive=exclusive) + + assert set(tree._streams) == {0, 1} + assert tree._streams[1].weight == 32 + assert tree._streams[1].parent is tree._streams[0] + @pytest.mark.parametrize("exclusive", (True, False)) @pytest.mark.parametrize("stream_id", (1, 5, 20, 32, 256)) def test_reprioritize_depend_on_self_is_error(self, stream_id, exclusive):