Repository navigation
Description
What happens
Citry stores computed results on a component class the first time it
needs them: the loaded template, the resolved CSS and JS, and similar
values. While one thread stores such a value, another thread may be
reading the same class's namespace (its __dict__). #285 made the reads
that a render reaches loop over a copy, which fixed the failing first
renders in #271. Some reads that remain still run over the live
namespace in one C call: dict.update(namespace), dict(namespace),
and set operations over it.
With the GIL, one such C call cannot be interrupted by another thread's
store, so they are safe. On free-threaded Python (3.14t), they can be.
This script uses no Citry code: one thread adds names to a class while
the main thread reads its namespace in the ways Citry does:
import threading, time
class C:
pass
for i in range(50):
setattr(C, f"a{i}", i)
def writer():
i = 0
while True:
setattr(C, f"w{i}", i)
i += 1
threading.Thread(target=writer, daemon=True).start()
for name, op in {
"{}.update(vars(C))": lambda: {}.update(vars(C)),
"dict(vars(C))": lambda: dict(vars(C)),
"frozenset().isdisjoint(vars(C))":
lambda: frozenset({"x"}).isdisjoint(vars(C)),
"vars(C).copy()": lambda: vars(C).copy(),
}.items():
errors = calls = 0
end = time.time() + 0.5
while time.time() < end:
calls += 1
try:
op()
except RuntimeError:
errors += 1
print(f"{name}: {errors} errors in {calls} calls")On Python 3.14.6 free-threaded (each error was
RuntimeError: dictionary changed size during iteration):
{}.update(vars(C)): 6050 errors in 6064 calls
dict(vars(C)): 3601 errors in 3601 calls
frozenset().isdisjoint(vars(C)): 288083 errors in 288088 calls
vars(C).copy(): 0 errors in 22 calls
On Python 3.14.9 with the GIL, every line reports 0 errors. The writer
here never stops, so it changes the dict far more often than Citry does;
the script shows that these calls can raise, not how often a server hits
it. I did not reproduce a failing Citry render on 3.14t.
Cause
Reads of a live class or module namespace that remain after #285, with
line numbers on #285's branch (fix/simple-vue-cold-thread-race):
| Place | Read |
|---|---|
packages/py/citry/citry/component.py:517 |
effective.update(_static_class_dict(base)) |
_simple_declarations.py:84 |
effective.update(_static_class_dict(base)) |
tag_rules.py:148 and :150 |
localns.update(vars(candidate)) |
_input_types.py:413 |
dict(_static_class_dict(owner)) |
ext/events/tokens.py:777 |
dict(vars(klass)) |
_nested_declarations.py:482 |
_CUSTOM_CONSTRUCTION_NAMES.isdisjoint(namespace) |
_simple_declarations.py:183 |
namespace.keys() - _SLOT_DECLARATION_METADATA - {"default"} |
ext/preview/extension.py:148 |
_FIELDS & vars(base).keys() |
These also loop over a live namespace in Python code, which can raise on
any build. They run outside the render threads, so they need another
thread writing at the same time:
| Place | Where it runs |
|---|---|
_provide_keys.py:702 |
citry check and the language server |
_checker.py:2983 |
citry check and the language server |
autodiscovery.py:101 (for value in vars(module).values()) |
autodiscovery, looping over each imported module's globals |
#285's description lists all of them as left for later.
Who hits it
- The first table: apps on free-threaded Python 3.14t that render on
several threads.citry-corepublishes free-threaded 3.14 wheels. - The second table: only if a component class or module is written to
whilecitry check, the language server, or autodiscovery reads it,
for example a background thread that renders during autodiscovery.
Fix direction
Build on #285. Read each namespace through #285's
_static_class_namespace_copy (in _class_introspection.py), which copies
with mappingproxy.copy(). #285's stress runs found no error from that
call on 3.14t, and it raised none in the script above. For a module's
globals, copy with vars(module).copy(). Add a grep-style test or lint
that flags a new update(, dict(, .keys() or set operation applied
directly to _static_class_dict(...) or vars(...).
A free-threaded CI job that runs #285's stress test could catch a
regression in the render path.
Activity