You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Render hangs when a Const callable renders a component while another thread resets a template #315
A render can hang forever when a function marked Const(...) renders another component for the first time while another thread resets a template on the same Citry instance (for example during a hot reload).
How to reproduce
importthreading, timefromcitryimportCitry, Component, Constapp=Citry(autodiscover=False)
in_build=threading.Event()
classLeaf(Component):
citry=apptemplate=""" <b>{{ value }}</b> """defhelper(v):
in_build.set()
time.sleep(0.5) # gives the reset time to take its locksreturnLeaf(value=v).render().serialize()
classPage(Component):
citry=apptemplate=""" <div>{{ helper(value) }}</div> """classOther(Component):
citry=apptemplate=""" <p>other</p> """defrender():
Page(
value=Const("fixed"),
helper=Const(helper),
).render().serialize()
defreset():
in_build.wait()
Other.reset_template()
threads= [threading.Thread(target=f) forfin (render, reset)]
fortinthreads:
t.start()
fortinthreads:
t.join() # never returns on main (01ba48efd)
Other is unrelated to Page and Leaf; resetting any template on the engine is enough.
Why it hangs
The two threads take the same two locks in opposite order:
Render thread.ConstBodyCache.get_or_build (packages/py/citry/citry/constness.py:1151) holds the cache's lock while build() computes the constant parts of Page's body. That calls helper(...), which renders Leaf for the first time. load_template (assets.py:289) then waits for Citry._template_source_lock.
Reset thread.reset_template (assets.py:461) holds _template_source_lock and calls _evict_component_cache, which reaches ConstBodyCache.evict_component (constness.py:1166) and waits for the cache's lock.
The root cause is that get_or_build runs user code (the Const callable, through build()) while it holds a lock.
Suggested fix
Same approach as #292 and #293: run build() outside the lock, then store the result with a check-then-set under the lock (keep the entry that was stored first). On concurrent first renders build() may then run more than once, but only one result is kept. Add a regression test with the two threads above and a timeout.
Found while auditing locks for #292 and #293. It is separate from them because it needs no extension hook.
Activity