Repository navigation
fix: first renders on several threads no longer race on a cold engine - #285
Merged
Merged
Conversation
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]>
This was referenced Oct 23, 2026
Closed
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.
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.
simple = "vue"class can render without a Component instance,_compute_simple_vue_admissionlooped over the class's namespace._static_class_dictreturns the live namespace, not a copy. While onethread 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.preparation". The regression test for the first race found this
one.
default_events_producerbuilds each engine's Vue producer onfirst 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 inone 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-threaded3.14t. A Python loop over
mp.items()raised on every build, and on3.14t
tuple(mp.items())anddict(mp)raised too.component_render.py: the admission check's member loop, and thesnapshot of plain (non-Component) bases, loop over that copy.
_vue/events.py:default_events_producerstores its producer withsetdefaultunder a new lock and returns the stored one, so everythread 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_idand
_citry_events_infoall hold plain data. None of their names startswith
on_, and none of their values is a function or a descriptor._citry_simple_vue_admissionis skipped by name. CPython itself alsostores
__annotations__(3.10 to 3.13), or__annotate_func__= None and__annotations_cache__(3.14), the first time something readscls.__annotations__. The first is skipped by name, and the others holdNone 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
citryfor other Python loops over a class namespace that arender 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
Stateafter the parenthad 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.pyandext/events/openapi.py: the names thatresolve a handler's annotations come from the component's MRO, which
includes parent components that may be rendering.
openapi.pyrunsfrom
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:432and_simple_declarations.py:218.Left for follow-up, because a server's render threads cannot reach them
on a GIL build:
namespace:
dict.update(mp)ordict(mp)incomponent.py:517,_simple_declarations.py:84,tag_rules.py:148/150,_input_types.py:413andext/events/tokens.py:777, and setoperations over a namespace in
_nested_declarations.py:482,_simple_declarations.py:183andext/preview/extension.py:148. Thestress run shows calls like these can raise there.
_provide_keys.py:702and_checker.py:2983loop over a livenamespace, but they run only in
citry checkand the language server.autodiscovery.py:101loops 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_classand_schema_infoinext/events/schemas.pycaneach keep a different converted class after two first event calls at
once.
configure_render_encoderanddispatcher_forinext/events/renderers.pycheck and store without a lock._load_asset_contentinassets.pycan runon_js_loaded/on_css_loadedtwice 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 ascount: int = 3, in a module withoutfrom __future__ import annotations) is rejected with "instance member__annotate_func__is notsupported", 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
mainwith the issue's error.test_simple_vue_first_renders_on_several_threads_succeed: the issue'sreproduction, 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 alsoexposed 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 acomponent whose
Stategets checked, then subclasses thatState. Itfails on
main.render_path), and thetouched test files also pass with
--python-render-path. The issue'sscript 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 faston 7b26396:"status": "PASSED"(every phase, including pytest and the Vue render parity check). mypy with
--platform linux:no issues.
🤖 Generated with Claude Code