Skip to content

Fix Python ownership detection and stabilize GIL handling for binding…#652

Merged
marvinoe21 merged 1 commit into
masterfrom
fix/python-owner-detection-and-gil-deadlock
May 7, 2026
Merged

Fix Python ownership detection and stabilize GIL handling for binding…#652
marvinoe21 merged 1 commit into
masterfrom
fix/python-owner-detection-and-gil-deadlock

Conversation

@marvinoe21
Copy link
Copy Markdown
Collaborator

@marvinoe21 marvinoe21 commented May 7, 2026

This PR fixes a deadlock in the interaction between the Python module, GTlabConsole, and the Python binding.

Previously, interpreter ownership was inferred from Py_IsInitialized(), which could be incorrect when Python had already been initialized by another module. This led to deadlocks during context creation in CLI mode.

Ownership is now controlled via GTLAB_PYTHON_INTERPRETER_OWNER (gtlab / external), with automatic fallback initialization based on the current application name. This makes ownership detection deterministic and stabilizes GIL handling across both GTlabConsole and the Python binding.

Summary by Sourcery

Stabilize Python interpreter ownership detection and GIL handling by making interpreter ownership explicit and environment-driven.

Bug Fixes:

  • Prevent deadlocks during context creation when Python is pre-initialized by another module by decoupling interpreter ownership from Py_IsInitialized().

Enhancements:

  • Introduce an environment-driven mechanism (GTLAB_PYTHON_INTERPRETER_OWNER) to deterministically decide whether GTlab owns the embedded Python interpreter or attaches to an external one.
  • Adjust PythonQt initialization flags to correctly reflect whether Python is already initialized while respecting the new ownership model.

@marvinoe21 marvinoe21 self-assigned this May 7, 2026
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 7, 2026

Reviewer's Guide

Introduces deterministic Python interpreter ownership via an environment variable and adjusts the context manager to respect external interpreters and existing initialization while stabilizing GIL-related behavior.

File-Level Changes

Change Details Files
Make Python interpreter ownership explicit and deterministic via environment configuration instead of inferring from Py_IsInitialized.
  • Add env::PYTHON_OWNER_ENV_VAR and corresponding values env::PYTHON_OWNER_GTLAB and env::PYTHON_OWNER_EXTERNAL to gtpy constants.
  • Introduce helper functions ensurePythonOwnerEnvVar and isGtlabPythonOwner to derive and cache ownership based on application name or explicit environment configuration.
  • Update GtpyContextManager to use isGtlabPythonOwner to decide whether the module owns the interpreter rather than relying on Py_IsInitialized.
src/module/utilities/gtpy_globals.h
src/module/utilities/gtpy_contextmanager.cpp
Decouple PythonQt initialization flags from interpreter ownership to avoid reinitialization and stabilize GIL handling when Python is already initialized.
  • Preserve detection of existing Python initialization with Py_IsInitialized and derive PythonQt flags from that instead of ownership.
  • Ensure embedded mode (RedirectStdOut) is only used when Python is not yet initialized, and PythonAlreadyInitialized when attaching to an existing interpreter.
src/module/utilities/gtpy_contextmanager.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@rainman110
Copy link
Copy Markdown
Contributor

Looks IMHO very good

@marvinoe21 marvinoe21 requested a review from rainman110 May 7, 2026 14:23
@marvinoe21 marvinoe21 marked this pull request as ready for review May 7, 2026 14:23
@marvinoe21 marvinoe21 merged commit dd342a8 into master May 7, 2026
2 checks passed
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Consider guarding ensurePythonOwnerEnvVar() against cases where QCoreApplication is not yet constructed or applicationFilePath() is empty, to avoid relying on an undefined or empty executable name when setting the default owner.
  • In isGtlabPythonOwner(), it might be useful to handle unexpected GTLAB_PYTHON_INTERPRETER_OWNER values (e.g., log or fall back explicitly) so misconfigured environments don’t silently default to one ownership mode based only on a string comparison.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider guarding `ensurePythonOwnerEnvVar()` against cases where `QCoreApplication` is not yet constructed or `applicationFilePath()` is empty, to avoid relying on an undefined or empty executable name when setting the default owner.
- In `isGtlabPythonOwner()`, it might be useful to handle unexpected `GTLAB_PYTHON_INTERPRETER_OWNER` values (e.g., log or fall back explicitly) so misconfigured environments don’t silently default to one ownership mode based only on a string comparison.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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.

2 participants