MantisBT - VTK
View Issue Details
0015545VTK(No Category)public2015-06-16 14:202016-08-12 09:55
Edson Tadeu M. Manoel 
Kitware Robot 
highminoralways
closedmoved 
6.0.0 
 
TBD
crash
0015545: Observer crashes with Python thread, bug in vtkPythonCommand and also in dataset_adapter
  Currently, if an observer is added in a python thread and then executed in another thread after the first one has finished, it will cause a segfault/access violation in the python interpreter.

  This code reproduces the problem:


    from Queue import Queue
    from threading import Thread
    import vtk
    
    
    def on_delete(caller, event):
        print 'on_delete:'
        print ' caller:', caller
        print ' event:', event
    
    
    def create_object_with_observer(result_queue):
        object_with_observer = vtk.vtkObject()
        object_with_observer.AddObserver('DeleteEvent', on_delete)
    
        result_queue.put(object_with_observer)
    
    
    def test_observer_command_with_threads():
        result_queue = Queue()
    
        thread = Thread(target=create_object_with_observer, args=(result_queue,))
        thread.start()
        thread.join()
    
        object_with_observer = result_queue.get(block=True)
        print object_with_observer
        del object_with_observer # <-- crashes here
        print 'checkpoint'
    
    
    if __name__ == '__main__':
        test_observer_command_with_threads()


This seems to happen because of this line in vtkPythonCommand.cxx, which seems to be assigning a bad ThreadState pointer to _PyThreadState_Current:


    prevThreadState = PyThreadState_Swap(this->ThreadState);


(The pointer seems to be dangling. Perhaps vtkPythonCommand::SetThreadState should incref the PyThreadState* object?)

This bug is directly affecting dataset_adapter, because it causes a crash in this use case (dataset_adapter uses a command to store the numpy array ref):


    from Queue import Queue
    from threading import Thread
    from vtk.numpy_interface.dataset_adapter import UnstructuredGrid
    import numpy
    import vtk
    
    
    def create_unstructured_grid(result_queue):
        points = numpy.array([[0.0, 0.0, 0.0]])
        ug = UnstructuredGrid(vtk.vtkUnstructuredGrid())
        ug.SetPoints(points)
        result_queue.put(ug)
    
    
    def test_unstructured_grid_in_thread():
        result_queue = Queue()
        thread1 = Thread(target=create_unstructured_grid, args=(result_queue,))
        thread1.start()
        thread1.join()
        ug = result_queue.get(block=True)
        print ug
        del ug # <-- crashes
        print 'checkpoint'
    
    
    if __name__ == '__main__':
        test_unstructured_grid_in_thread()
I was able to reproduce the problem with the supplied test program.

Also, I replaced the DeleteEvent with UserEvent, and it still crashed:

    print object_with_observer
    object_with_observer.InvokeEvent(vtk.vtkCommand.UserEvent) # <-- crashes here
    print 'checkpoint'

Crash occurred in vtkPythonCommand.cxx line 222:

       219 arglist = Py_BuildValue((char*)"(Ns)", obj2, eventname);
       220 }
       221
    -> 222 PyObject *result = PyEval_CallObject(this->obj, arglist);
       223 Py_DECREF(arglist);
       224
       225 if (result)
No tags attached.
Issue History
2015-06-16 14:20Edson Tadeu M. ManoelNew Issue
2015-06-17 10:01David GobbiReproducibilityhave not tried => always
2015-06-17 10:01David GobbiSteps to Reproduce Updatedbug_revision_view_page.php?rev_id=1116#r1116
2016-07-06 16:47Berk GeveciStatusbacklog => expired
2016-08-12 09:55Kitware RobotNote Added: 0037395
2016-08-12 09:55Kitware RobotStatusexpired => closed
2016-08-12 09:55Kitware RobotResolutionopen => moved
2016-08-12 09:55Kitware RobotAssigned To => Kitware Robot

Notes
(0037395)
Kitware Robot   
2016-08-12 09:55   
Resolving issue as `moved`.

This issue tracker is no longer used. Further discussion of this issue may take place in the current VTK Issues page linked in the banner at the top of this page.