Repository navigation
test: make the free-threaded 3.14 tests check what they say - #844
Merged
Merged
Conversation
- Render the worker-thread routes' slot in an empty context. Executor workers copy the submitting thread's context variables on free-threaded 3.14 (sys.flags.thread_inherit_context), so the slot rendered on the ordinary path instead of the node path the routes exist to test. - Let the check thread wake while it is idle in the request-data test. With the GIL off, the error the check raised and the frame holding the data are freed only once the check thread runs Python code again. Refs #739 Co-Authored-By: Claude Opus 5.5 <[email protected]>
On free-threaded Python 3.14 a new thread copies the context variables of the thread that starts it, so these threads joined the caller's render hold or validation scope instead of opening their own. Also say in the trust boundaries doc when a worker thread copies context variables, and word the earlier comments more plainly. Refs #739 Co-Authored-By: Claude Opus 5.5 <[email protected]>
4 tasks done
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.
Refs #739. The weekly run 45211772140 failed three tests on free-threaded CPython 3.14 (
test (3.14t)). Each failure is a wrong assumption in its test, so this pull request changes only tests and one design doc. On every build, each way of rendering a slot writes the same HTML. What differs on free-threaded 3.14 is which way a plain worker thread takes.1 and 2: worker-thread routes took the ordinary path
test_node_path_attribute_values.py::...test_markup_in_nested_template_stays_in_its_attribute[worker-thread]test_attribute_safe_string_subclass.py::TestElementKey::test_key_escapes_a_subclass_that_keeps_html[worker-thread]The worker-thread route calls
str(slot)in aThreadPoolExecutorto reach the node path, the node classes that render a template outside the page render's context. On free-threaded 3.14,sys.flags.thread_inherit_contextis 1 by default, so a new thread copies the context variables of the thread that starts it. The worker therefore rendered the slot inside the page render's context, on the ordinary path:assert node_path_calls). Its other assertions passed: the attribute matched the ordinary path, with the markup escaped inside it.data-citry-key. On a page without Vue, the ordinary path writes no key by design (an element's#c-keyis the Vue key there), and the worker-thread output matched thecaptureroute exactly:<div id="t" data-cid-…="">x</div>.Both fail on a 3.14t build only with
-X thread_inherit_context=1, and pass with=0. The route now renders the slot withcontextvars.Context().run, so it reaches the node path on every build.The same assumption sat in three more tests, which passed on 3.14t while testing something else:
test_c_element_foreign_body.py: its worker-thread routes tested the ordinary path. It gets the same change.test_vue_asset_memory.py::test_a_render_in_progress_keeps_its_files_while_another_render_publishes: the "other render" joined the first render's asset hold instead of opening its own.test_events_pydantic_v1_data.py::..._while_another_thread_holds_the_hooks: the other thread checked inside the test's validation scope instead of opening its own.The last two now start their thread with
contextvars.Context().run.docs/design/trust_boundaries.mdnow says that a plain executor or thread copies context variables whensys.flags.thread_inherit_contextis set, and then a slot rendered there uses capture.3: the request data was freed only after the check thread woke
test_events_pydantic_bounded.py::test_a_check_that_raises_lets_go_of_the_request_data_without_the_cycle_collectorThis failed only in the job's second pytest step, which sets
PYTHON_GIL: 0. In the first step, importingcitry_core._rustturns the GIL back on.With the GIL off, when one thread drops the last reference to an object another thread made, CPython frees it only once the thread that made it runs Python code again. The error the check raises, its traceback, and the check's frame (which holds the data) are made on the check thread. That thread then waits idle for up to 60 seconds, so the data stayed alive after the caller let go of it. Nothing else held it: with the cycle collector off and the same thread still holding its last job, the data is freed as soon as that thread's idle wait times out and the thread runs again.
The test now sets the idle wait to 10 ms and waits up to 5 seconds for the data to be freed. It still fails when either guard it protects is removed (
del value, outcomeinrun_on_check_thread, orself.call = self.context = Nonein_Job.run), with the GIL on and off.Left open: in production, on a free-threaded build with the GIL off, any object a check thread built and the caller dropped while that thread was idle stays alive until the thread runs again. That covers a failed check's error and the data it reaches, and a successful check's result. Each idle check thread keeps at most one such set, for at most 60 seconds. This pull request does not change that.
Checks
test_attribute_safe_string_subclass.py, 830 passed withPYTHON_GIL=0(withthread_inherit_context0 and 1) and with the GIL on.py--alt-interpreters.ymlon this branch, run 45251491575:test (3.14t)passed both pytest steps, including the one withPYTHON_GIL: 0. The PyPy job is unrelated to this change.Versioning and changelog
Only tests and a design doc change, so nothing the 1.x promise covers is touched, and there is no changelog entry.
🤖 Generated with Claude Code