Skip to content

fix(events): free a check's data promptly on free-threaded Python with the GIL off - #870

Merged
JuroOravec merged 4 commits into
mainfrom
events/clear-idle-check-thread
Nov 24, 2026
Merged

JuroOravec merged 4 commits into
mainfrom
events/clear-idle-check-thread

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

(Opus 5.5)

Closes #848.

What was wrong

With the GIL off, when one thread drops the last reference to an object another thread made, CPython frees the object only when the thread that made it next runs Python code (or when the cycle collector runs). An Events check thread makes a failed check's error and traceback, which reach the request data, and it also makes a passed check's result. The caller drops these after the check thread has gone idle, and an idle check thread waited up to 60 seconds before it ran Python code again. So the data stayed in memory for up to 60 seconds after the app let go of it.

Clearing the check thread's own references does not help, because it already holds none: _Job.run drops the call and context, and the caller drops the outcome. A pure-Python test on 3.14t showed the delay with all references cleared. With -X gil=1, and on a regular build, the objects were freed at once.

The change

_CheckThreads._serve in packages/py/citry/citry/ext/events/_pydantic_bounded.py:

  • With the GIL off, an idle check thread splits its 60 seconds of idle time into 60 waits of one second (_CHECK_THREAD_WAKE_SECONDS). Each wake runs the loop, which frees what callers dropped since the last wake.
  • The thread counts its idle time in waits, not by the clock, so code that replaces time.monotonic (such as freezegun) cannot stretch the idle time.
  • The thread checks sys._is_gil_enabled() before each wait, because importing an extension such as citry_core can turn the GIL back on.
  • A kept thread past its idle time waits 60 seconds at a time on any build, as before.
  • With the GIL on, the wait is one 60 second wait, the same as before.

No other Citry thread hands objects to callers and then waits. I checked the reload watcher, the preview server, the embedded provider's pipe threads, and the citry_ui test servers. The reviewer pointed out a related case: sync route handlers run through asyncio.to_thread, whose stdlib workers wait with no timeout. This pull request does not change that; it may deserve its own issue.

Tests

  • test_a_check_that_raises_lets_go_of_the_request_data_without_the_cycle_collector no longer shortens the idle wait. New: test_a_check_result_the_caller_drops_is_freed_without_the_cycle_collector. Both wait up to 5 seconds for the object to be freed. With the GIL off they fail without the fix, and on GIL builds they still guard the cleared references.
  • New loop tests, which pass on any build: test_an_idle_check_thread_wakes_often_without_the_gil, test_an_idle_check_thread_without_the_gil_still_ends_after_its_idle_time, and test_an_idle_check_thread_with_the_gil_waits_out_its_idle_time_at_once.
  • Local, CPython 3.14 (GIL): test_events_pydantic_bounded.py passed (233 tests, 3 runs). The Events tests passed (2975 passed, 68 skipped). ruff and mypy are clean, including mypy --platform linux.
  • Local, CPython 3.14.9 free-threaded with the GIL off: no published macOS cp314t citry-core wheel exists, so I loaded the check-thread module on its own, without the rest of Citry. Before the fix, a returned result was still alive after 5 seconds. After the fix, failed and passed checks were freed in about 1.0 seconds. The reviewer got the same result with 8 jobs at once and with queued jobs.

Review

An independent reviewer found one bug in the first version: it read time.monotonic, so freezegun could keep an idle thread alive forever, and on Windows the oversized timeout would likely raise. The thread now counts its idle time in waits. The re-review found no bugs, and its wording fixes are applied.

Versioning, changelog and security

  • The change touches only private constants and the check thread's loop, with no public API or data format change. It is a patch-level fix and fits in 1.3.0.
  • The changelog has a 1.3.0 "Fixed" entry.
  • Nothing about what reaches the browser changes.

🤖 Generated with Claude Code

JuroOravec and others added 4 commits November 24, 2026 20:48
…the last check's data

With the GIL off, an object one thread made and another thread drops is
freed only when the thread that made it next runs Python code. A check
thread makes a failed check's error and traceback (which reach the
request data) and a check's result, and the caller drops them after the
thread has gone idle for up to 60 seconds. For its first 60 seconds idle,
a check thread now wakes every second when the GIL is off, so those
objects are freed within about a second. With the GIL on, the wait is
unchanged.

Closes #848.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…lock

Code that replaces time.monotonic, such as freezegun, could stretch the
idle time without end or, on Windows, pass a timeout too large for a
lock. With the GIL on, the idle wait is again one 60 second wait.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec merged commit 92e29dc into main Nov 24, 2026
0 of 12 checks passed
@JuroOravec
JuroOravec deleted the events/clear-idle-check-thread branch November 24, 2026 21:12
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.

Free-threaded builds: an idle Events check thread keeps the last check's data for up to 60 seconds

1 participant