Repository navigation
fix(events): free a check's data promptly on free-threaded Python with the GIL off - #870
Merged
Merged
Conversation
…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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
(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.rundrops 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._serveinpackages/py/citry/citry/ext/events/_pydantic_bounded.py:_CHECK_THREAD_WAKE_SECONDS). Each wake runs the loop, which frees what callers dropped since the last wake.time.monotonic(such as freezegun) cannot stretch the idle time.sys._is_gil_enabled()before each wait, because importing an extension such ascitry_corecan turn the GIL back on.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_collectorno 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.test_an_idle_check_thread_wakes_often_without_the_gil,test_an_idle_check_thread_without_the_gil_still_ends_after_its_idle_time, andtest_an_idle_check_thread_with_the_gil_waits_out_its_idle_time_at_once.test_events_pydantic_bounded.pypassed (233 tests, 3 runs). The Events tests passed (2975 passed, 68 skipped). ruff and mypy are clean, including mypy--platform linux.citry-corewheel 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
🤖 Generated with Claude Code