Skip to content

fix: run server-stored State, Pydantic Events and the test suite on PyPy and free-threaded 3.14 - #709

Merged
JuroOravec merged 4 commits into
mainfrom
fix/pypy-weekly-585
Nov 15, 2026
Merged

JuroOravec merged 4 commits into
mainfrom
fix/pypy-weekly-585

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

The weekly run on PyPy and free-threaded CPython 3.14 (py--alt-interpreters.yml) has failed since it started. This PR fixes the causes that could be pinned down. The rest are tracked in #708 (v1.1). Refs #585. It does not close #585, because #708 is still open.

Product fixes (changelog entries)

  1. Server-stored State on PyPy. InMemoryCache sized State entries with sys.getsizeof, which always raises TypeError on PyPy. Every component with State._storage = "server" failed to render there (since 0.7.0). Entries are now counted by their UTF-8 size when getsizeof gives no size. CPython is unchanged.
  2. Pydantic classes with Events on PyPy. Events checked pydantic-core support by reading SchemaValidator's signature. PyPy cannot read the signature of a compiled class, so Events refused every Pydantic State and event data class with "needs Pydantic 2.13.0 or newer" although 2.14.1 was installed. Events now builds a tiny validator with the _use_prebuilt keyword instead. A probe run on PyPy went from 298 failing tests to 4 with this fix.

Test and CI fixes

  1. PyPy has no _tracemalloc, so 9 test modules failed to import. They import it from tests/_memory.py. Tests that measure memory skip on PyPy. The State budget replay test still runs there and skips only its memory bound.
  2. Under from __future__ import annotations, PyPy compiles tuple[()] to the text tuple[] when pytest rewrites asserts. The test names the type through an alias. A user hits the same thing only with a State dataclass that has a tuple[()] field and is defined inside their own pytest test module on PyPy: that class loses its type check.
  3. Free-threaded 3.14 copies the context variables of the thread that starts a new thread. A Pydantic v1 test started its thread inside a validation scope, so the thread hit the scope's limits. The test now starts the thread first. A run of the citry tests with -X thread_inherit_context=1 on CPython 3.14 found no other test with this problem. The sub-path code comment and the "Your own threads" note on the web frameworks page now describe this behavior.
  4. A concurrent messages test expected on_messages_loaded to run once. The hook's documented contract allows one run per thread when two threads load the messages at the same time, which happened once on 3.14t. The test now accepts that.
  5. The workflow had a comment line inside its folded pytest command, which turned -rfE -o faulthandler_timeout=600 into a shell comment. Both options only repeated pytest's default and pyproject.toml, so they are removed and the comment moves above the step.
  6. On PyPy, test_a_chain_of_nested_models_deeper_than_the_nesting_limit_is_refused uses all 16 GB of the runner and crashes its worker. It skips on PyPy until PyPy and free-threaded test failures left after #585 #708 is resolved.

Left for v1.1 (#708)

  • The 300-model chain memory use on PyPy (item 8). Probe run 43714378175 recorded memory rising from 1.3 GB to 15.5 GB before the crash. The earlier "runner lost communication" ending of run 43703841315 is not pinned down. That run still refused Pydantic classes, so it did not reach this test.
  • PyPy: test_a_class_made_at_run_time_can_be_freed (a Pydantic class stays alive after gc.collect()), and the JetBrains fixture test in citry_lsp.
  • 3.14t with the GIL off (report-only step): request data passed to a check that raised stays alive until the cycle collector runs. It is probably how the free-threaded build counts references, not a Citry leak.

Earlier weekly runs on main (42629897066, 41453907909) passed on 3.14t. The 3.14t failures here come from tests added since then (item 5) and a rare race (item 6).

Checks

  • citry tests on CPython 3.14: 13562 passed. With -X thread_inherit_context=1: all passed.
  • ruff check and format over the repository, mypy (the gate's file list) on darwin and with --platform linux, and scripts/validate.py: all pass. The Rust phases were not run because nothing under crates/ changed.
  • The PyPy fixes were checked in manual runs of the weekly workflow on a temporary branch, since deleted.

Review

Independent reviewers checked the changes, with separate prose passes and a docs cold read of the web frameworks paragraph and the changelog. Changes made from their findings:

  • The CI comment no longer claims the options were missing. The redundant options are removed.
  • The thread test sets its event in a finally, so a failure cannot leave pytest waiting for the thread at exit.
  • The web frameworks note now puts the fix first, covers 3.14t and later, and names the symptom (another visitor's sub-path, so scripts fail to load).
  • The changelog entries use _storage = "server" and lead with the outcome.
  • Wording fixes in the test comments, _memory.py, and the new docstring.

🤖 Generated with Claude Code

JuroOravec and others added 4 commits November 15, 2026 11:23
The weekly PyPy job failed for three reasons:

- `InMemoryCache` sized State entries with `sys.getsizeof`, which always
  raises `TypeError` on PyPy, so a component whose State is kept on the
  server failed to render there. Entries are now counted by their UTF-8
  size where `getsizeof` gives no size; CPython is unchanged.
- PyPy has no `_tracemalloc`, so every test module that imported
  `tracemalloc` failed to load. Those modules now import it from
  `tests/_memory.py`, and the tests that measure memory skip on PyPy.
- PyPy writes the annotation `tuple[()]` as "tuple[]" when it compiles a
  module from a syntax tree, as pytest's assert rewriting does, so the
  test State class names the type through a module alias.

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. The Pydantic v1 test that checks another
  thread's validation started its thread inside the validation scope, so
  the thread hit the scope's limits. It now starts the thread first.
- The concurrent messages test expected `on_messages_loaded` to run once,
  but the hook's documented contract allows one run per thread when
  threads load the messages at the same time.
- The State budget replay test runs on PyPy again and skips only its
  process-memory bound there.
- The workflow kept a comment inside its folded pytest command, which
  turned the options after it into a shell comment. The options it hid
  repeated pytest defaults and pyproject.toml, so they are removed and
  the comment moves above the step.
- The sub-path comment and the web frameworks page now say that a thread
  started on free-threaded Python sees the request that started it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Events decided whether the installed pydantic-core supports its bounded
validator copy by reading `SchemaValidator`'s signature. PyPy cannot read
the signature of a compiled class, so Events refused every Pydantic class
there with "needs Pydantic 2.13.0 or newer". It now builds a tiny
validator with the keyword it needs instead.

On PyPy the 300-model nesting test then uses all the runner's memory and
crashes its worker, so it skips there until #708 is investigated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec merged commit 74177ec into main Nov 15, 2026
0 of 12 checks passed
@JuroOravec
JuroOravec deleted the fix/pypy-weekly-585 branch November 15, 2026 14:36
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.

Weekly Python tests on PyPy or free-threaded CPython failed

1 participant