FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Python crashes when closing figures using TkAgg on Mac OS · Issue #9856 · matplotlib/matplotlib · GitHub

Repository navigation

Python crashes when closing figures using TkAgg on Mac OS #9856

Closed

Description

Bug report

Bug summary

Python crashes when I create and then close figures in a for loop using the TkAgg backend on Mac OS X in an interactive session. The Python session crashes out with

called Tcl_FindHashEntry on deleted table
Abort trap: 6

This may be related to #7743 and #8387.

Code for reproduction

import sys
import matplotlib as mpl
import pylab as plt

print('OS: %s'%sys.platform)
print('matplotlib version: %s'%mpl.__version__)
print('Backend: %s'%matplotlib.get_backend())

for i in range(3):
    plt.figure()
    plt.close()

plt.figure()
plt.plot([0,1],[0,1])
plt.show()

Actual outcome

$ python -i figure_close.py
OS: darwin
matplotlib version: 2.0.2
Backend: TkAgg
called Tcl_FindHashEntry on deleted table
Abort trap: 6

Expected outcome

I would expect this code to create and destroy three figures before creating and retaining the last figure.

Matplotlib version

  • Operating system: Mac OS X 10.12.1
  • Matplotlib version: 2.0.2
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg
  • Python version: 2.7.13

This was matplotlib installed from conda from the conda-forge channel.

Activity

  1. tacaswell commented on Nov 25, 2017

    Member

    I get slightly different (and less fatal) errors with masterish on linux:

    18:44 $ python /tmp/tk_close.py 
    OS: linux
    matplotlib version: 2.1.0.post289+g88a20b7e6
    Backend: TkAgg
    can't invoke "event" command:  application has been destroyed
        while executing
    "event generate $w <<ThemeChanged>>"
        (procedure "ttk::ThemeChanged" line 6)
        invoked from within
    "ttk::ThemeChanged"
    can't invoke "event" command:  application has been destroyed
        while executing
    "event generate $w <<ThemeChanged>>"
        (procedure "ttk::ThemeChanged" line 6)
        invoked from within
    "ttk::ThemeChanged"
    can't invoke "event" command:  application has been destroyed
        while executing
    "event generate $w <<ThemeChanged>>"
        (procedure "ttk::ThemeChanged" line 6)
        invoked from within
    "ttk::ThemeChanged"
    
    

    My guess is that we are creating an tearing down Tk objects when the figures are created and then destroyed, but are leaving things in a limbo-like state. When we finally let the tk event loop run with show() it dies.

  2. jklymak commented on Dec 2, 2017

    Member

    I get the same as @tacaswell on master-ish:

    OS: darwin
    matplotlib version: 2.1.0.post954+g1d4160c4b
    Backend: TkAgg
    can't invoke "event" command:  application has been destroyed
        while executing
    "event generate $w <<ThemeChanged>>"
    

    This unsightly, but it doesn't "crash" python on macOS. Tempted to close this, but I am going to remove the OS/Apple label because the behaviour is the same in MacOS and linux (at least on master).

  3. added this to the milestone on Dec 3, 2017
  4. tacaswell commented on Dec 3, 2017

    Member

    We should leave this open, there is a real race condition here. Frequently these sorts of things are responsible for more than one odd behavior (that are hard to reproduce due to the race), having a more-or-less reproducible repro example is helpful!

  5. kadrlica commented on Dec 3, 2017

    Author

    I have seen similar warnings (i.e. can't invoke "event" command: ...) in other situations with version 2.0.2, but they do not appear for me with this simple working example.

  6. anntzer commented on Dec 3, 2017

    Contributor

    Does #9275 help?

  7. mattip commented on Dec 8, 2017

    Contributor

    This reproduces in PyPy (with PR #9275 and PR #9356 ) as well.

  8. mattip commented on Dec 8, 2017

    Contributor

    Let me know if I am being too noisy, I am diving into this and using this issue as a record of my work.

    Adding some print statements, it seems the problem is starting the event loop after closing the images

    first the diff:

    diff --git a/lib/matplotlib/backends/backend_tkagg.py b/lib/matplotlib/backends/backend_tkagg.py
    index 7a36dc6..6e23b96 100644
    --- a/lib/matplotlib/backends/backend_tkagg.py
    +++ b/lib/matplotlib/backends/backend_tkagg.py
    @@ -214,6 +214,10 @@ class FigureCanvasTkAgg(FigureCanvasAgg):
             self._master = master
             self._tkcanvas.focus_set()
     
    +    def close_event(self, *args, **kwargs):
    +        print('in close_event')
    +        FigureCanvasAgg.close_event(self, *args, **kwargs)
    +
         def resize(self, event):
             width, height = event.width, event.height
             if self._resize_callback is not None:
    @@ -1079,4 +1083,6 @@ class _BackendTkAgg(_Backend):
     
         @staticmethod
         def mainloop():
    +        print('starting main loop')
             Tk.mainloop()
    +        print('stopping main loop')
    
    

    which now prints:

    OS: linux2
    matplotlib version: 2.1.0+989.g1712678
    Backend: TkAgg
    in close_event
    in close_event
    in close_event
    starting main loop
    can't invoke "event" command: application has been destroyed
        while executing
    "event generate $w <<ThemeChanged>>"
        (procedure "ttk::ThemeChanged" line 6)
        invoked from within
    "ttk::ThemeChanged"
    can't invoke "event" command: application has been destroyed
        while executing
    "event generate $w <<ThemeChanged>>"
        (procedure "ttk::ThemeChanged" line 6)
        invoked from within
    "ttk::ThemeChanged"
    in close_event
    stopping main loop
    

    Note that the errors are after "starting main loop", in other words only after using pylab.show()

  9. mattip commented on Dec 8, 2017

    Contributor

    or perhaps pyplot.close() is not "doing enough" to remove pending events

  10. mattip commented on Dec 8, 2017

    Contributor

    PR #9956 fixes the issue for me (linux, PyPy). Could someone on macOS check that it solves the issue for them as well?

  11. tacaswell commented on Dec 13, 2017

    Member

    @mattip Don't worry about being too noisy!

  12. efiring commented on Dec 13, 2017

    Member

    Closed by #9956.
    I verified this on OSX.

  13. modified the milestones: , v2.2.0 on Feb 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL