Backtestspydanticrun-20261002-175417#13419

Issue #13419 Merged fix PR #13438 Run 2 Oct 2026 · run-20261002-175417 pydantic/pydantic

"dictionary changed size during iteration" during model_rebuild

Worse

The merged fix was judged better than our patch.

In this judgment, Patch B = our agent, Patch A = the merged fix.

The root cause is that `model_rebuild()` is not thread-safe. When several threads instantiate an incomplete model, each one enters `model_rebuild()` and mutates class `__dict__`s (delattr/setattr of the schema, validator and serializer, and the `__pydantic_complete__` flag). Meanwhile another thread's rebuild is iterating those same live `vars(cls)` mappings during annotation evaluation.

Patch A fixes this directly. It serializes rebuilds with a global reentrant lock, keeps a cheap unlocked fast path, and re-checks completeness inside the lock. That removes both symptoms the issue names: the `RuntimeError` and the sibling `AttributeError` from the delattr block. It also prevents redundant concurrent rebuilds and duplicate `__pydantic_on_complete__` hook calls. Using an RLock that is global rather than per-class is a reasoned choice, since rebuilds nest during generic parametrization and per-class locks could deadlock; the patch documents this. Including `MockCoreSchema` in the isinstance check further reduces exposure of unlocked readers.

Patch A's tests cover the reentrancy case and the 'already rebuilt by another thread' case, deterministically and with hook-call assertions. They do not literally reproduce the dict-iteration error. Weaknesses of A: `rebuild_dataclass` is left unlocked, and holding a global lock adds some contention.

Patch B treats the symptoms. It snapshots mappingproxies before iterating in `LazyLocalNamespace.data` and `_decorator_infos_for_class`, and it swallows `AttributeError` on delattr. Concurrent rebuilds of the same class still run in parallel. Threads still flip `__pydantic_complete__`, replace validators while other threads use them, and duplicate work and hooks. Any other place that iterates a live class dict, or reads attributes mid-rebuild, can still fail.

Patch B's tests are good: settrace-based deterministic reproductions that would fail before the change, plus a concurrency smoke test. But they validate patched symptoms rather than establishing thread-safety.

On the primary criterion of correctness at the root cause, A is the more robust fix, which outweighs B's stronger test reproduction.

The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.

Solve

13m 14s

Wall-clock time in the container

Full test suite

No new failures

12,264 passed · 501 skipped · 12,765 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

232vs 168 merged

Changed lines, ours vs merged

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

This just hit us in production:

