PyPlot.jl icon indicating copy to clipboard operation
PyPlot.jl copied to clipboard

Crashing Julia on Ctrl+C

Open warpuv opened this issue 7 years ago • 6 comments

Loop termination crashes Julia process if I'm using PyPlot inside the loop. PyPlot.backend is "qt5agg" Julia 0.6.0

using PyPlot 

for i=1:1000
    plot(randn(32))
    sleep(0)
end
Traceback (most recent call last):
  File "C:\Users\Yury Parfenov\.julia\v0.6\Conda\deps\usr\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 197, in __draw_idle_agg
    FigureCanvasAgg.draw(self)
  File "C:\Users\Yury Parfenov\.julia\v0.6\Conda\deps\usr\lib\site-packages\matplotlib\backends\backend_agg.py", line 464, in draw
    self.figure.draw(self.renderer)
  File "C:\Users\Yury Parfenov\.julia\v0.6\Conda\deps\usr\lib\site-packages\matplotlib\artist.py", line 63, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Users\Yury Parfenov\.julia\v0.6\Conda\deps\usr\lib\site-packages\matplotlib\figure.py", line 1144, in draw
    renderer, self, dsu, self.suppressComposite)
  File "C:\Users\Yury Parfenov\.julia\v0.6\Conda\deps\usr\lib\site-packages\matplotlib\image.py", line 139, in _draw_list_compositing_images
    a.draw(renderer)
  File "C:\Users\Yury Parfenov\.julia\v0.6\Conda\deps\usr\lib\site-packages\matplotlib\artist.py", line 63, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Users\Yury Parfenov\.julia\v0.6\Conda\deps\usr\lib\site-packages\matplotlib\axes\_base.py", line 2426, in draw
    mimage._draw_list_compositing_images(renderer, self, dsu)
  File "C:\Users\Yury Parfenov\.julia\v0.6\Conda\deps\usr\lib\site-packages\matplotlib\image.py", line 139, in _draw_list_compositing_images
    a.draw(renderer)
  File "C:\Users\Yury Parfenov\.julia\v0.6\Conda\deps\usr\lib\site-packages\matplotlib\artist.py", line 63, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Users\Yury Parfenov\.julia\v0.6\Conda\deps\usr\lib\site-packages\matplotlib\lines.py", line 822, in draw
    drawFunc(renderer, gc, tpath, affine.frozen())
  File "C:\Users\Yury Parfenov\.julia\v0.6\Conda\deps\usr\lib\site-packages\matplotlib\lines.py", line 1267, in _draw_lines
    self._lineFunc(renderer, gc, path, trans)
  File "C:\Users\Yury Parfenov\.julia\v0.6\Conda\deps\usr\lib\site-packages\matplotlib\lines.py", line 1293, in _draw_solid
    renderer.draw_path(gc, path, trans)
  File "C:\Users\Yury Parfenov\.julia\v0.6\Conda\deps\usr\lib\site-packages\matplotlib\backends\backend_agg.py", line 166, in draw_path
    self._renderer.draw_path(gc, path, transform, rgbFace)
KeyboardInterrupt

warpuv avatar Jul 14 '17 20:07 warpuv

I also have a weird and long-standing (since at least Julia 0.4.7) issue with PyPlot and ctrl-C. This is for a Windows 10 installation. Once my code begins and PyPlot is imported, Ctrl-C no longer works in the REPL to interrupt my code. What happens instead is that the keypush of Ctrl-C is somehow 'saved', so that the next time a PyPlot function call is executed, the very old Ctrl-C keypush finally interrupts the code.

StephenVavasis avatar Aug 30 '17 20:08 StephenVavasis

I have a similar issue: if I want to interrupt the execution of my code in the middle of a for loop containing 'plot' (useful for animations) using Ctrl+C, PyPlot hangs and eventually Julia crashes. I'm using PyPlot and Qt5 as a backend. Any workarounds? I'm on Julia 1.0.2.

MaxandreJ avatar Feb 26 '19 23:02 MaxandreJ

Once my code begins and PyPlot is imported, Ctrl-C no longer works in the REPL to interrupt my code.

I think what is happening is that the PyCall gui event loop, which is happening in an asynchronous task, is the one catching these ctrl-c signals instead of the main task.

I'm not sure if there is a way to disable interrupts just for the gui task?

stevengj avatar Mar 07 '19 14:03 stevengj

Could you try editing PyCall (do dev PyCall at the package prompt and then edit .julia/dev/PyCall/src/gui.jl) to change install_doevent to:

function install_doevent(doevent, sec::Real)
    return Base.Timer(sec, interval=sec) do async
        Base.sigatomic_begin() # like Base.disable_sigint, disable interrupts
        doevent(async)
        Base.sigatomic_end()
    end
end

stevengj avatar Mar 07 '19 14:03 stevengj

I've just done what you suggested @stevengj and unfortunately it doesn't fix the problem. Matplotlib still crashes if I do a Ctrl-C.

MaxandreJ avatar Mar 07 '19 17:03 MaxandreJ

Does the traceback that you get come from Python? (i.e., the one in OP?) I'm a bit confused about why can KeyboardInterrupt even fire here.

Maybe you can use sys.excepthook = traceback.print_exception to workaround the problem? See: https://coldfix.eu/2016/11/08/pyqt-boilerplate/#handling-exceptions-pyqt5

tkf avatar Mar 08 '19 11:03 tkf