Skip to content

feat(events): run bounded checks on large-stack threads and set the nesting limit to 64 - #653

Merged
JuroOravec merged 3 commits into
mainfrom
feat/bounded-checks-large-stack
Nov 14, 2026
Merged

JuroOravec merged 3 commits into
mainfrom
feat/bounded-checks-large-stack

Conversation

@JuroOravec

@JuroOravec JuroOravec commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

What this changes

Events checks event data, browser writes to State, and State read back from its token with Pydantic. Each nested level of data takes room on the stack of the thread that runs the check, so until now the nesting limit had to fit the smallest thread stack a server might use, and it was 32.

This PR runs those checks on a small set of threads that Citry starts itself, each with a 16 MiB stack. The nesting limit now does not depend on the server's thread stack size, and it rises to 64.

  • Where checks run. run_on_check_thread in citry/ext/events/_pydantic_bounded.py hands a check to one of up to 32 threads, keeps two idle, and ends the others after 60 seconds without work. A check past 32 waits in a queue. Payload checks, State restore, State writes from the browser, writing State out for its token, comparing State a handler changed, and building and type-checking State for a render all go through it. A check that is already on a check thread runs at once.
  • What carries over. The check sees a copy of the caller's context variables. Its exceptions reach the caller with their traceback. Inside a running validation, the CPU time limit counts the check thread's own CPU time. A forked child starts with no threads.
  • gevent and eventlet. The threads use the original _thread functions, whether the patching happened before or after Citry was imported, so a check always gets a real OS thread with its own stack.
  • Where no thread can start. In Pyodide, or where the stack size cannot be set, the check runs on the calling thread with the earlier limit of 32.
  • Why 64. The check threads hold far more than that: the heaviest shape measured needs about 0.9 MiB at the limit. The handler still gets the checked data on the server's own thread, though. On macOS with Python 3.14, a handler that calls model_dump() on a chain of models fits about 100 levels in 512 KiB and about 22 in 128 KiB. The docs and changelog advise giving request threads at least 512 KiB.
  • Cost. Handing work to a check thread takes about 10 to 40 microseconds (measured on macOS), and a call that writes State does it about a dozen times.

Upgrade note

App validators, State serializers and __post_init__ methods now run on Citry's threads. They can read the request's context variables, but threading.local values are unset there, and a Django query from a validator uses that thread's own connection. The changelog has an "Upgrading from 0.8.x" section for this, and docs_site/content/security.md has the details.

Tests

  • Subprocess tests that check data at the limit from a thread with a 128 KiB stack (event data, State writes, State written back for keep, change and render handlers). Each fails without this change.
  • A subprocess test that checks data at the limit on a check thread with half the stack, and one where a handler writes out data at the limit on a 512 KiB thread.
  • Unit tests for the pool: context copy, nested checks, tracebacks, the queue past 32 threads, CPU time, fork, threads that cannot start, a stack size that cannot be set or reset, failures in the thread loop, and freeing the data of a finished or failed check.

tests/test_events_pydantic_bounded.py passes (206 tests). ruff passes, and mypy passes for both darwin and --platform linux. CI will show the Linux and Windows results.

🤖 Generated with Claude Code

@JuroOravec
JuroOravec force-pushed the feat/bounded-checks-large-stack branch from d248b45 to 4dea6cc Compare November 14, 2026 14:12
JuroOravec and others added 3 commits November 14, 2026 15:23
…lures

A check thread no longer keeps the last job's call, context or result
after the caller has them, a failure in the thread's own loop is logged
and leaves an outcome that was already set, and a failed reset of the
thread stack size is logged. Tests cover each, plus a render with plain
nested data from a small thread and a handler writing out data at the
limit on a 512 KiB thread. Docs give the measured hand-over time and
say what a slow validator does to the pool.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec force-pushed the feat/bounded-checks-large-stack branch from 8b10ece to 3754ba0 Compare November 14, 2026 14:23
@JuroOravec
JuroOravec merged commit 1d8595a into main Nov 14, 2026
12 of 28 checks passed
@JuroOravec
JuroOravec deleted the feat/bounded-checks-large-stack branch November 14, 2026 14:38
JuroOravec added a commit that referenced this pull request Nov 14, 2026
…ight merged changes (#682)

Add one record under docs/security/reviews/ for each of the eight published
0.8.0 advisories (GHSA-4hj5, -8xvf, -7qpw, -g658, -x6rm, -h524, -xmx5,
-xg8c) and for each of the pull requests that Mechanism 7, step 7 owes one:
#610, #640, #651, #652, #653, #658, #674 and #675. Each record says only what
the public pull request, the published advisory, the merged code and its tests
show, and lists what was not checked.

The reviews README describes how a record for a published advisory differs
from a pull request record. The Mechanism 7 list in CLAUDE.md gains the two
places that GHSA-7qpw added: parser rules browsers differ on, and the page's
declared encoding.

Co-authored-by: Claude Opus 5.5 <[email protected]>
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