Traceback (most recent call last):
  ...
  File "fastapi/routing.py", line 120, in app
    response = await f(request)
  File "fastapi/routing.py", line 674, in app
    raw_response = await run_endpoint_function(
  File "fastapi/routing.py", line 330, in run_endpoint_function
    return await run_in_threadpool(dependant.call, **values)
  File "starlette/concurrency.py", line 34, in run_in_threadpool
    return await anyio.to_thread.run_sync(func)
  File "anyio/to_thread.py", line 63, in run_sync
    return await get_async_backend().run_sync_in_worker_thread(
  File "anyio/_backends/_asyncio.py", line 2518, in run_sync_in_worker_thread
    return await future
  File "anyio/_backends/_asyncio.py", line 1002, in run
    result = context.run(func, *args)
  ...
  File "...", line 157, in ...
    MyPydanticClass(
  File "pydantic/_internal/_mock_val_ser.py", line 165, in handler
    if cls.model_rebuild(raise_errors=False, _parent_namespace_depth=5) is not False:
  File "pydantic/main.py", line 683, in model_rebuild
    return _model_construction.complete_model_class(
  File "pydantic/_internal/_model_construction.py", line 642, in complete_model_class
    cls.__pydantic_fields__, cls.__pydantic_extra_info__ = rebuild_model_fields(
  File "pydantic/_internal/_fields.py", line 464, in rebuild_model_fields
    new_field = _recreate_field_info(
  File "pydantic/_internal/_fields.py", line 502, in _recreate_field_info
    ann = _typing_extra.eval_type(
  File "pydantic/_internal/_typing_extra.py", line 442, in eval_type
    return eval_type_backport(value, globalns, localns)
  File "pydantic/_internal/_typing_extra.py", line 474, in eval_type_backport
    return _eval_type_backport(value, globalns, localns, type_params)
  File "pydantic/_internal/_typing_extra.py", line 511, in _eval_type_backport
    return _eval_type(value, globalns, localns, type_params)
  File "pydantic/_internal/_typing_extra.py", line 543, in _eval_type
    evaluated = typing._eval_type(  # type: ignore
  File "typing.py", line 461, in _eval_type
    return evaluate_forward_ref(t, globals=globalns, locals=localns,
  File "typing.py", line 1000, in evaluate_forward_ref
    value = forward_ref.evaluate(globals=globals, locals=locals,
  File "annotationlib.py", line 207, in evaluate
    return eval(code, globals=globals, locals=locals)
  File "<string>", line 1, in <module>
    from __future__ import annotations
  File "pydantic/_internal/_namespace_utils.py", line 97, in __getitem__
    return self.data[key]
  File "functools.py", line 1126, in __get__
    val = self.func(instance)
  File "pydantic/_internal/_namespace_utils.py", line 91, in data
    return {k: v for ns in self._namespaces for k, v in ns.items()}
RuntimeError: dictionary changed size during iteration

Reproducer below.

Example Code

"""
NOTE: Repro script generated with Claude Fable

Repro: concurrent lazy `model_rebuild()` raises
`RuntimeError: dictionary changed size during iteration`.

Setup: a module using `from __future__ import annotations` defines models whose
field types are only defined *later* in the same module. Building such a model's
schema fails during class creation, so pydantic leaves it incomplete (mock
validator) and defers the build to the first instantiation via
`model_rebuild(raise_errors=False)`.

If the first instantiations happen concurrently (e.g. FastAPI's threadpool
serving the first requests after process start), multiple threads enter
`model_rebuild()` for the same class simultaneously. `model_rebuild()` mutates
the class `__dict__` (delattr/setattr of `__pydantic_core_schema__`,
`__pydantic_validator__`, `__pydantic_serializer__`), while
`NsResolver.types_namespace` includes the live `vars(cls)` of that class in the
`LazyLocalNamespace` another thread is iterating during annotation evaluation
(`LazyLocalNamespace.data`). Result:

    RuntimeError: dictionary changed size during iteration

The same race also intermittently produces
    AttributeError: type object 'Box0' has no attribute '__pydantic_core_schema__'
from the delattr block at the top of `model_rebuild()`.

Each round executes the module source into a fresh module (fresh, incomplete
classes -- like a freshly started process) and races threads instantiating the
models. Typically fires within a few dozen rounds (< 1 min).

Run: python pydantic_rebuild_race_repro.py
"""

import sys
import threading
import time
import traceback
import types

import pydantic

print(f"python {sys.version}")
print(f"pydantic {pydantic.VERSION}")

sys.setswitchinterval(1e-5)  # more frequent thread preemption; widens the race window

N_GENERICS = 2  # generic origins shared by all models (parametrizing triggers origin.model_rebuild)
N_LEAVES = 16  # leaf models, defined at the end of the module so everything before stays incomplete
N_MODELS = 4  # top-level models, the ones instantiated by the racing threads
N_METHODS = 60  # dummy methods to enlarge each class __dict__ (the dict iterated during annotation eval)
N_THREADS = 32
ROUNDS = 200
RACE_WINDOW_S = 0.5


def make_module_source() -> str:
    boxes = " | ".join(f"Box{g}[Leaf{l}]" for g in range(N_GENERICS) for l in range(N_LEAVES))
    methods = [f"    def method{m}(self) -> None: ..." for m in range(N_METHODS)]
    lines = ["from __future__ import annotations", "from pydantic import BaseModel", ""]
    # Top-level models come first: all their field types are still undefined here,
    # so their schema build is deferred until first instantiation.
    for i in range(N_MODELS):
        lines += [f"class Model{i}(BaseModel):", f"    items: list[{boxes}] = []", *methods, ""]
    for g in range(N_GENERICS):
        lines += [f"class Box{g}[T](BaseModel):", "    value: T | None = None", "    ref: Leaf0 | None = None", *methods, ""]
    for l in range(N_LEAVES):
        lines += [f"class Leaf{l}(BaseModel):", "    x: int = 0", ""]
    return "\n".join(lines)


MODULE_SOURCE = make_module_source()

errors: list[str] = []
errors_lock = threading.Lock()


def worker(barrier: threading.Barrier, model: type) -> None:
    barrier.wait()
    end = time.monotonic() + RACE_WINDOW_S
    while time.monotonic() < end:
        try:
            model()  # first call hits the mock validator -> lazy model_rebuild()
        except Exception:
            with errors_lock:
                errors.append(traceback.format_exc())


for rnd in range(ROUNDS):
    # Fresh module => fresh incomplete classes, like a freshly started process.
    mod = types.ModuleType(f"repro_generated_{rnd}")
    sys.modules[mod.__name__] = mod
    exec(compile(MODULE_SOURCE, mod.__name__, "exec"), mod.__dict__)
    models = [getattr(mod, f"Model{i}") for i in range(N_MODELS)]
    assert not models[0].__pydantic_complete__

    barrier = threading.Barrier(N_THREADS)
    threads = [threading.Thread(target=worker, args=(barrier, models[i % N_MODELS])) for i in range(N_THREADS)]
    for t in threads:
        t.start()
    for t in threads:
        t.join()

    hits = [tb for tb in errors if "changed size during iteration" in tb]
    if hits:
        print(hits[0], file=sys.stderr)
        print(f"REPRODUCED in round {rnd}: RuntimeError: dictionary changed size during iteration")
        sys.exit(1)
    if errors:
        first_line = errors[0].strip().splitlines()[-1]
        print(f"round {rnd}: {len(errors)} sibling race errors (e.g. {first_line})")
        errors.clear()

print("not reproduced")

Python, Pydantic & OS Version

In Prod: Ubuntu Docker, in Dev:

             pydantic version: 2.13.4
        pydantic-core version: 2.46.4
          pydantic-core build: profile=release pgo=false
               python version: 3.14.6 | packaged by conda-forge | (main, Jun 12 2026, 08:47:56) [Clang 20.1.8 ]
                     platform: macOS-26.5.1-arm64-arm-64bit-Mach-O
             related packages: pyright-1.1.410 fastapi-0.136.3 pydantic-extra-types-2.11.1 email-validator-2.3.0 pydantic-settings-2.14.1 typing_extensions-4.15.0
                       commit: unknown

The two patches

Our patch+228 −4
pydantic/_internal/_decorators.pyindex 3b8f847e..e00d7cdd 100644@@ -502,7 +502,9 @@ def _decorator_infos_for_class(     res = DecoratorInfos()     to_replace: list[tuple[str, Any]] = [] -    for var_name, var_value in vars(typ).items():+    # Iterate over a snapshot of the class namespace: `.copy()` is atomic, whereas iterating+    # the live mapping can fail if another thread mutates it concurrently (e.g. `model_rebuild()`):+    for var_name, var_value in vars(typ).copy().items():         if isinstance(var_value, PydanticDescriptorProxy):             info = var_value.decorator_info             if isinstance(info, ValidatorDecoratorInfo):pydantic/_internal/_namespace_utils.pyindex d308f7e4..ea5b430e 100644@@ -4,6 +4,7 @@ import sys from collections.abc import Callable, Generator, Iterator, Mapping from contextlib import contextmanager from functools import cached_property+from types import MappingProxyType from typing import Any, NamedTuple, TypeAlias, TypeVar  from typing_extensions import ParamSpec, TypeAliasType, TypeVarTuple@@ -88,7 +89,14 @@ class LazyLocalNamespace(Mapping[str, Any]):      @cached_property     def data(self) -> dict[str, Any]:-        return {k: v for ns in self._namespaces for k, v in ns.items()}+        data: dict[str, Any] = {}+        for ns in self._namespaces:+            # Namespaces can be live mappings (e.g. `vars(cls)`) that other threads may mutate+            # concurrently (e.g. `model_rebuild()` sets/deletes class attributes). Iterating over+            # them would raise a `RuntimeError`, so we take a snapshot using the atomic `copy()`+            # method of mapping proxies (merging a `dict` with `update()` is already atomic):+            data.update(ns.copy() if isinstance(ns, MappingProxyType) else ns)+        return data      def __len__(self) -> int:         return len(self.data)pydantic/dataclasses.pyindex 62d380f5..8650d220 100644@@ -333,7 +333,11 @@ def rebuild_dataclass(         if attr in cls.__dict__ and not isinstance(getattr(cls, attr), _mock_val_ser.MockValSer):             # Deleting the validator/serializer is necessary as otherwise they can get reused in             # pydantic-core. Same applies for the core schema that can be reused in schema generation.-            delattr(cls, attr)+            try:+                delattr(cls, attr)+            except AttributeError:+                # Another thread rebuilding the dataclass concurrently already deleted the attribute:+                pass      cls.__pydantic_complete__ = False pydantic/main.pyindex 758a839c..541cecee 100644@@ -693,7 +693,11 @@ class BaseModel(metaclass=_model_construction.ModelMetaclass):                 # pydantic-core. We do so only if they aren't mock instances, otherwise — as `model_rebuild()`                 # isn't thread-safe — concurrent model instantiations can lead to the parent validator being used.                 # Same applies for the core schema that can be reused in schema generation.-                delattr(cls, attr)+                try:+                    delattr(cls, attr)+                except AttributeError:+                    # Another thread rebuilding the model concurrently already deleted the attribute:+                    pass          if _types_namespace is not None:             rebuild_ns = _types_namespacetests/test_dataclasses.pyindex a32bc7ae..ccbf6308 100644@@ -2618,6 +2618,39 @@ def test_rebuild_dataclass():     assert MyDataClass2.__pydantic_complete__  +@pytest.mark.thread_unsafe(reason='`sys.settrace()` is thread unsafe')+def test_rebuild_dataclass_attribute_concurrently_deleted() -> None:+    """https://github.com/pydantic/pydantic/issues/13419: another thread rebuilding the same dataclass+    may delete the core schema/validator/serializer between the check and the `delattr()` call.+    """++    @pydantic.dataclasses.dataclass+    class MyDataClass:+        x: int++    lines, start = inspect.getsourcelines(rebuild_dataclass)+    delattr_lineno = next(start + i for i, line in enumerate(lines) if 'delattr(cls, attr)' in line)+    rebuild_dataclass_code = rebuild_dataclass.__code__++    def local_tracer(frame, event, arg):+        if event == 'line' and frame.f_lineno == delattr_lineno:+            # Simulate a concurrent `rebuild_dataclass()` call deleting the attribute first:+            delattr(MyDataClass, frame.f_locals['attr'])+        return local_tracer++    def tracer(frame, event, arg):+        return local_tracer if frame.f_code is rebuild_dataclass_code else None++    old_trace = sys.gettrace()+    sys.settrace(tracer)+    try:+        assert rebuild_dataclass(MyDataClass, force=True) is True+    finally:+        sys.settrace(old_trace)++    assert MyDataClass(x=1).x == 1++ @pytest.mark.parametrize(     'dataclass_decorator',     [tests/test_internal.pyindex acd0b46e..6e41188b 100644@@ -288,3 +288,79 @@ def test_resolve_default_value_missing_validated_data():             validated_data=None,             call_default_factory=True,         )+++def _mutate_on_line_events(code: Any, mutate: Any) -> Any:+    """Return a trace function calling `mutate()` on every line event of the frame running `code`.++    This deterministically simulates another thread mutating a namespace while it is being iterated.+    """++    def tracer(frame, event, arg):+        if frame.f_code is not code:+            return None++        def local_tracer(frame, event, arg):+            if event == 'line':+                mutate()+            return local_tracer++        return local_tracer++    return tracer+++@pytest.mark.thread_unsafe(reason='`sys.settrace()` is thread unsafe')+def test_lazy_local_namespace_concurrent_class_dict_mutation() -> None:+    """https://github.com/pydantic/pydantic/issues/13419: `model_rebuild()` running in another thread+    mutates the class `__dict__` included (through `vars(cls)`) in the namespace used for annotations evaluation.+    """+    from pydantic._internal._namespace_utils import LazyLocalNamespace, NsResolver++    class Model:+        a: int++    counter = iter(range(10_000))++    def mutate() -> None:+        setattr(Model, f'attr_{next(counter)}', 1)++    ns_resolver = NsResolver()+    with ns_resolver.push(Model):+        locals_ns = ns_resolver.types_namespace.locals+        assert isinstance(locals_ns, LazyLocalNamespace)++        old_trace = sys.gettrace()+        sys.settrace(_mutate_on_line_events(LazyLocalNamespace.data.func.__code__, mutate))+        try:+            assert locals_ns['Model'] is Model+        finally:+            sys.settrace(old_trace)+++@pytest.mark.thread_unsafe(reason='`sys.settrace()` is thread unsafe')+def test_decorator_infos_concurrent_class_dict_mutation() -> None:+    from pydantic import field_validator+    from pydantic._internal._decorators import _decorator_infos_for_class++    class Model:+        @field_validator('a')+        @classmethod+        def val_a(cls, v: Any) -> Any:+            return v++        def method(self) -> None: ...++    counter = iter(range(10_000))++    def mutate() -> None:+        setattr(Model, f'attr_{next(counter)}', 1)++    old_trace = sys.gettrace()+    sys.settrace(_mutate_on_line_events(_decorator_infos_for_class.__code__, mutate))+    try:+        res, _ = _decorator_infos_for_class(Model, collect_to_replace=False)+    finally:+        sys.settrace(old_trace)++    assert list(res.field_validators) == ['val_a']tests/test_main.pyindex c87dae79..a669e607 100644@@ -2557,6 +2557,103 @@ def test_model_rebuild_zero_depth():     assert m.model_dump() == {'x': 42}  +@pytest.mark.thread_unsafe(reason='`sys.settrace()` is thread unsafe')+def test_model_rebuild_attribute_concurrently_deleted() -> None:+    """https://github.com/pydantic/pydantic/issues/13419: another thread rebuilding the same model+    may delete the core schema/validator/serializer between the check and the `delattr()` call.+    """+    import inspect++    class Model(BaseModel):+        x: int++    lines, start = inspect.getsourcelines(BaseModel.model_rebuild)+    delattr_lineno = next(start + i for i, line in enumerate(lines) if 'delattr(cls, attr)' in line)+    model_rebuild_code = BaseModel.model_rebuild.__func__.__code__++    def local_tracer(frame, event, arg):+        if event == 'line' and frame.f_lineno == delattr_lineno:+            # Simulate a concurrent `model_rebuild()` call deleting the attribute first:+            delattr(Model, frame.f_locals['attr'])+        return local_tracer++    def tracer(frame, event, arg):+        return local_tracer if frame.f_code is model_rebuild_code else None++    old_trace = sys.gettrace()+    sys.settrace(tracer)+    try:+        assert Model.model_rebuild(force=True) is True+    finally:+        sys.settrace(old_trace)++    assert Model(x=1).x == 1+++@pytest.mark.thread_unsafe(reason='Spawns threads')+def test_model_rebuild_concurrent_instantiation(create_module) -> None:+    """https://github.com/pydantic/pydantic/issues/13419: concurrent instantiations of an incomplete+    model trigger concurrent `model_rebuild()` calls, which shouldn't fail.+    """+    import threading++    module = create_module(+        # language=Python+        """+from __future__ import annotations++from typing import Generic, TypeVar++from pydantic import BaseModel++T = TypeVar('T')+++class Model(BaseModel):+    items: list[Box[Leaf]] = []++    def method1(self) -> None: ...+    def method2(self) -> None: ...+    def method3(self) -> None: ...+++class Box(BaseModel, Generic[T]):+    value: T | None = None+    ref: Leaf | None = None+++class Leaf(BaseModel):+    x: int = 0+"""+    )++    n_threads = 8+    barrier = threading.Barrier(n_threads)+    errors: list[BaseException] = []++    def worker() -> None:+        barrier.wait()+        try:+            for _ in range(20):+                assert module.Model(items=[{'value': {'x': 1}}]).items[0].value == module.Leaf(x=1)+        except BaseException as e:+            errors.append(e)++    old_interval = sys.getswitchinterval()+    sys.setswitchinterval(1e-6)+    try:+        threads = [threading.Thread(target=worker) for _ in range(n_threads)]+        for t in threads:+            t.start()+        for t in threads:+            t.join()+    finally:+        sys.setswitchinterval(old_interval)++    assert errors == []+    assert module.Model.__pydantic_complete__++ @pytest.fixture(scope='session', name='InnerEqualityModel') def inner_equality_fixture():     class InnerEqualityModel(BaseModel): 
Merged fix · PR #13438+138 −30
pydantic/dataclasses.pyindex 62d380f54..474289ec3 100644@@ -330,9 +330,13 @@ def rebuild_dataclass(         return None      for attr in ('__pydantic_core_schema__', '__pydantic_validator__', '__pydantic_serializer__'):-        if attr in cls.__dict__ and not isinstance(getattr(cls, attr), _mock_val_ser.MockValSer):+        if attr in cls.__dict__ and not isinstance(+            getattr(cls, attr), (_mock_val_ser.MockCoreSchema, _mock_val_ser.MockValSer)+        ):             # Deleting the validator/serializer is necessary as otherwise they can get reused in             # pydantic-core. Same applies for the core schema that can be reused in schema generation.+            # We do so only if they aren't mock instances, otherwise concurrent reads of these attributes+            # (e.g. when instantiating the dataclass in another thread) can resolve them from the parent class.             delattr(cls, attr)      cls.__pydantic_complete__ = Falsepydantic/main.pyindex 758a839c6..6bb1554e5 100644@@ -8,6 +8,7 @@ from __future__ import annotations as _annotations  import operator import sys+import threading import types import warnings from collections.abc import Generator, Mapping@@ -78,6 +79,18 @@ IncEx: TypeAlias = set[int] | set[str] | Mapping[int, Union['IncEx', bool]] | Ma  _object_setattr = _model_construction.object_setattr +_rebuild_lock = threading.RLock()+"""+A lock used to make model rebuilds thread-safe when first instantiating an incomplete model.++Rebuilding a model isn't thread-safe (the class attributes are mutated during the rebuild,+while other threads may be reading them to perform validation/serialization), so `model_rebuild()`+calls are serialized using this lock. The lock is reentrant, as rebuilding a model can trigger the+rebuild of another one (e.g. when a generic origin is rebuilt during parametrization in+`__class_getitem__()`). For the same reason, the lock is global and not per-class: two threads+holding their own class's lock could otherwise request the other's and deadlock.+"""+  def _check_frozen(model_cls: type[BaseModel], name: str, value: Any) -> None:     if model_cls.model_config.get('frozen'):@@ -681,42 +694,54 @@ class BaseModel(metaclass=_model_construction.ModelMetaclass):             Returns `None` if the schema is already "complete" and rebuilding was not required.             If rebuilding _was_ required, returns `True` if rebuilding was successful, otherwise `False`.         """-        already_complete = cls.__pydantic_complete__-        if already_complete and not force:+        # As the rebuild lock is global (not per model class), avoid needlessly holding it if the model is+        # already complete:+        if cls.__pydantic_complete__ and not force:             return None -        cls.__pydantic_complete__ = False+        with _rebuild_lock:+            # Re-check inside the lock, as another thread may have rebuilt the model while we were waiting:+            already_complete = cls.__pydantic_complete__+            if already_complete and not force:+                return None -        for attr in ('__pydantic_core_schema__', '__pydantic_validator__', '__pydantic_serializer__'):-            if attr in cls.__dict__ and not isinstance(getattr(cls, attr), _mock_val_ser.MockValSer):-                # Deleting the validator/serializer is necessary as otherwise they can get reused in-                # pydantic-core. We do so only if they aren't mock instances, otherwise — as `model_rebuild()`-                # isn't thread-safe — concurrent model instantiations can lead to the parent validator being used.-                # Same applies for the core schema that can be reused in schema generation.-                delattr(cls, attr)+            cls.__pydantic_complete__ = False -        if _types_namespace is not None:-            rebuild_ns = _types_namespace-        elif _parent_namespace_depth > 0:-            rebuild_ns = _typing_extra.parent_frame_namespace(parent_depth=_parent_namespace_depth, force=True) or {}-        else:-            rebuild_ns = {}+            for attr in ('__pydantic_core_schema__', '__pydantic_validator__', '__pydantic_serializer__'):+                if attr in cls.__dict__ and not isinstance(+                    getattr(cls, attr), (_mock_val_ser.MockCoreSchema, _mock_val_ser.MockValSer)+                ):+                    # Deleting the validator/serializer is necessary as otherwise they can get reused in+                    # pydantic-core. Same applies for the core schema that can be reused in schema generation.+                    # We do so only if they aren't mock instances, otherwise concurrent reads of these attributes+                    # — performed without holding the rebuild lock (e.g. when instantiating the model) — can+                    # resolve them from the parent class.+                    delattr(cls, attr)++            if _types_namespace is not None:+                rebuild_ns = _types_namespace+            elif _parent_namespace_depth > 0:+                rebuild_ns = (+                    _typing_extra.parent_frame_namespace(parent_depth=_parent_namespace_depth, force=True) or {}+                )+            else:+                rebuild_ns = {} -        parent_ns = _model_construction.unpack_lenient_weakvaluedict(cls.__pydantic_parent_namespace__) or {}+            parent_ns = _model_construction.unpack_lenient_weakvaluedict(cls.__pydantic_parent_namespace__) or {} -        ns_resolver = _namespace_utils.NsResolver(-            parent_namespace={**rebuild_ns, **parent_ns},-        )+            ns_resolver = _namespace_utils.NsResolver(+                parent_namespace={**rebuild_ns, **parent_ns},+            ) -        return _model_construction.complete_model_class(-            cls,-            _config.ConfigWrapper(cls.model_config, check=False),-            ns_resolver,-            raise_errors=raise_errors,-            # If the model was already complete, we don't need to call the hook again.-            call_on_complete_hook=not already_complete,-            is_force_rebuild=force,-        )+            return _model_construction.complete_model_class(+                cls,+                _config.ConfigWrapper(cls.model_config, check=False),+                ns_resolver,+                raise_errors=raise_errors,+                # If the model was already complete, we don't need to call the hook again.+                call_on_complete_hook=not already_complete,+                is_force_rebuild=force,+            )      @classmethod     def model_validate(tests/test_main.pyindex c87dae793..f924cec99 100644@@ -2,6 +2,7 @@ import json import platform import re import sys+import threading import typing import warnings from collections import defaultdict@@ -2541,6 +2542,84 @@ def test_model_rebuild_localns():         C.model_rebuild(_types_namespace={'A': A})  +@pytest.mark.timeout(5)+def test_model_rebuild_nested_during_parametrization():+    T = TypeVar('T')++    class Model(BaseModel):+        b: Optional['Box[Leaf]'] = None++    class Box(BaseModel, Generic[T]):+        leaf: Optional['Leaf'] = None++    class Leaf(BaseModel):+        x: int = 0++    assert not Model.__pydantic_complete__+    assert not Box.__pydantic_complete__++    # Rebuilding `Model` evaluates `Box[Leaf]`, and `__class_getitem__` then calls+    # `Box.model_rebuild()` on the same thread. Any locking added to the rebuild logic+    # must support this reentrancy:+    m = Model(b={'leaf': {'x': 1}})+    assert m.b.leaf.x == 1+    assert Model.__pydantic_complete__+++@pytest.mark.skipif(sys.platform == 'emscripten', reason='no threading on emscripten')+def test_model_rebuild_already_rebuilt_by_other_thread():+    schema_gen_entered = threading.Event()+    resume_schema_gen = threading.Event()+    hook_calls: list[type] = []++    class Model(BaseModel):+        x: 'Gate'++        @classmethod+        def __pydantic_on_complete__(cls) -> None:+            hook_calls.append(cls)++    class Gate:+        @classmethod+        def __get_pydantic_core_schema__(cls, source_type, handler):+            schema_gen_entered.set()+            resume_schema_gen.wait(timeout=10)+            return core_schema.int_schema()++    assert not Model.__pydantic_complete__++    results: dict[str, bool | None] = {}+    t2_started = threading.Event()++    def rebuild(key: str) -> None:+        if key == 't2':+            t2_started.set()+        results[key] = Model.model_rebuild(_types_namespace={'Gate': Gate})++    # The first thread acquires the rebuild lock and waits in schema generation:+    t1 = threading.Thread(target=rebuild, args=('t1',), daemon=True)+    t1.start()+    assert schema_gen_entered.wait(timeout=10)++    # While the first thread is waiting on `resume_schema_gen`, `Model` can't become complete,+    # so the second thread is guaranteed to pass the unlocked completeness check and block on the lock:+    t2 = threading.Thread(target=rebuild, args=('t2',), daemon=True)+    t2.start()+    assert t2_started.wait(timeout=10)+    assert t2.is_alive()+    assert 't2' not in results++    resume_schema_gen.set()+    t1.join(timeout=10)+    t2.join(timeout=10)+    assert not t1.is_alive()+    assert not t2.is_alive()++    assert results == {'t1': True, 't2': None}+    assert hook_calls == [Model]+    assert Model(x=1).x == 1++ def test_model_rebuild_zero_depth():     class Model(BaseModel):         x: 'X_Type'