Skip to content

refactor(python-bindings): migrate to pyo3 0.27.2 threading primitives#25

Merged
vvanglro merged 2 commits into
mainfrom
feat/dep
Dec 4, 2025
Merged

refactor(python-bindings): migrate to pyo3 0.27.2 threading primitives#25
vvanglro merged 2 commits into
mainfrom
feat/dep

Conversation

@vvanglro

@vvanglro vvanglro commented Dec 3, 2025

Copy link
Copy Markdown
Member

Modernize Python integration layer by adopting latest pyo3 patterns. The GIL release mechanism switches from allow_threads() to detach() across all ULID generation and conversion functions (new, from_string, from_uuid, from_datetime, from_timestamp, from_parts). Module setup now uses Python::initialize() instead of the outdated prepare_freethreaded_python().

This refactoring maintains identical functionality while ensuring compatibility with pyo3's current API surface and threading model.

Modernize Python integration layer by adopting latest pyo3 patterns.
The GIL release mechanism switches from allow_threads() to detach()
across all ULID generation and conversion functions (new, from_string,
from_uuid, from_datetime, from_timestamp, from_parts). Module setup now
uses Python::initialize() instead of the outdated prepare_freethreaded_python().

This refactoring maintains identical functionality while ensuring
compatibility with pyo3's current API surface and threading model.

Signed-off-by: vvanglro <vvanglro@gmail.com>
@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello @vvanglro, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on modernizing the Python integration layer by adopting the latest pyo3 patterns and API. The changes ensure compatibility with current pyo3 versions and improve the threading model by updating the GIL release mechanism and module initialization, all while maintaining existing functionality.

Highlights

  • Dependency Update: The pyo3 dependency and its related crates (pyo3-build-config, pyo3-ffi, pyo3-macros, pyo3-macros-backend) have been upgraded from version 0.24.1 to 0.27.2.
  • GIL Release Mechanism: The Python Global Interpreter Lock (GIL) release mechanism has been updated from _py.allow_threads() to the more modern _py.detach() across all ULID generation and conversion functions.
  • Module Initialization: The Python module setup now uses pyo3::Python::initialize() instead of the deprecated pyo3::prepare_freethreaded_python().
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly migrates the Python bindings from pyo3 version 0.24.1 to 0.27.2. The changes properly replace the deprecated allow_threads() with detach() and prepare_freethreaded_python() with Python::initialize(), aligning with the latest pyo3 API for handling the GIL and multithreading.

I've found a critical issue in the from_string function related to memory safety when releasing the GIL. While this issue was present before the refactoring, it's crucial to address it as part of these threading-related changes to prevent potential use-after-free vulnerabilities. My review includes a specific comment with a suggested fix for this.

Comment thread src/lib.rs Outdated
…losure

The `detach` closure moves to a separate thread where the original
reference lifetime cannot be guaranteed. Converting to owned String
before the closure ensures the value remains valid in the detached
context.

Signed-off-by: vvanglro <vvanglro@gmail.com>
@vvanglro
vvanglro merged commit 1ec0e8b into main Dec 4, 2025
16 checks passed
@vvanglro
vvanglro deleted the feat/dep branch December 4, 2025 07:01
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