View Issue Details Jump to Notes ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0015545VTK(No Category)public2015-06-16 14:202016-08-12 09:55
ReporterEdson Tadeu M. Manoel 
Assigned ToKitware Robot 
PriorityhighSeverityminorReproducibilityalways
StatusclosedResolutionmoved 
PlatformOSOS Version
Product Version6.0.0 
Target VersionFixed in Version 
Summary0015545: Observer crashes with Python thread, bug in vtkPythonCommand and also in dataset_adapter
Description  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()
Steps To ReproduceI 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)
TagsNo tags attached.
ProjectTBD
Typecrash
Attached Files

 Relationships

  Notes
(0037395)
Kitware Robot (administrator)
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.

 Issue History
Date Modified Username Field Change
2015-06-16 14:20 Edson Tadeu M. Manoel New Issue
2015-06-17 10:01 David Gobbi Reproducibility have not tried => always
2015-06-17 10:01 David Gobbi Steps to Reproduce Updated
2016-07-06 16:47 Berk Geveci Status backlog => expired
2016-08-12 09:55 Kitware Robot Note Added: 0037395
2016-08-12 09:55 Kitware Robot Status expired => closed
2016-08-12 09:55 Kitware Robot Resolution open => moved
2016-08-12 09:55 Kitware Robot Assigned To => Kitware Robot


Copyright © 2000 - 2018 MantisBT Team