diff --git a/Lib/lib-tk/test/test_tkinter/test_threads.py b/Lib/lib-tk/test/test_tkinter/test_threads.py new file mode 100644 index 00000000000000..92667da2a4320f --- /dev/null +++ b/Lib/lib-tk/test/test_tkinter/test_threads.py @@ -0,0 +1,117 @@ +from __future__ import print_function +import Tkinter as tkinter +import random +import string +import sys +import threading +import time + +import unittest + + +class TestThreads(unittest.TestCase): + def setUp(self): + self.stderr = "" + + def test_threads(self): + import subprocess + p = subprocess.Popen([sys.executable, __file__, "run"], + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + + rt = threading.Thread(target=reader_thread, args=(p.stdout, self.stderr)) + rt.start() + + try: + t = time.time() + # Test code is designed to complete in a few seconds + while time.time() - t < 10: + if p.poll() is not None: break + time.sleep(0.2) + else: + p.kill() + setattr(self, 'killed', True) + finally: + p.stdout.close() + rt.join() + rc = p.returncode + if hasattr(self, 'killed'): + self.fail("Test code hang. Stderr: " + repr(self.stderr)) + self.assertTrue(rc == 0, + "Nonzero exit status: " + str(rc) + "; stderr:" + repr(self.stderr)) + self.assertTrue(len(self.stderr) == 0, "stderr: " + repr(self.stderr)) + + +def reader_thread(stream, output): + while True: + s = stream.read() + if not s: break + output += s + + +running = True + + +class EventThread(threading.Thread): + def __init__(self, target): + threading.Thread.__init__(self) + self.target = target + + def run(self): + while running: + time.sleep(0.02) + c = random.choice(string.ascii_letters) + self.target.event_generate(c) + + +class Main(object): + def __init__(self): + self.root = tkinter.Tk() + self.root.bind('', dummy_handler) + self.threads = [] + + self.t_cleanup = threading.Thread(target=self.tf_stop) + + def go(self): + self.root.after(0, self.add_threads) + self.root.after(500, self.stop) + self.root.protocol("WM_DELETE_WINDOW", self.stop) + self.root.mainloop() + self.t_cleanup.join() + + def stop(self): + self.t_cleanup.start() + + def tf_stop(self): + global running + running = False + for t in self.threads: t.join() + self.root.after(0, self.root.destroy) + + def add_threads(self): + for _ in range(20): + t = EventThread(self.root) + self.threads.append(t) + t.start() + + +def dummy_handler(event): + pass + + +tests_gui = (TestThreads,) + +if __name__ == '__main__': + import os + + if sys.argv[1:] == ['run']: + if os.name == 'nt': + import ctypes + + # the bug causes crashes + ctypes.windll.kernel32.SetErrorMode(3) + Main().go() + else: + from test.support import run_unittest + + run_unittest(*tests_gui) diff --git a/Misc/NEWS.d/next/Library/2018-04-14-01-26-10.bpo-33257.i32qOW.rst b/Misc/NEWS.d/next/Library/2018-04-14-01-26-10.bpo-33257.i32qOW.rst new file mode 100644 index 00000000000000..b061786ae808af --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-04-14-01-26-10.bpo-33257.i32qOW.rst @@ -0,0 +1 @@ +Fixed race conditions in Tkinter with nonthreaded Tcl diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index 444c268c0b4738..9764693738610c 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -151,35 +151,38 @@ Copyright (C) 1994 Steen Lumholt. interpreter lock was used for this. However, this causes problems when other Python threads need to run while Tcl is blocked waiting for events. - To solve this problem, a separate lock for Tcl is introduced. Holding it - is incompatible with holding Python's interpreter lock. The following four - macros manipulate both locks together. + + To solve this problem, a separate lock for Tcl is introduced. + We normally treat holding it as incompatible with holding Python's + interpreter lock. The following macros manipulate both locks together. ENTER_TCL and LEAVE_TCL are brackets, just like Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS. They should be used whenever a call into Tcl is made that could call an event handler, or otherwise affect the state of a Tcl interpreter. These assume that the surrounding code has the Python - interpreter lock; inside the brackets, the Python interpreter lock has been - released and the lock for Tcl has been acquired. + interpreter lock: ENTER_TCL releases the Python lock, then acquires the Tcl + lock, and LEAVE_TCL does the reverse. Sometimes, it is necessary to have both the Python lock and the Tcl lock. - (For example, when transferring data from the Tcl interpreter result to a - Python string object.) This can be done by using different macros to close - the ENTER_TCL block: ENTER_OVERLAP reacquires the Python lock (and restores - the thread state) but doesn't release the Tcl lock; LEAVE_OVERLAP_TCL - releases the Tcl lock. + (For example, when transferring data between the Tcl interpreter and/or + objects and Python objects.) To avoid deadlocks, when acquiring, we always + acquire the Tcl lock first, then the Python lock. The additional macros for + finer lock control are: ENTER_OVERLAP acquires the Python lock (and restores + the thread state) when already holding the Tcl lock; LEAVE_OVERLAP releases + the Python lock and keeps the Tcl lock; and LEAVE_OVERLAP_TCL releases the + Tcl lock and keeps the Python lock. By contrast, ENTER_PYTHON and LEAVE_PYTHON are used in Tcl event - handlers when the handler needs to use Python. Such event handlers are - entered while the lock for Tcl is held; the event handler presumably needs - to use Python. ENTER_PYTHON releases the lock for Tcl and acquires - the Python interpreter lock, restoring the appropriate thread state, and - LEAVE_PYTHON releases the Python interpreter lock and re-acquires the lock - for Tcl. It is okay for ENTER_TCL/LEAVE_TCL pairs to be contained inside - the code between ENTER_PYTHON and LEAVE_PYTHON. - - These locks expand to several statements and brackets; they should not be - used in branches of if statements and the like. + handlers when the handler needs to use Python. Such event handlers + are entered while the lock for Tcl is held; the event handler + presumably needs to use Python. ENTER_PYTHON acquires the Python + interpreter lock, restoring the appropriate thread state, and + LEAVE_PYTHON releases the Python interpreter lock. Tcl lock is not + released because that would lead to a race condition: another, + unrelated call that uses these macros may start (but not finish) in + the meantime, and a wrong Tcl stack frame will be on top when the original + handler regains the lock. Since a handler's Python payload may need to make + Tkinter calls itself, the Tcl lock is made reentrant. If Tcl is threaded, this approach won't work anymore. The Tcl interpreter is only valid in the thread that created it, and all Tk activity must happen in this @@ -197,6 +200,10 @@ Copyright (C) 1994 Steen Lumholt. */ static PyThread_type_lock tcl_lock = 0; +/* Only the thread holding the lock can modify these */ +static unsigned long tcl_lock_thread_ident = 0; +static unsigned int tcl_lock_reentry_count = 0; + #ifdef TCL_THREADS static Tcl_ThreadDataKey state_key; @@ -206,26 +213,66 @@ typedef PyThreadState *ThreadSpecificData; static PyThreadState *tcl_tstate = NULL; #endif + +#define ACQUIRE_TCL_LOCK \ +if (tcl_lock) { \ + if (tcl_lock_thread_ident == PyThread_get_thread_ident()) { \ + tcl_lock_reentry_count++; \ + if(!tcl_lock_reentry_count) \ + Py_FatalError("Tcl lock reentry count overflow"); \ + } else { \ + PyThread_acquire_lock(tcl_lock, 1); \ + tcl_lock_thread_ident = PyThread_get_thread_ident(); \ + } \ +} + +#define RELEASE_TCL_LOCK \ +if (tcl_lock) { \ + if (tcl_lock_reentry_count) { \ + tcl_lock_reentry_count--; \ + } else { \ + tcl_lock_thread_ident = 0; \ + PyThread_release_lock(tcl_lock); \ + } \ +} + + #define ENTER_TCL \ - { PyThreadState *tstate = PyThreadState_Get(); Py_BEGIN_ALLOW_THREADS \ - if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); tcl_tstate = tstate; + { PyThreadState *tstate = PyThreadState_Get(); \ + ENTER_TCL_CUSTOM_TSTATE(tstate) + +#define ENTER_TCL_CUSTOM_TSTATE(tstate) \ + Py_BEGIN_ALLOW_THREADS \ + ACQUIRE_TCL_LOCK \ + tcl_tstate = tstate; #define LEAVE_TCL \ - tcl_tstate = NULL; if(tcl_lock)PyThread_release_lock(tcl_lock); Py_END_ALLOW_THREADS} + tcl_tstate = NULL; \ + RELEASE_TCL_LOCK; Py_END_ALLOW_THREADS} + +#define LEAVE_TCL_WITH_BUSYWAIT(result) \ + tcl_tstate = NULL; \ + RELEASE_TCL_LOCK \ + if (result == 0) \ + Sleep(Tkinter_busywaitinterval); \ + Py_END_ALLOW_THREADS + #define ENTER_OVERLAP \ Py_END_ALLOW_THREADS +#define LEAVE_OVERLAP \ + Py_BEGIN_ALLOW_THREADS + #define LEAVE_OVERLAP_TCL \ - tcl_tstate = NULL; if(tcl_lock)PyThread_release_lock(tcl_lock); } + tcl_tstate = NULL; RELEASE_TCL_LOCK } #define ENTER_PYTHON \ - { PyThreadState *tstate = tcl_tstate; tcl_tstate = NULL; \ - if(tcl_lock)PyThread_release_lock(tcl_lock); PyEval_RestoreThread((tstate)); } +{ PyThreadState *tstate = tcl_tstate; tcl_tstate = NULL; PyEval_RestoreThread((tstate)); } #define LEAVE_PYTHON \ - { PyThreadState *tstate = PyEval_SaveThread(); \ - if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); tcl_tstate = tstate; } +{ PyThreadState *tstate = PyEval_SaveThread(); tcl_tstate = tstate; } + #define CHECK_TCL_APPARTMENT \ if (((TkappObject *)self)->threaded && \ @@ -237,8 +284,11 @@ static PyThreadState *tcl_tstate = NULL; #else #define ENTER_TCL +#define ENTER_TCL_CUSTOM_TSTATE(tstate) #define LEAVE_TCL +#define LEAVE_TCL_WITH_BUSYWAIT(result) #define ENTER_OVERLAP +#define LEAVE_OVERLAP #define LEAVE_OVERLAP_TCL #define ENTER_PYTHON #define LEAVE_PYTHON @@ -1644,24 +1694,32 @@ Tkapp_Call(PyObject *selfptr, PyObject *args) #endif { + ENTER_TCL + ENTER_OVERLAP + objv = Tkapp_CallArgs(args, objStore, &objc); - if (!objv) - return NULL; - ENTER_TCL + if (objv) { - i = Tcl_EvalObjv(self->interp, objc, objv, flags); + LEAVE_OVERLAP - ENTER_OVERLAP + i = Tcl_EvalObjv(self->interp, objc, objv, flags); - if (i == TCL_ERROR) - Tkinter_Error(selfptr); - else - res = Tkapp_CallResult(self); + ENTER_OVERLAP - LEAVE_OVERLAP_TCL + if (i == TCL_ERROR) + Tkinter_Error(selfptr); + else + res = Tkapp_CallResult(self); + + LEAVE_OVERLAP + + Tkapp_CallDeallocArgs(objv, objStore, objc); - Tkapp_CallDeallocArgs(objv, objStore, objc); + ENTER_OVERLAP + + } + LEAVE_OVERLAP_TCL } return res; } @@ -1952,19 +2010,20 @@ SetVar(PyObject *self, PyObject *args, int flags) if (!PyArg_ParseTuple(args, "O&O:setvar", varname_converter, &name1, &newValue)) return NULL; - /* XXX Acquire tcl lock??? */ - newval = AsObj(newValue); - if (newval == NULL) - return NULL; ENTER_TCL - ok = Tcl_SetVar2Ex(Tkapp_Interp(self), name1, NULL, - newval, flags); ENTER_OVERLAP - if (!ok) - Tkinter_Error(self); - else { - res = Py_None; - Py_INCREF(res); + newval = AsObj(newValue); + if (newval) { + LEAVE_OVERLAP + ok = Tcl_SetVar2Ex(Tkapp_Interp(self), name1, NULL, + newval, flags); + ENTER_OVERLAP + if (!ok) + Tkinter_Error(self); + else { + res = Py_None; + Py_INCREF(res); + } } LEAVE_OVERLAP_TCL break; @@ -1974,16 +2033,20 @@ SetVar(PyObject *self, PyObject *args, int flags) return NULL; CHECK_STRING_LENGTH(name1); CHECK_STRING_LENGTH(name2); - /* XXX must hold tcl lock already??? */ - newval = AsObj(newValue); ENTER_TCL - ok = Tcl_SetVar2Ex(Tkapp_Interp(self), name1, name2, newval, flags); ENTER_OVERLAP - if (!ok) - Tkinter_Error(self); - else { - res = Py_None; - Py_INCREF(res); + newval = AsObj(newValue); + if (newval) { + LEAVE_OVERLAP + ok = Tcl_SetVar2Ex(Tkapp_Interp(self), name1, name2, + newval, flags); + ENTER_OVERLAP + if (!ok) + Tkinter_Error(self); + else { + res = Py_None; + Py_INCREF(res); + } } LEAVE_OVERLAP_TCL break; @@ -2433,7 +2496,7 @@ typedef struct { } PythonCmd_ClientData; static int -PythonCmd_Error(Tcl_Interp *interp) +PythonCmd_Error(void) { errorInCmd = 1; PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd); @@ -2461,25 +2524,25 @@ PythonCmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[]) /* Create argument list (argv1, ..., argvN) */ if (!(arg = PyTuple_New(argc - 1))) - return PythonCmd_Error(interp); + return PythonCmd_Error(); for (i = 0; i < (argc - 1); i++) { PyObject *s = fromTclString(argv[i + 1]); if (!s || PyTuple_SetItem(arg, i, s)) { Py_DECREF(arg); - return PythonCmd_Error(interp); + return PythonCmd_Error(); } } res = PyEval_CallObject(func, arg); Py_DECREF(arg); if (res == NULL) - return PythonCmd_Error(interp); + return PythonCmd_Error(); obj_res = AsObj(res); if (obj_res == NULL) { Py_DECREF(res); - return PythonCmd_Error(interp); + return PythonCmd_Error(); } else { Tcl_SetObjResult(interp, obj_res); @@ -3049,15 +3112,9 @@ Tkapp_MainLoop(PyObject *selfptr, PyObject *args) LEAVE_TCL } else { - Py_BEGIN_ALLOW_THREADS - if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); - tcl_tstate = tstate; + ENTER_TCL_CUSTOM_TSTATE(tstate) result = Tcl_DoOneEvent(TCL_DONT_WAIT); - tcl_tstate = NULL; - if(tcl_lock)PyThread_release_lock(tcl_lock); - if (result == 0) - Sleep(Tkinter_busywaitinterval); - Py_END_ALLOW_THREADS + LEAVE_TCL_WITH_BUSYWAIT(result) } #else result = Tcl_DoOneEvent(0); @@ -3563,17 +3620,11 @@ EventHook(void) } #endif #if defined(WITH_THREAD) || defined(MS_WINDOWS) - Py_BEGIN_ALLOW_THREADS - if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); - tcl_tstate = event_tstate; + ENTER_TCL_CUSTOM_TSTATE(event_tstate) result = Tcl_DoOneEvent(TCL_DONT_WAIT); - tcl_tstate = NULL; - if(tcl_lock)PyThread_release_lock(tcl_lock); - if (result == 0) - Sleep(Tkinter_busywaitinterval); - Py_END_ALLOW_THREADS + LEAVE_TCL_WITH_BUSYWAIT(result) #else result = Tcl_DoOneEvent(0); #endif