Author Ivan.Pozdeev
Recipients Ivan.Pozdeev
Date 2018-04-10.16:41:08
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1523378469.47.0.682650639539.issue33257@psf.upfronthosting.co.za>
In-reply-to
Content
(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.
History
Date User Action Args
2018-04-10 16:41:10Ivan.Pozdeevsetrecipients: + Ivan.Pozdeev
2018-04-10 16:41:09Ivan.Pozdeevsetmessageid: <1523378469.47.0.682650639539.issue33257@psf.upfronthosting.co.za>
2018-04-10 16:41:09Ivan.Pozdeevlinkissue33257 messages
2018-04-10 16:41:09Ivan.Pozdeevcreate