Krita 5.3/6.0 beta thread

Qt5 based Krita(5.3) hangs/freezes during Python loop:
Race condition between addShapesFromSvg and Shape.remove

About:

I have identified a critical freeze issue when performing batch operations on vector shapes via the Python API. The freeze occurs not on a single command, but when multiple shapes are processed in a loop involving both creation and deletion.
This will only happend in 5.3beta.
Other versions are no problem(5.2.x and 6.0).

How to reproduce:

  1. Create a Vector Layer with multiple shapes (e.g., 2〜3 shapes).
  2. Select all THE shapes(e.g., 2〜3 shapes).
  3. Run a sample script by scripter(Copy and paste it)
  4. Replaced the shapes are changed to text.

The script execution prints all messages to the console, suggesting the Python interpreter finishes its job. However, Krita hangs immediately afterward.

Simple Test script is here.

from krita import Krita

def main_loop():

    # Test code for a freeze in Krita 5.3.0.
    # The issue occurs when addShapesFromSvg and remove() are called 
    # repeatedly within a single loop.

    doc = Krita.instance().activeDocument()
    if not (doc and doc.activeNode()):
        print("Error: No active document or layer.")
        return
    
    node = doc.activeNode()
    
    # Target all selected shapes to simulate batch processing
    selected_shapes = [s for s in node.shapes() if s.isSelected()]
    
    if len(selected_shapes) < 2:
        print("Please select at least 2 or 3 vector shapes to trigger the race condition.")
        return

    print(f"Starting batch process for {len(selected_shapes)} shapes...")

    for i, old_shape in enumerate(selected_shapes):
        print(f"Processing shape {i+1}/{len(selected_shapes)}...")
        
        # 1. Capture current transformation matrix
        matrix = old_shape.transformation()
        
        # 2. Add a new shape via SVG
        # Rapidly calling addShapesFromSvg seems to stress the undo/event queue
        svg = '<svg><text y="20" font-size="20">Replaced </text></svg>'
        new_shapes = node.addShapesFromSvg(svg)
        
        if new_shapes:
            # 3. Apply transformation to the new shape
            new_shapes[0].setTransformation(matrix)
            
            # 4. Remove the old shape

            old_shape.remove()
            
    print("Loop execution finished in Python.")
    
    # Explicit refresh
    doc.refreshProjection()
    
    print("Success:the bug did not occur.")

main_loop()

This issue happen in Krita 5.3.0 only.
This is not affected by the OS version or whether the Mac is old or new.

  • Krita5.2.9 with Mojave(MacOS 10.14.7) → OK

  • Krita5.2.14 with Mojave(MacOS 10.14.7) → OK

  • Krita5.3.0-beta(6b6785d) with Mojave(MacOS 10.14.7) → Freeze

  • Krita5.4.0-beta(027815a) with Mojave(MacOS 10.14.7) → Freeze

  • Krita5.3.0-beta(6b6785d) with Sonoma(MacOS 14.7.1) → Freeze

  • Krita6.0.0-beta(6b6785d) with Sonoma(MacOS 14.7.1) → OK

See these logs for the details.

------------------------------------------------------------
Krita5.2.9 with Mojave(MacOS 10.14.7)
==== Warning: Script not saved! ====
Please select at least 2 or 3 vector shapes to trigger the race condition.
==== Warning: Script not saved! ====
Starting batch process for 3 shapes...
Processing shape 1/3...
Processing shape 2/3...
Processing shape 3/3...
Loop execution finished in Python.
Success:the bug did not occur.


Krita5.2.14-prealpha (git a504879) with Mojave(MacOS 10.14.7)
==== Warning: Script not saved! ====
Starting batch process for 3 shapes...
Processing shape 1/3...
Processing shape 2/3...
Processing shape 3/3...
Loop execution finished in Python.
Success:the bug did not occur.



Krita5.3.0-beta(6b6785d) with Mojave(MacOS 10.14.7)
==== Warning: Script not saved! ====
Starting batch process for 3 shapes...
Processing shape 1/3...
    (Freeze)

--------------------------------------------------------


Krita5.3.0-beta(6b6785d) with Sonoma(MacOS 14.7.1)
==== Warning: Script not saved! ====
Starting batch process for 3 shapes...
Processing shape 1/3...
    (Freeze)

Krita6.0.0-beta(6b6785d)with Sonoma(MacOS 14.7.1)
==== Warning: Script not saved! ====
Starting batch process for 3 shapes...
Processing shape 1/3...
Processing shape 2/3...
Processing shape 3/3...
Loop execution finished in Python.
Success:the bug did not occur.