Skip to content

fix: first renders on several threads no longer race on a cold engine - #285

Merged
JuroOravec merged 2 commits into
mainfrom
fix/simple-vue-cold-thread-race
Oct 23, 2026
Merged

JuroOravec merged 2 commits into
mainfrom
fix/simple-vue-cold-thread-race

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

Fixes #271

What was wrong

When several threads render their first pages at the same time, as a
server's workers do right after it starts, a few of those renders failed.
Two separate races caused it.

  1. "dictionary changed size during iteration". To decide whether a
    simple = "vue" class can render without a Component instance,
    _compute_simple_vue_admission looped over the class's namespace.
    _static_class_dict returns the live namespace, not a copy. While one
    thread looped, other threads stored their computed results on the same
    class (the loaded template, resolved CSS and JS, the dependency cache,
    the admission record itself), and Python raised RuntimeError.
  2. "early selected-tree compilation does not match the current render
    preparation".
    The regression test for the first race found this
    one. default_events_producer builds each engine's Vue producer on
    first use and stores it without a lock. Two threads could each build
    one, and the second store replaced the first. A serialize compiles the
    page with the producer it got, later prepares the page with the stored
    one, and compares the two by identity, so a thread holding the
    replaced producer raised ValueError.

Both happen on both render paths: the admission check runs before Citry
picks the Rust or Python render path, and both paths share the producer.

The fix

  • _class_introspection.py: a new helper, _static_class_namespace_copy,
    returns a copy of a class namespace taken with mappingproxy.copy().
    That forwards to the class dict's own copy(), which copies the dict in
    one C call that another thread cannot change partway through. Stress
    runs with a writer thread against several reader threads found no error
    from .copy() on Python 3.10 and 3.14 with the GIL, or on free-threaded
    3.14t. A Python loop over mp.items() raised on every build, and on
    3.14t tuple(mp.items()) and dict(mp) raised too.
  • component_render.py: the admission check's member loop, and the
    snapshot of plain (non-Component) bases, loop over that copy.
  • _vue/events.py: default_events_producer stores its producer with
    setdefault under a new lock and returns the stored one, so every
    thread gets the same producer. The lock is not held while the producer
    is built.

Why a copy, and not a lock around the admission check

The issue suggested computing the admission record under a per-class
lock. A lock alone would not fix the error: most of the results stored on
the class are written outside the admission check, by template loading,
asset loading and dependency resolution on other threads. Every loop over
the namespace therefore has to loop over a copy. With the copy in place,
two threads that compute the record at once do the same work twice and
store equal records, one per class. The template plan inside the record
is already built once, under the template's compile_lock.

Whether a stored result can change the answer

I checked every attribute Citry stores on a component class after the
class is defined. _citry_template, _resolved_css, _resolved_js,
_resolved_messages, _citry_dependencies_cache,
_citry_component_script_capture, _citry_simple_template, _class_id
and _citry_events_info all hold plain data. None of their names starts
with on_, and none of their values is a function or a descriptor.
_citry_simple_vue_admission is skipped by name. CPython itself also
stores __annotations__ (3.10 to 3.13), or __annotate_func__ = None and
__annotations_cache__ (3.14), the first time something reads
cls.__annotations__. The first is skipped by name, and the others hold
None or a dict. So the check gives the same answer whether or not the
copy already holds them.

Other loops of the same kind

I searched citry for other Python loops over a class namespace that a
render thread can write to at the same time. Three can, and they now loop
over the same copy:

  • ext/events/state.py: the State meta check walks a State class's MRO,
    which can include a parent's State that a render is using. The render
    stores its input checks on that State as _citry_input_type_checks.
    The check also rejected that stored name as an unknown State meta
    attribute, so defining a subclass of a parent's State after the parent
    had rendered failed, even on one thread. The name is now one of the
    names Citry writes itself, which the check skips. This has its own
    changelog entry.
  • ext/events/handlers.py and ext/events/openapi.py: the names that
    resolve a handler's annotations come from the component's MRO, which
    includes parent components that may be rendering. openapi.py runs
    from build_openapi_document(), which a live server can call.

The other loops over class namespaces read classes that Citry never
writes to after definition (Lint, Events config, Slots, authored config
classes), so they cannot race. Those are _linting.py,
extension.py:1923, ext/events/handlers.py:432 and
_simple_declarations.py:218.

Left for follow-up, because a server's render threads cannot reach them
on a GIL build:

  • On free-threaded builds, a few single C calls also read a live
    namespace: dict.update(mp) or dict(mp) in component.py:517,
    _simple_declarations.py:84, tag_rules.py:148/150,
    _input_types.py:413 and ext/events/tokens.py:777, and set
    operations over a namespace in _nested_declarations.py:482,
    _simple_declarations.py:183 and ext/preview/extension.py:148. The
    stress run shows calls like these can raise there.
  • _provide_keys.py:702 and _checker.py:2983 loop over a live
    namespace, but they run only in citry check and the language server.
  • autodiscovery.py:101 loops over a module's live namespace.

Other first-use caches whose races cause repeated work or a second cached
object, not the errors above, also left for follow-up:
convert_schema_class and _schema_info in ext/events/schemas.py can
each keep a different converted class after two first event calls at
once. configure_render_encoder and dispatcher_for in
ext/events/renderers.py check and store without a lock.
_load_asset_content in assets.py can run on_js_loaded /
on_css_loaded twice for one class.

A separate bug found during this work, for its own issue: on Python 3.14,
a simple = "vue" class with its own class-level annotation (such as
count: int = 3, in a module without from __future__ import annotations) is rejected with "instance member __annotate_func__ is not
supported", because the admission check does not skip that name. The same
class is accepted on 3.13.

Tests

  • test_simple_vue_admission_survives_a_class_write_during_its_member_check:
    stores an attribute on the class from inside the member loop, which is
    where another thread's write lands in the race, and checks that the
    page renders. It fails on main with the issue's error.
  • test_simple_vue_first_renders_on_several_threads_succeed: the issue's
    reproduction, smaller. It runs three attempts of eight threads on a new
    engine with the thread switch interval lowered to 1 µs, and takes 0.2 s
    to 0.4 s per render path. It failed in every run on main, and it also
    exposed the producer race. Its barrier and joins have timeouts, so a
    deadlock fails the test instead of hanging the run.
  • test_default_producer_is_one_object_when_threads_build_it_at_once:
    holds two threads inside the producer's construction until both have
    started, then checks that both get the same producer. It fails on
    main.
  • test_state_subclass_defined_after_the_parent_rendered: renders a
    component whose State gets checked, then subclasses that State. It
    fails on main.
  • The admission tests run on both render paths (render_path), and the
    touched test files also pass with --python-render-path. The issue's
    script now runs 60 attempts of 16 threads without an error on both
    paths. Before the fix it failed on the first attempt.

scripts/check.py --profile fast on 7b26396: "status": "PASSED"
(every phase, including pytest and the Vue render parity check). mypy with --platform linux:
no issues.

🤖 Generated with Claude Code

When a server's worker threads rendered their first pages at the same
time, a few renders failed:

- The simple="vue" admission check looped over the class's live
  namespace while other threads stored computed results on that class,
  so Python raised "dictionary changed size during iteration". The loop,
  the plain-base snapshot, and three Events loops over component and
  State MROs now loop over a copy taken with mappingproxy.copy().
- default_events_producer could build two producers for one engine. A
  serialize that compiled with one and prepared with the other raised
  "early selected-tree compilation does not match the current render
  preparation". The first stored producer is now returned to every
  thread.

The State meta check also skips the input checks a render stores on a
State class, so subclassing a parent's State after the parent rendered
no longer fails with "'_citry_input_type_checks' is not a recognized
State meta attribute".

Fixes #271

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec merged commit 7cb509d into main Oct 23, 2026
26 checks passed
@JuroOravec
JuroOravec deleted the fix/simple-vue-cold-thread-race branch October 23, 2026 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

First renders on several threads fail with "dictionary changed size during iteration" when a page uses simple="vue" components

1 participant