From d22e28cf7e3c2a589a9399b30c13009949f28eb2 Mon Sep 17 00:00:00 2001 From: wuxiaobai24 Date: Tue, 2 Dec 2025 08:44:58 +0800 Subject: [PATCH 1/3] support free threaded --- Cargo.toml | 2 +- pyproject.toml | 2 ++ rust/cocoindex/Cargo.toml | 3 ++- rust/cocoindex/src/py/mod.rs | 2 +- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9fec1f59..6bcce3c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,10 +10,10 @@ license = "Apache-2.0" [workspace.dependencies] pyo3 = { version = "0.27.1", features = [ - "abi3-py311", "auto-initialize", "chrono", "uuid", + "extension-module", ] } pythonize = "0.27.0" pyo3-async-runtimes = { version = "0.27.0", features = ["tokio-runtime"] } diff --git a/pyproject.toml b/pyproject.toml index 38c4a948..baa6ff98 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -82,6 +82,8 @@ all = [ # This is the set of dependencies that needs to be installed for the CI workflow. all-ci = ["pytest", "pytest-asyncio", "mypy", "pydantic>=2.11.9"] +free-threaded = ["sentence-transformers>=3.3.1", "colpali-engine"] + [tool.mypy] python_version = "3.11" strict = true diff --git a/rust/cocoindex/Cargo.toml b/rust/cocoindex/Cargo.toml index 91fab53b..55ac1873 100644 --- a/rust/cocoindex/Cargo.toml +++ b/rust/cocoindex/Cargo.toml @@ -11,7 +11,8 @@ crate-type = ["cdylib"] [features] default = ["legacy-states-v0"] -legacy-states-v0 = [] +legacy-states-v0 = [ "pyo3/abi3-py311"] +free-threaded = [] [dependencies] cocoindex_utils = { path = "../utils", features = [ diff --git a/rust/cocoindex/src/py/mod.rs b/rust/cocoindex/src/py/mod.rs index d4653fe4..bdb25c94 100644 --- a/rust/cocoindex/src/py/mod.rs +++ b/rust/cocoindex/src/py/mod.rs @@ -603,7 +603,7 @@ fn seder_roundtrip<'py>( } /// A Python module implemented in Rust. -#[pymodule] +#[pymodule(gil_used = false)] #[pyo3(name = "_engine")] fn cocoindex_engine(m: &Bound<'_, PyModule>) -> PyResult<()> { m.add("__version__", env!("CARGO_PKG_VERSION"))?; From 9ce879b4b563b288b8418fdb92fa3e93826901db Mon Sep 17 00:00:00 2001 From: wuxiaobai24 Date: Tue, 2 Dec 2025 09:15:20 +0800 Subject: [PATCH 2/3] add test for free-threaed --- .github/workflows/_test.yml | 79 +++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/.github/workflows/_test.yml b/.github/workflows/_test.yml index 799f7a8e..b528487d 100644 --- a/.github/workflows/_test.yml +++ b/.github/workflows/_test.yml @@ -75,6 +75,85 @@ jobs: run: | ${{ matrix.platform.python_exec }} -m pytest --capture=no python/cocoindex/tests + build-test-free-threaded: + runs-on: ubuntu-latest + env: + RUSTFLAGS: "-C debuginfo=0" + CARGO_INCREMENTAL: "0" + SCCACHE_GHA_ENABLED: "true" + RUSTC_WRAPPER: "sccache" + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 1 + + - name: Run sccache-cache + uses: mozilla-actions/sccache-action@v0.0.9 + + - name: Setup Python (3.14 free-threaded) + uses: actions/setup-python@v5 + with: + python-version: '3.14' + allow-prereleases: true + freethreaded: true + + - name: Verify interpreter configuration + run: | + python - <<'PY' +import sys +import sysconfig + +print(f"Python version: {sys.version}") +print(f"GIL enabled? {getattr(sys, '_is_gil_enabled', 'missing')}") +print(f"SOABI: {sysconfig.get_config_var('SOABI')}") +if not hasattr(sys, '_is_gil_enabled') or sys._is_gil_enabled(): + raise SystemExit("Free-threaded interpreter with disabled GIL is required") +PY + + - run: rustup toolchain install stable --profile minimal + + - name: Rust tests (no default features) + run: cargo test --no-default-features --verbose + + - name: Rust tests + run: cargo test --verbose + + - name: Setup venv + run: | + python -m venv .venv + + - name: Install Python toolchains + run: | + .venv/bin/python -m pip install --upgrade pip + .venv/bin/python -m pip install maturin + + - name: Python build (free-threaded) + run: | + .venv/bin/python -m maturin develop --no-default-features -F free-threaded --strip -E all-ci,free-threaded + + - name: Verify CocoIndex in free-threaded mode + run: | + .venv/bin/python - <<'PY' +import sys +import sysconfig +import cocoindex + +print(f"CocoIndex version: {cocoindex.__version__}") +print(f"GIL enabled? {getattr(sys, '_is_gil_enabled', 'missing')}") +print(f"SOABI: {sysconfig.get_config_var('SOABI')}") +if sys._is_gil_enabled(): + raise SystemExit("Expected free-threaded interpreter with disabled GIL") +PY + + - name: Python type check (mypy) + run: | + .venv/bin/python -m mypy python + + - name: Python tests (free-threaded) + run: | + .venv/bin/python -m pytest --capture=no python/cocoindex/tests + validate-3p-notices: runs-on: ubuntu-latest steps: From eed4e9b2f3672645e65a1c141815f50fc7a259a2 Mon Sep 17 00:00:00 2001 From: wuxiaobai24 Date: Wed, 3 Dec 2025 08:55:51 +0800 Subject: [PATCH 3/3] fix abi3 and free-threaded feature --- rust/cocoindex/Cargo.toml | 5 +++-- rust/cocoindex/src/py/mod.rs | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/rust/cocoindex/Cargo.toml b/rust/cocoindex/Cargo.toml index 55ac1873..b104fcb1 100644 --- a/rust/cocoindex/Cargo.toml +++ b/rust/cocoindex/Cargo.toml @@ -10,8 +10,9 @@ name = "cocoindex_engine" crate-type = ["cdylib"] [features] -default = ["legacy-states-v0"] -legacy-states-v0 = [ "pyo3/abi3-py311"] +default = ["legacy-states-v0", "abi3"] +legacy-states-v0 = [] +abi3 = ["pyo3/abi3-py311"] free-threaded = [] [dependencies] diff --git a/rust/cocoindex/src/py/mod.rs b/rust/cocoindex/src/py/mod.rs index bdb25c94..87fb8030 100644 --- a/rust/cocoindex/src/py/mod.rs +++ b/rust/cocoindex/src/py/mod.rs @@ -603,7 +603,8 @@ fn seder_roundtrip<'py>( } /// A Python module implemented in Rust. -#[pymodule(gil_used = false)] +#[cfg_attr(feature = "free-threaded", pymodule(gil_used = false))] +#[cfg_attr(not(feature = "free-threaded"), pymodule)] #[pyo3(name = "_engine")] fn cocoindex_engine(m: &Bound<'_, PyModule>) -> PyResult<()> { m.add("__version__", env!("CARGO_PKG_VERSION"))?;