Skip to content

Declare citry_core safe without the GIL (gil_used = false) #433

Open

Description

@JuroOravec

Problem

crates/citry_core_py/src/lib.rs:57 declares the extension module #[pymodule(gil_used = true)]. On a free-threaded CPython build (3.14t, 3.15t), importing citry_core._rust turns the GIL back on for the whole process, unless PYTHON_GIL=0 is set. Python then warns:

RuntimeWarning: The global interpreter lock (GIL) has been enabled to load module 'citry_core._rust', which has not declared that it can run safely without the GIL.

So Citry users on free-threaded builds get a process that runs with the GIL. The weekly free-threaded test run (#264) shows the same thing.

The declaration exists because the native render code (citry_core._rust.native_render) reads Python dicts and lists without locking them. That is safe only under the GIL.

What already keeps free-threaded builds safe

packages/py/citry/citry/_vue/native_render.py:108 (_decide) refuses the native render path whenever the build has Py_GIL_DISABLED. It checks the build setting, not the runtime GIL state. A free-threaded build therefore always renders on the Python path and never calls native_render, even after the GIL comes back on.

Audit so far (read-only, not built)

  • native_render reads containers without locking. Free-threaded builds never call it, because of the check above.
  • native_render/runs.rs:505 holds a static SHARED_DATA: PyOnceLock<...>. PyO3 provides PyOnceLock for free-threaded builds.
  • The lazy_static! tables in citry_template_parser/src/constants.rs (lines 283, 451, 531) are read-only constants, and the macro requires them to be Sync.
  • The thread_local! RefCells in citry_html_transform/src/hydration_nesting.rs:157 and citry_vue_compiler/src/lib.rs:278 each belong to a single thread.
  • No static mut and no unsafe impl Sync/Send appear in the crates.

Open item

Every PyO3 entry point that walks a Python dict or list argument needs checking: the extraction into Rust types, and any PyDict or PyList iteration outside native_render. PyO3 0.30 locks a dict while it iterates it on free-threaded builds. Code that holds a borrowed item across a call that can run Python code, or that reads a list's length and its items in separate steps, still needs a look.

Done when

  • That open item is audited, and any container access that needs a lock is fixed.
  • The module declares gil_used = false.
  • The free-threaded run in Run the Python test suite on PyPy and free-threaded CPython #264 passes without the GIL. That run already includes a report-only PYTHON_GIL=0 step over the render, serialize, Events and thread tests.

Activity

  1. added
    next-sprint
    on Oct 27, 2026
  2. JuroOravec commented on Oct 27, 2026

    @JuroOravec
    MemberAuthor

    Postponed until after 0.7.0, as the maintainer decided. Until then, a free-threaded build (cp314t, cp315t) installs and runs Citry with the GIL turned back on, and the weekly free-threaded run in #264 keeps a report-only PYTHON_GIL=0 step as evidence for this work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    next-sprint

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions