python / cpython

The Python programming language
https://www.python.org
Other
62.14k stars 29.86k forks source link

Tkinter wait_visibility hanging when used in thread #86657

Open 8f0e93bb-dfac-4d58-a1c8-13765aac6f99 opened 3 years ago

8f0e93bb-dfac-4d58-a1c8-13765aac6f99 commented 3 years ago
BPO 42491
Nosy @terryjreedy, @serhiy-storchaka, @E-Paine, @spcmicro
Files
  • threadTest.zip: Contains source files and screen captures
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields: ```python assignee = None closed_at = None created_at = labels = ['3.8', 'type-bug', 'expert-tkinter', '3.9', '3.10'] title = 'Tkinter wait_visibility hanging when used in thread' updated_at = user = 'https://github.com/spcmicro' ``` bugs.python.org fields: ```python activity = actor = 'spcmicro2' assignee = 'none' closed = False closed_date = None closer = None components = ['Tkinter'] creation = creator = 'spcmicro' dependencies = [] files = ['49631'] hgrepos = [] issue_num = 42491 keywords = [] message_count = 6.0 messages = ['381990', '382001', '382006', '382011', '399873', '399914'] nosy_count = 5.0 nosy_names = ['terry.reedy', 'serhiy.storchaka', 'epaine', 'spcmicro', 'spcmicro2'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = 'behavior' url = 'https://bugs.python.org/issue42491' versions = ['Python 3.8', 'Python 3.9', 'Python 3.10'] ```

    8f0e93bb-dfac-4d58-a1c8-13765aac6f99 commented 3 years ago

    I'm having a problem with an application that uses threads and TKinter. Sometimes everything works fine but there are 2 errors that I can't seem to figure out why they are happening. The first error is the GUI completely locking up with no text cursor in the textbox. The second problem is a crash after the input has been entered. I have created a bare minimum test application that demonstrates the problem. The problems are easily reproducible. I've attached a simpleteTKinter test application that I created using Page. I've also attached 3 screen captures.

    1. threadTest-OK.jpg is proper behavior
    2. threadTest-Hung.jpg shows the unresponsive application
    3. threadTest-Exception.jpg shows the exception thrown when input window is closed It looks like a race condition to me. I've tried putting in delays and using after but I can't seem to work around it. Am I doing something wrong here?
    919475b3-36a2-4cd7-997c-9c38f05f93c7 commented 3 years ago

    I agree this is a race condition, and have narrowed the issue down to wait_visibility with the following also hanging indefinitely (tested against the master branch):

    import threading
    import tkinter as tk
    def create_tp():
        t = tk.Toplevel()
        t.wait_visibility()
    root = tk.Tk()
    tk.Button(root, text="Create", command=lambda:
              threading.Thread(target=create_tp).start()).pack()
    root.mainloop()

    I agree this is a race condition (presumably between wait_visibility and window render) and am pretty sure there is nothing either us or the Tk team can do (IMO, this issue should be closed - though thank you for reporting it). Similar behaviour to issue-42142, though I don't believe threading is used there.

    Am I doing something wrong here? Using threads?! tkinter is known to not like threads and there are several bugs which can cause the Python interpreter to crash.

    8f0e93bb-dfac-4d58-a1c8-13765aac6f99 commented 3 years ago

    Thank you to epaine for the quick reply. IMHO this should be elevated and addressed. TKinter not working well with threads appears to be a well known problem that a lot of developers have encountered and have had to work around. The use of threads is not something that should be avoided, but rather used when needed in the appropriate manner. They've been around for a long time and work well in many development environments on a multitude of platforms. I think it's time to make TKinter thread safe. Just my opinion...

    919475b3-36a2-4cd7-997c-9c38f05f93c7 commented 3 years ago

    You make very good points but sadly it is not that simple. The issue is described in detail in the comment found in Modules/_tkinter.c:178. A good summary is probably the comment for Tkapp_Call (Modules/_tkinter.c:1522).

    terryjreedy commented 3 years ago

    tcl/tk can be compiled without or with thread support. For 8.5, the default was without. For 8.6, the default is with. The Windows and macOS installers include 8.6 compiled with the default. I have not previously heard of problems with thread when it is so compiled. Both of you, what tcl versions? how compiled?

    ad2095c1-954d-45e0-b614-9240e0b31992 commented 3 years ago

    This is a Tkinter problem and not a TK problem. I ended up working around the problem by forcing garbage collection before opening and after closing any Tkinter windows that are created from other Tkinter windows. Since adding this code I have not had the problem.