Skip to content

Unsafe cross-thread access to package objects during Python script evaluation in wizards #650

@marvinoe21

Description

@marvinoe21

Summary

GtpyAbstractScriptingWizardPage exposes package objects to the Python context so that scripts can access them during evaluation. These package objects are cloned in GtpyAbstractScriptingWizardPage::loadPackages() on the GUI thread, parented to the wizard page, and inserted into the Python context.

Script evaluation is later started asynchronously in GtpyAbstractScriptingWizardPage::evalScript() by creating a GtpyScriptRunnable and submitting it to the global QThreadPool.

As a result, the Python context contains QObject-based package objects with GUI-thread affinity, while the Python script itself is executed on a different thread.

If the script accesses one of these package objects and triggers functionality that depends on QObject thread affinity, Qt reports thread-affinity assertions.

In a nutshell

The wizard currently does this:

  1. Clone package objects on the GUI thread.
  2. Add those objects to the Python context.
  3. Run the Python script in a worker thread via GtpyScriptRunnable.
  4. Allow the script to access the injected package objects from that worker thread.

This is unsafe because the package objects retain GUI-thread affinity while being accessed from a worker thread.

Expected Behaviour

Package objects exposed to Python should only be accessed from the thread that owns them.

Possible Fix

Ensure that both the package objects and the Python evaluation run on the same thread, e.g. move the objects to the evaluation thread before execution. The key requirement is that object access and thread affinity must match.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions