Skip to content

test: make the free-threaded 3.14 tests check what they say - #844

Merged
JuroOravec merged 2 commits into
mainfrom
fix/free-threaded-tests
Nov 23, 2026
Merged

JuroOravec merged 2 commits into
mainfrom
fix/free-threaded-tests

Conversation

@JuroOravec

@JuroOravec JuroOravec commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

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 a ThreadPoolExecutor to 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_context is 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:

  • Test 1 found no node-path call (assert node_path_calls). Its other assertions passed: the attribute matched the ordinary path, with the markup escaped inside it.
  • Test 2 found no data-citry-key. On a page without Vue, the ordinary path writes no key by design (an element's #c-key is the Vue key there), and the worker-thread output matched the capture route 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 with contextvars.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.md now says that a plain executor or thread copies context variables when sys.flags.thread_inherit_context is 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_collector

This failed only in the job's second pytest step, which sets PYTHON_GIL: 0. In the first step, importing citry_core._rust turns 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, outcome in run_on_check_thread, or self.call = self.context = None in _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

  • Local, CPython 3.14.9 free-threaded: the five changed test modules plus test_attribute_safe_string_subclass.py, 830 passed with PYTHON_GIL=0 (with thread_inherit_context 0 and 1) and with the GIL on.
  • CI: py--alt-interpreters.yml on this branch, run 45251491575: test (3.14t) passed both pytest steps, including the one with PYTHON_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

JuroOravec and others added 2 commits November 23, 2026 20:28
- 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]>
@JuroOravec
JuroOravec merged commit f1e8c30 into main Nov 23, 2026
33 of 34 checks passed
@JuroOravec
JuroOravec deleted the fix/free-threaded-tests branch November 23, 2026 20:48
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.

1 participant