classification
Title: IDLE shutdown glitch when started by import
Type: behavior Stage: patch review
Components: Tkinter Versions: Python 3.6, Python 3.5, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: markroseman, serhiy.storchaka, terry.reedy
Priority: normal Keywords: patch

Created on 2016-05-15 05:25 by terry.reedy, last changed 2016-06-13 00:12 by markroseman.

Files
File name Uploaded Description Edit
tkinter_destroy_cancel_handlers.patch serhiy.storchaka, 2016-05-15 09:21 review
Messages (6)
msg265581 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-05-15 05:25
Win10, 3.5.1: Run 'python' in interactive mode.
>>> import idlelib.idle  # open idle shell
# or import idlelib.__main__
# only works once in an interactive session as re-import does not rerun
# In IDLE, open editor to some file.  For instance, alt-M, idlelib.run.

# Close editor, close shell.  In python, see something like
>>> bgerror failed to handle background error.
    Original error: invalid command name "2361007434952font_timer_event"
    Error in bgerror: can't invoke "tk" command: application has been destroyed"

Exit python, restart, import to start idle.
Close shell, close editor.  In python window, see
>>> invalid command name "1220802489864timer_event"
    while executing
"1220802489864timer_event"
    ("after" script)
invalid command name "1220865810504font_timer_event"
    while executing
"1220865810504font_timer_event"
    ("after" script)

Opening IDLE directly with 'python -m idlelib' (or 'idlelib.idle') does not show the problem.  So this is not a problem for the vast majority of users who run idle in an idle process.

My guess is that shutting down the entire process instead of just the idle part of a process shuts down tcl/tk and any pending callbacks.  Perhaps some tkinter class is called without passing the explicit IDLE root.  Though I then would not understand why the callback fails.

My guess is that the order dependence is associated with the startup and shutdown code being part of PyShell instead of being in an independent app module.  (I hope to change this.)

Serhiy, do you have any additiional insight into what is going on and what I should look for?
msg265594 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-05-15 08:30
There are few possible workarounds:

1. Catch and ignore TclError in CodeContext.timer_event, CodeContext.font_timer_event and other callbacks.

2. Save results of after() and after_idle() calls and pass them to after_cancel() before destroying widgets. Maybe this should be done automatically in Tkinter.
msg265602 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-05-15 09:21
Proposed patch fixes the issue for all Tkinter applications.
msg265644 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-05-15 19:11
This is a partial fix.  Run python, import idlelib.idle, open idlelib.run, close editor, close shell, get clean >>> prompt in python. No 'bgerror ...'.  This is probably enough to justify patch.  (Directly opening and closing IDLE continues to work fine, as before.)

Close shell first, then editor, get >>> + similar 'invalid ... invalid ...' message as reported above.
msg268407 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-06-12 23:53
I applied to 2.7 after changing the file name.  I still get
F:\Python\dev\27>pcbuild\python_d.exe -m test.regrtest -ugui test_idle
[1/1] test_idle
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"

This is the only instance left in all three versions after using root.update_idletasks where needed.  It does not occur when running the particular test file by itself.  Mark Roseman say something similar running test.test_idle with 3.6 on Mac.  I do not running same on Windows.
msg268410 - (view) Author: Mark Roseman (markroseman) * Date: 2016-06-13 00:12
tried this patch on 3.6 as per terry's previous msg; still getting same error in idle test suite on os x
History
Date User Action Args
2016-06-13 00:12:12markrosemansetnosy: + markroseman
messages: + msg268410
2016-06-12 23:53:48terry.reedysetmessages: + msg268407
2016-05-15 19:11:34terry.reedysetmessages: + msg265644
2016-05-15 09:21:55serhiy.storchakasetfiles: + tkinter_destroy_cancel_handlers.patch

components: + Tkinter, - IDLE
versions: + Python 2.7
keywords: + patch
type: behavior
messages: + msg265602
stage: test needed -> patch review
2016-05-15 08:30:46serhiy.storchakasetmessages: + msg265594
2016-05-15 05:25:46terry.reedycreate