[New-bugs-announce] [issue33257] Race conditions in Tkinter with non-threaded Tcl

Ivan Pozdeev report at bugs.python.org
Tue Apr 10 12:41:09 EDT 2018


New submission from Ivan Pozdeev <ivan_pozdeev at mail.ru>:

(Marked only 2.7 as affected but this would affect any branch if built with nonthreaded Tcl.)

When running the attached TkinterCrash2-2.py repeatedly with 2.7.14 and 2.7 head, win7 x64, two kinds of errors pop up randomly:

1. Crashes and freezes.
2. Exceptions on the console like:

Exception in thread Thread-14:
Traceback (most recent call last):
  File "C:\Users\Sasha\Documents\cpython\lib\threading.py", line 801, in __boots
trap_inner
    self.run()
  File "../tkt/TkinterCrash2-2.py", line 50, in run
    self.deliverToqueue((self.target, z, y))
  File "../tkt/TkinterCrash2-2.py", line 133, in arrival_122
    new_yz[1])
  File "C:\Users\Sasha\Documents\cpython\lib\lib-tk\Tkinter.py", line 2328, in create_line
    return self._create('line', args, kw)
  File "C:\Users\Sasha\Documents\cpython\lib\lib-tk\Tkinter.py", line 2310, in _create
    *(args + self._options(cnf, kw))))
ValueError: invalid literal for int() with base 10: 'None'


Running the same code with the minimal required changes (attached as TkinterCrash3-2-2.py) under 3.6 (same platform) goes without any errors.


Diagnostics showed:

1. Under debug Python, a crash became an MSVC double-free assertion error. The stacktrace is: Tkapp_Call->Tkapp_CallDeallocArgs->Tcl_DecrRefCount->TclFreeObj->free

2. The exceptions are caused by a `<canvas> create line` Tk call randomly returning "None" (a string) instead of an integer -- which it should never do according to its doc. Since it happens inconsistently, this also suggests a race condition.

3. In Tkapp_Call and SetVar, Tcl lock isn't held when creating/destroying Tcl objects for the call. The underlying fns modify the global state (free objects list, reference counters), so it should be held.
These two are the only such places in the code.


I've fixed this, will file the PR shortly. Holding the lock when calling Tkapp_CallDeallocArgs in Tkapp_Call eliminated the crashes and freezes, locking objects' creation in the same fn decreased the number of exceptions greatly, and locking creation in SetVar, too, eliminated them completely.


I did not check if objects created with AsObj in SetVar need to be disposed of like in Tkapp_Call.

Also have no idea how to autotest the fix. If someone does, I'm all ears.

----------
components: Tkinter
files: TkinterCrash2-2.py
messages: 315172
nosy: Ivan.Pozdeev
priority: normal
severity: normal
status: open
title: Race conditions in Tkinter with non-threaded Tcl
type: crash
versions: Python 2.7
Added file: https://bugs.python.org/file47529/TkinterCrash2-2.py

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


More information about the New-bugs-announce mailing list