Skip to content

SIGINT is ignored by MacOSX backend #3991

Closed

Description

@Equidamoid

Here is an example:

import matplotlib
#matplotlib.use('Qt4Agg')
from matplotlib import pyplot as plt
plt.plot(1, 1)
plt.show()

Run this script from terminal, wait for the window to appear and press Ctrl-C (in terminal). The script will not die.
Then uncomment the matplotlib.use line an Ctrl-C will work as intended.

UPD:
If I press Ctrl-C and then move mouse over the plot window the following exception appears in the log:

Traceback (most recent call last):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/backend_bases.py", line 1850, in enter_notify_event
    def enter_notify_event(self, guiEvent=None, xy=None):
KeyboardInterrupt

The same is for callback set using signal.signal(signal.SIGTERM, cb), callback is not called until window receives any event (focus change, mouse move, etc).

Activity

  1. Equidamoid commented on Jan 10, 2015

    @Equidamoid
    Author

    I've found a workaround to make script exit on sigint. Backend's event loop seems to block (sigmask or sigsuspend?) signals while waiting for a new event so we need to introduce an event that will fire without any interation with the window -- a timer. Next, all interrupts are catched somewhere inside matplotlib code so we need to make own SIGINT handler that will end the current process.
    Here is the example code:

    from matplotlib import pyplot as plt
    import signal
    import os
    
    def sig(a, b):
        print("got sigint, exitting!")
        os._exit(0)
    
    def timercb(e):
        print("timer")
    signal.signal(signal.SIGINT, sig)
    
    plt.plot(1)
    fig, ax = plt.subplots()
    timer = fig.canvas.new_timer(interval=5000)
    timer.add_callback(timercb, ax)
    timer.start()
    plt.show()
    

    I set the timer interval to 5 sec to make it sensible that signal arrives only after timer event happens.

  2. added this to the milestone on Jan 10, 2015
  3. tacaswell commented on Jan 10, 2015

    @tacaswell
    Member
  4. WeatherGod commented on Jan 10, 2015

    @WeatherGod
    Member

    This is an interesting idea. I wonder if this approach (the timer, that is)
    might be a suitable trick to deal with the other issues we are having with
    the macosx backend and its eventloop?

    On Sat, Jan 10, 2015 at 9:41 AM, Thomas A Caswell [email protected]
    wrote:

    attn @mdehoon https://github.com/mdehoon

    —
    Reply to this email directly or view it on GitHub
    #3991 (comment)
    .

  5. mdehoon commented on Jan 13, 2015

    @mdehoon
    Contributor

    @WeatherGod Which other issues are there with the macosx backend and its event loop?

  6. tacaswell commented on Jan 13, 2015

    @tacaswell
    Member

    I assume @WeatherGod is referring to bilt not working.

  7. mdehoon commented on Jan 13, 2015

    @mdehoon
    Contributor

    @WeatherGod

    This is an interesting idea. I wonder if this approach (the timer, that is)
    might be a suitable trick to deal with the other issues we are having with
    the macosx backend and its eventloop?

    When working on event loops, one has to resist the urge to use tricks to solve issues. Tricks may solve a particular symptom but you will end up with a bunch of hacks that will be very difficult to disentangle.

  8. mdehoon commented on Jan 13, 2015

    @mdehoon
    Contributor

    @tacaswell The bug in that case is not caused by the MacOSX backend, but by the upstream code trying to draw stuff outside of the event loop.

  9. WeatherGod commented on Jan 13, 2015

    @WeatherGod
    Member

    @mdehoon, Fair enough. and I can see a bunch of problems with the idea for
    blitting anyway. In any case, what do you think of this PR?

    On Mon, Jan 12, 2015 at 9:28 PM, mdehoon [email protected] wrote:

    @tacaswell https://github.com/tacaswell The bug in that case is not
    caused by the MacOSX backend, but by the upstream code trying to draw stuff
    outside of the event loop.

    —
    Reply to this email directly or view it on GitHub
    #3991 (comment)
    .

  10. mdehoon commented on Jan 14, 2015

    @mdehoon
    Contributor

    @WeatherGod The suggested bug fix should not be applied, as this is not a bug in the MacOSX backend, but in Python itself. See issue 3180 on the Python website:
    http://bugs.python.org/issue3180
    Note that the bug also appears with other backends (e.g. Tkinter, see the example on the Python bug report).
    It could help if you guys poke the Python developers on this issue if it stalls.
    Depending on how issue 3180 is fixed by the Python developers, some changes to the MacOSX backend may be needed at that time. Let's keep this issue open until then.

  11. mdehoon commented on Jan 17, 2015

    @mdehoon
    Contributor

    This pull request #4006 allows the delivery of interrupts once Python is fixed.

  12. petehuang commented on Jan 8, 2017

    @petehuang
    Contributor

    Can someone on OSX try to reproduce using the snippet in OP?

  13. efiring commented on Jan 8, 2017

    @efiring
    Member

    I can still reproduce the original problem (Ctl-C does not close the window) with master and python 3.5.2 from Anaconda.

  14. tacaswell commented on Jan 8, 2017

    @tacaswell
    Member

    https://bugs.python.org/issue23237 <- @mdehoon 's upstream patch is still not merged, closing as there is nothing we can do about this.

  15. tacaswell commented on Jan 8, 2017

    @tacaswell
    Member

    @efiring does this problem also persist with mpl 2.0.0rc2 + IPython?

  16. efiring commented on Jan 8, 2017

    @efiring
    Member

    I can't run it from ipython on my system; I am using Anaconda, which is running ipython with a non-framework build of python. I can only test it as a standalone script using pythonw.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions