From 4451ec6373bb9dd47ab32d86f548622287c2dbb0 Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Mon, 13 Jul 2026 11:48:14 -0700 Subject: [PATCH 1/3] fix(http-client-python): make Model.__new__ lazy init thread-safe without locks Concurrent first-time model construction could raise "RuntimeError: dictionary changed size during iteration" in Model.__new__, which deserialization silently swallowed, returning raw dicts instead of models. Iterate a snapshot of each class __dict__ and publish _attr_to_rest_field atomically, guarded by the class's own __dict__. Fixes #11234 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- ...el-new-thread-safety-2026-7-13-11-33-46.md | 7 ++ .../codegen/templates/model_base.py.jinja2 | 30 +++-- .../tests/unit/test_model_base_concurrency.py | 116 ++++++++++++++++++ 3 files changed, 143 insertions(+), 10 deletions(-) create mode 100644 .chronus/changes/fix-model-new-thread-safety-2026-7-13-11-33-46.md create mode 100644 packages/http-client-python/tests/unit/test_model_base_concurrency.py diff --git a/.chronus/changes/fix-model-new-thread-safety-2026-7-13-11-33-46.md b/.chronus/changes/fix-model-new-thread-safety-2026-7-13-11-33-46.md new file mode 100644 index 00000000000..a328a895ea9 --- /dev/null +++ b/.chronus/changes/fix-model-new-thread-safety-2026-7-13-11-33-46.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@typespec/http-client-python" +--- + +Fix a race condition in the generated `_model_base.py` where concurrent first-time model construction could raise `RuntimeError: dictionary changed size during iteration` in `Model.__new__`. Because deserialization swallows that error, affected responses were silently returned as raw dicts (or models with raw-dict nested fields) instead of deserialized models. The lazy metadata initialization is now thread-safe without locks: it iterates a snapshot of each class `__dict__` and publishes `_attr_to_rest_field` atomically, guarded by the class's own `__dict__`. diff --git a/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 b/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 index 54821400d89..fbe2638fc0e 100644 --- a/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 +++ b/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 @@ -881,9 +881,6 @@ def _build_xml_field_plan( # pylint: disable=docstring-missing-return, docstrin # pylint: enable=docstring-missing-param class Model(_MyMutableMapping): _is_model = True - # label whether current class's _attr_to_rest_field has been calculated - # could not see _attr_to_rest_field directly because subclass inherits it from parent class - _calculated: set[str] = set() def __init__(self, *args: typing.Any, **kwargs: typing.Any) -> None: class_name = self.__class__.__name__ @@ -1024,18 +1021,28 @@ class Model(_MyMutableMapping): return Model(self.__dict__) def __new__(cls, *args: typing.Any, **kwargs: typing.Any) -> Self: - if f"{cls.__module__}.{cls.__qualname__}" not in cls._calculated: + # First time we build this class, compute its field metadata and cache it on the class. + # Check `cls.__dict__` (this class's own attrs), not a shared set: subclasses inherit + # `_attr_to_rest_field`, so this is only True once *this* class is done. No lock needed. + if "_attr_to_rest_field" not in cls.__dict__: # we know the last nine classes in mro are going to be 'Model', '_MyMutableMapping', 'MutableMapping', # 'Mapping', 'Collection', 'Sized', 'Iterable', 'Container' and 'object' mros = cls.__mro__[:-9][::-1] # ignore parents, and reverse the mro order + # Read from a copy (`dict(...)`), not the original: another thread building a parent class + # may write its cache into that parent's attrs at the same time, and reading the original + # while it changes raises "dictionary changed size during iteration" (silently swallowed + # during deserialization, returning raw JSON). The copy has the same fields anyway. attr_to_rest_field: dict[str, _RestField] = { # map attribute name to rest_field property - k: v for mro_class in mros for k, v in mro_class.__dict__.items() if k[0] != "_" and hasattr(v, "_type") + k: v + for mro_class in mros + for k, v in dict(mro_class.__dict__).items() + if k[0] != "_" and hasattr(v, "_type") } annotations = { k: v for mro_class in mros if hasattr(mro_class, "__annotations__") - for k, v in mro_class.__annotations__.items() + for k, v in dict(mro_class.__annotations__).items() } for attr, rf in attr_to_rest_field.items(): rf._module = cls.__module__ @@ -1043,17 +1050,20 @@ class Model(_MyMutableMapping): rf._type = rf._get_deserialize_callable_from_annotation(annotations.get(attr, None)) if not rf._rest_name_input: rf._rest_name_input = attr - cls._attr_to_rest_field: dict[str, _RestField] = dict(attr_to_rest_field.items()) {% if code_model.has_padded_model_property %} cls._backcompat_attr_to_rest_field: dict[str, _RestField] = { - Model._get_backcompat_attribute_name(cls._attr_to_rest_field, attr): rf for attr, rf in cls - ._attr_to_rest_field.items() + Model._get_backcompat_attribute_name(attr_to_rest_field, attr): rf + for attr, rf in attr_to_rest_field.items() } {% endif %} # Build XML field plan for fast _init_from_xml (only for XML models) if getattr(cls, "_xml", None): cls._xml_field_plan = _build_xml_field_plan(cls, attr_to_rest_field) - cls._calculated.add(f"{cls.__module__}.{cls.__qualname__}") + # Set `_attr_to_rest_field` LAST: it's the "done" flag checked above, so the extra maps + # are set first and are ready before another thread sees it. Built as a local and assigned + # in one step, never mutated after - readers always see a complete map. Two threads racing + # here just build the same map twice (harmless), so no lock is needed. + cls._attr_to_rest_field: dict[str, _RestField] = dict(attr_to_rest_field) return super().__new__(cls) diff --git a/packages/http-client-python/tests/unit/test_model_base_concurrency.py b/packages/http-client-python/tests/unit/test_model_base_concurrency.py new file mode 100644 index 00000000000..c15ca2cacf3 --- /dev/null +++ b/packages/http-client-python/tests/unit/test_model_base_concurrency.py @@ -0,0 +1,116 @@ +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ +"""Concurrency regression tests for ``Model`` lazy metadata initialization. + +Regression coverage for https://github.com/microsoft/typespec/issues/11234 +(upstream Azure/azure-sdk-for-python#47426). + +``Model.__new__`` lazily computes each class's ``_attr_to_rest_field`` metadata on +first construction. The original implementation iterated a live class ``__dict__`` +while another thread published its own metadata into that same ``__dict__``, raising +``RuntimeError: dictionary changed size during iteration``. That error was then +swallowed by ``_deserialize_default``, so concurrent first-time deserialization +silently returned raw ``dict`` objects (or models with raw-dict nested fields) +instead of fully deserialized models. + +Each trial below defines *fresh* model classes, so their lazy metadata is +uninitialized - exactly like the first response of a freshly started process - then +deserializes the same payload from many threads at once. +""" +import copy +import sys +import threading +from typing import List, Optional + +import pytest + +from specialwords._utils.model_base import Model, rest_field, _deserialize + + +N_THREADS = 32 +N_TRIALS = 20 + + +def _run_concurrent_deserialization(model_cls, payload, verify): + """Deserialize ``payload`` into ``model_cls`` from N_THREADS threads simultaneously. + + Returns the list of per-thread corruption descriptions (empty when all results + are fully deserialized). + """ + results = [None] * N_THREADS + barrier = threading.Barrier(N_THREADS) + + def work(i): + # Independent input per thread, like real HTTP responses. + local_payload = copy.deepcopy(payload) + # Release all threads together to maximize contention on the first-time + # lazy initialization in Model.__new__. + barrier.wait() + results[i] = _deserialize(model_cls, local_payload) + + threads = [threading.Thread(target=work, args=(i,)) for i in range(N_THREADS)] + for t in threads: + t.start() + for t in threads: + t.join() + + corruptions = [] + for r in results: + problem = verify(r) + if problem is not None: + corruptions.append(problem) + return corruptions + + +def test_concurrent_first_deserialization_is_thread_safe(): + """First-time concurrent deserialization always returns fully deserialized models.""" + + # Widen the race window (the original bug reproduces at the default interval too, + # this just makes it deterministic). Restored in the finally block. + old_switch_interval = sys.getswitchinterval() + sys.setswitchinterval(1e-6) + try: + for trial in range(N_TRIALS): + # Fresh classes each trial => uninitialized lazy metadata, like a new process. + class Inner(Model): + name: str = rest_field() + value: int = rest_field() + + class Outer(Model): + inner: Inner = rest_field() + items: List[Inner] = rest_field() + maybe: Optional[Inner] = rest_field() + + payload = { + "inner": {"name": "a", "value": 1}, + "items": [{"name": "b", "value": 2}, {"name": "c", "value": 3}], + "maybe": {"name": "d", "value": 4}, + } + + def verify(result): + if not isinstance(result, Outer): + return f"got {type(result).__name__} instead of Outer" + try: + if not isinstance(result.inner, Inner): + return f"inner is {type(result.inner).__name__}, not Inner" + if (result.inner.name, result.inner.value) != ("a", 1): + return f"inner has wrong values: {result.inner}" + if not all(isinstance(it, Inner) for it in result.items): + return "items contains a raw dict instead of Inner" + if [it.name for it in result.items] != ["b", "c"]: + return f"items has wrong values: {result.items}" + if not isinstance(result.maybe, Inner): + return f"maybe is {type(result.maybe).__name__}, not Inner" + except AttributeError as exc: # nested field is a raw dict + return f"partially deserialized model: {exc}" + return None + + corruptions = _run_concurrent_deserialization(Outer, payload, verify) + assert not corruptions, ( + f"trial {trial}: {len(corruptions)}/{N_THREADS} results corrupted, " + f"e.g. {corruptions[0]}" + ) + finally: + sys.setswitchinterval(old_switch_interval) From d14af9b46e282ff4b2a3835140c93bef5eb2c924 Mon Sep 17 00:00:00 2001 From: l0lawrence Date: Tue, 10 Nov 2026 13:27:53 -0800 Subject: [PATCH 2/3] fix(http-client-python): snapshot discriminator scans Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../codegen/templates/model_base.py.jinja2 | 2 +- .../tests/unit/test_model_base_concurrency.py | 189 +++++++++++++++++- 2 files changed, 179 insertions(+), 12 deletions(-) diff --git a/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 b/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 index a6b9a50de24..cce55abd9fb 100644 --- a/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 +++ b/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 @@ -1102,7 +1102,7 @@ class Model(_MyMutableMapping): @classmethod def _get_discriminator(cls, exist_discriminators) -> typing.Optional["_RestField"]: - for v in cls.__dict__.values(): + for v in dict(cls.__dict__).values(): if isinstance(v, _RestField) and v._is_discriminator and v._rest_name not in exist_discriminators: return v return None diff --git a/packages/http-client-python/tests/unit/test_model_base_concurrency.py b/packages/http-client-python/tests/unit/test_model_base_concurrency.py index c15ca2cacf3..6686a1ebdc4 100644 --- a/packages/http-client-python/tests/unit/test_model_base_concurrency.py +++ b/packages/http-client-python/tests/unit/test_model_base_concurrency.py @@ -15,24 +15,32 @@ silently returned raw ``dict`` objects (or models with raw-dict nested fields) instead of fully deserialized models. -Each trial below defines *fresh* model classes, so their lazy metadata is -uninitialized - exactly like the first response of a freshly started process - then -deserializes the same payload from many threads at once. +The tests cover controlled parent/subclass initialization as well as concurrent +deserialization of fresh model classes. """ + import copy import sys import threading +import uuid from typing import List, Optional import pytest -from specialwords._utils.model_base import Model, rest_field, _deserialize - +from specialwords._utils.model_base import Model, rest_field, rest_discriminator, _RestField, _deserialize N_THREADS = 32 N_TRIALS = 20 +def _give_unique_qualnames(*model_classes): + # The original cache used module and qualname, so avoid testing a cache collision + # instead of a race when creating fresh classes. + suffix = uuid.uuid4().hex + for model_class in model_classes: + model_class.__qualname__ = f"{model_class.__qualname__}_{suffix}" + + def _run_concurrent_deserialization(model_cls, payload, verify): """Deserialize ``payload`` into ``model_cls`` from N_THREADS threads simultaneously. @@ -64,11 +72,170 @@ def work(i): return corruptions +@pytest.mark.parametrize("deserialize", [False, True], ids=["constructor", "deserialize"]) +def test_parent_and_child_first_initialization_does_not_invalidate_iteration(deserialize): + """Publishing parent metadata cannot invalidate the child's field iteration.""" + child_scanning_parent = threading.Event() + resume_child = threading.Event() + parent_finished = threading.Event() + results = [] + parent_results = [] + errors = [] + + class PauseDuringScan: + # Field discovery probes `_type`; pause the child, then report this is not a field. + @property + def _type(self): + if threading.current_thread() is child_thread: + child_scanning_parent.set() + if not resume_child.wait(timeout=10): + raise TimeoutError("Parent initialization did not finish") + raise AttributeError("_type") + + class Parent(Model): + pause = PauseDuringScan() + name: str = rest_field() + + class Child(Parent): + extra: int = rest_field() + + _give_unique_qualnames(Parent, Child) + + def build_child(): + try: + payload = {"name": "child", "extra": 42} + results.append(_deserialize(Child, payload) if deserialize else Child(payload)) + except Exception as exc: + errors.append(exc) + + def build_parent(): + try: + parent_results.append(Parent({"name": "parent"})) + except Exception as exc: + errors.append(exc) + finally: + parent_finished.set() + + child_thread = threading.Thread(target=build_child, daemon=True) + parent_thread = threading.Thread(target=build_parent, daemon=True) + child_thread.start() + try: + assert child_scanning_parent.wait(timeout=10), "Child did not start scanning Parent" + parent_thread.start() + # A synchronized implementation may hold the parent until the child resumes. + parent_finished.wait(timeout=5) + finally: + resume_child.set() + child_thread.join(timeout=10) + if parent_thread.ident is not None: + parent_thread.join(timeout=10) + + assert not child_thread.is_alive(), "Child initialization did not finish" + assert not parent_thread.is_alive(), "Parent initialization did not finish" + if errors: + raise errors[0] + assert len(parent_results) == 1 + assert parent_results[0].name == "parent" + assert len(results) == 1 + child = results[0] + assert isinstance(child, Child) + assert child.name == "child" + assert child.extra == 42 + + +def test_models_with_same_qualname_initialize_independently(): + def make_model(): + class Repeated(Model): + value: int = rest_field() + + return Repeated + + first = make_model() + second = make_model() + assert first is not second + assert first.__qualname__ == second.__qualname__ + assert first({"value": 1}).value == 1 + assert second({"value": 2}).value == 2 + + +def test_discriminator_scan_does_not_fail_during_first_initialization(): + """Publishing model metadata cannot invalidate a discriminator scan.""" + discriminator_scanning = threading.Event() + resume_discriminator_scan = threading.Event() + parent_finished = threading.Event() + discriminator_thread = None + results = [] + parent_results = [] + errors = [] + + class PausingField(_RestField): + def __init__(self): + super().__init__(name="probe", is_discriminator=False) + + @property + def _is_discriminator(self): + if threading.current_thread() is discriminator_thread: + discriminator_scanning.set() + if not resume_discriminator_scan.wait(timeout=10): + raise TimeoutError("Parent initialization did not finish") + return self.__dict__["_is_discriminator"] + + @_is_discriminator.setter + def _is_discriminator(self, value): + self.__dict__["_is_discriminator"] = value + + class Animal(Model): + __mapping__: dict[str, Model] = {} + probe: str = PausingField() + kind: str = rest_discriminator(name="kind") + + class Cat(Animal, discriminator="cat"): + pass + + _give_unique_qualnames(Animal, Cat) + + def deserialize_animal(): + try: + results.append(_deserialize(Animal, {"kind": "cat"})) + except Exception as exc: + errors.append(exc) + + def initialize_animal(): + try: + parent_results.append(Animal({"kind": "cat"})) + except Exception as exc: + errors.append(exc) + finally: + parent_finished.set() + + discriminator_thread = threading.Thread(target=deserialize_animal, daemon=True) + parent_thread = threading.Thread(target=initialize_animal, daemon=True) + discriminator_thread.start() + try: + assert discriminator_scanning.wait(timeout=10), "Discriminator scan did not start" + parent_thread.start() + # A synchronized implementation may hold the initializer until the scan resumes. + parent_finished.wait(timeout=5) + finally: + resume_discriminator_scan.set() + discriminator_thread.join(timeout=10) + if parent_thread.ident is not None: + parent_thread.join(timeout=10) + + assert not discriminator_thread.is_alive(), "Discriminator scan did not finish" + assert not parent_thread.is_alive(), "Parent initialization did not finish" + if errors: + raise errors[0] + assert len(parent_results) == 1 + assert len(results) == 1 + assert isinstance(results[0], Cat) + assert results[0].kind == "cat" + + def test_concurrent_first_deserialization_is_thread_safe(): """First-time concurrent deserialization always returns fully deserialized models.""" - # Widen the race window (the original bug reproduces at the default interval too, - # this just makes it deterministic). Restored in the finally block. + # Increase contention and restore the process-wide interval afterwards. old_switch_interval = sys.getswitchinterval() sys.setswitchinterval(1e-6) try: @@ -83,6 +250,8 @@ class Outer(Model): items: List[Inner] = rest_field() maybe: Optional[Inner] = rest_field() + _give_unique_qualnames(Inner, Outer) + payload = { "inner": {"name": "a", "value": 1}, "items": [{"name": "b", "value": 2}, {"name": "c", "value": 3}], @@ -108,9 +277,7 @@ def verify(result): return None corruptions = _run_concurrent_deserialization(Outer, payload, verify) - assert not corruptions, ( - f"trial {trial}: {len(corruptions)}/{N_THREADS} results corrupted, " - f"e.g. {corruptions[0]}" - ) + if corruptions: + pytest.fail(f"trial {trial}: {len(corruptions)}/{N_THREADS} results corrupted, e.g. {corruptions[0]}") finally: sys.setswitchinterval(old_switch_interval) From 2ffa7321442bbbe8f60d814757aaa96450cf4b2a Mon Sep 17 00:00:00 2001 From: l0lawrence Date: Thu, 12 Nov 2026 10:03:07 -0800 Subject: [PATCH 3/3] test(http-client-python): benchmark complex deserialization Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../unit/test_model_base_serialization.py | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/packages/http-client-python/tests/unit/test_model_base_serialization.py b/packages/http-client-python/tests/unit/test_model_base_serialization.py index 93c1aab2270..2a1d311bf51 100644 --- a/packages/http-client-python/tests/unit/test_model_base_serialization.py +++ b/packages/http-client-python/tests/unit/test_model_base_serialization.py @@ -7,6 +7,8 @@ import decimal import json import datetime +import time +import uuid from pathlib import Path from typing import ( Any, @@ -4228,6 +4230,98 @@ def test_multi_layer_discriminator(): assert AnotherDog(name="Rex", trained=True, breed="German Shepherd") == model_dog +def test_complex_model_deserialization(request): + """Exercise and time repeated deserialization of a complex model graph. + + The recorded warm per-operation duration can be compared when this test is run + against different generated runtimes. It is intentionally not a CI threshold. + """ + + class Address(Model): + street: str = rest_field() + city: str = rest_field() + zip_code: Optional[str] = rest_field(name="zipCode") + + class Tag(Model): + key: str = rest_field() + value: str = rest_field() + + class Animal(Model): + __mapping__: dict[str, Model] = {} + kind: str = rest_discriminator(name="kind") + name: str = rest_field() + age: Optional[int] = rest_field() + tags: list[Tag] = rest_field() + + class Dog(Animal, discriminator="dog"): + kind: Literal["dog"] = rest_discriminator(name="kind") + barks: bool = rest_field() + + class Cat(Animal, discriminator="cat"): + __mapping__: dict[str, Model] = {} + kind: Literal["cat"] = rest_discriminator(name="kind") + breed: str = rest_discriminator(name="breed") + lives: int = rest_field() + + class HouseCat(Cat, discriminator="house"): + breed: Literal["house"] = rest_discriminator(name="breed") + indoor: bool = rest_field() + + class Owner(Model): + id: str = rest_field() + name: str = rest_field() + created: datetime.datetime = rest_field(format="rfc3339") + home: Address = rest_field() + addresses: dict[str, Address] = rest_field() + pets: list[Animal] = rest_field() + favorite: Optional[Animal] = rest_field() + scores: list[float] = rest_field() + + suffix = uuid.uuid4().hex + for model_class in (Address, Tag, Animal, Dog, Cat, HouseCat, Owner): + model_class.__qualname__ = f"{model_class.__qualname__}_{suffix}" + + tags = [{"key": f"k{i}", "value": f"v{i}"} for i in range(3)] + dog = {"kind": "dog", "name": "Rex", "age": 4, "tags": tags, "barks": True} + cat = { + "kind": "cat", + "breed": "house", + "name": "Milo", + "age": 3, + "tags": tags, + "lives": 9, + "indoor": True, + } + data = { + "id": "owner-1", + "name": "Ada", + "created": "2026-11-12T09:30:00Z", + "home": {"street": "1 Main", "city": "Seattle", "zipCode": "98101"}, + "addresses": {f"a{i}": {"street": f"{i} Pine", "city": "Seattle", "zipCode": None} for i in range(5)}, + "pets": [dict(cat if i % 2 else dog) for i in range(20)], + "favorite": cat, + "scores": [1.5, 2.5, 3.5], + } + + # Initialize lazy model metadata before timing warm deserialization. + result = _deserialize(Owner, data) + iterations = 100 + start = time.perf_counter() + for _ in range(iterations): + result = _deserialize(Owner, data) + elapsed = time.perf_counter() - start + + request.node.user_properties.append( + ("complex_model_deserialize_us_per_operation", elapsed / iterations * 1_000_000) + ) + assert result.created == datetime.datetime(2026, 11, 12, 9, 30, tzinfo=datetime.timezone.utc) + assert isinstance(result.home, Address) + assert all(isinstance(address, Address) for address in result.addresses.values()) + assert all(isinstance(pet, Dog if i % 2 == 0 else HouseCat) for i, pet in enumerate(result.pets)) + assert isinstance(result.favorite, HouseCat) + assert result.pets[0].tags[0].value == "v0" + + def test_array_encode_comma_delimited(): """Test commaDelimited format for array of strings"""