[issue33412] Tkinter hangs if using multiple threads and event handlers

Terry J. Reedy report at bugs.python.org
Thu May 3 15:43:26 EDT 2018


Terry J. Reedy <tjreedy at udel.edu> added the comment:

>From 1994 to 2017, _tkinter.c has received a steady flow of multiple revisions each year, for 333 total, by maybe 20 people including Guido.  This makes it one of the more actively maintained files and indicates the opposite of indifference and not caring.

I tested event generation from threads on my Win 10 machine.  With installed 2.7, with non-thread tcl 8.5, the process hangs as described, after 1 key event is sent and received.  So we should document "don't do that".

With installed 64-bit 3.6 with thread-compiled tcl 8.6, I see something completely different.  The process runs for 5 seconds until the stop call.  The two threads alternate sending key events, which are all received in the main thread.  Ditto for built 32-big debug 3.6 and 3.8.  

The only problem is that the first t.join() hangs because of a thread deadlock bug.  t.join() blocks until t.run exits.  t.run does not exit until the last event_generate, with running=False, returns.  But that blocks until dummy_handler runs.  Dummy_handler does not run when the main thread is blocked by t.join.

I fixed this by not generating events when running is False.  The revised program exits properly.

    def run(self):
        tid = self.ident
        while True:
            time.sleep(0.02)
            c = random.choice(string.ascii_letters)
            print(running, "%d: sending '%s'"%(tid,c),file=sys.stderr)
            if running:
                self.target.event_generate(c)
            else:
                break

I suppose there is a teeny possibility that 'running' could be flipped between the test and the call.  Can that be prevented with a lock?

Another possibility is for stop() to change conditions so that 'self.target.event_generate(c)' fails with an exception, and change if/else to for/except.  My first try sort of works in IDLE but not the console.

----------
nosy: +serhiy.storchaka, terry.reedy

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue33412>
_______________________________________


More information about the Python-bugs-list mailing list