diff Python/thread.c @ 48217:eefe4450f773 2.5

Backport r60148 and r65481: sanity checks to avoid infinite loops.
author Georg Brandl <georg@python.org>
date Tue, 12 Aug 2008 08:46:02 +0000
parents 7c09b66f04bd
children 8d902c513ba0
line wrap: on
line diff
--- a/Python/thread.c	Tue Aug 12 08:39:33 2008 +0000
+++ b/Python/thread.c	Tue Aug 12 08:46:02 2008 +0000
@@ -264,15 +264,25 @@
 static struct key *
 find_key(int key, void *value)
 {
-	struct key *p;
+	struct key *p, *prev_p;
 	long id = PyThread_get_thread_ident();
 
 	if (!keymutex)
 		return NULL;
 	PyThread_acquire_lock(keymutex, 1);
+	prev_p = NULL;
 	for (p = keyhead; p != NULL; p = p->next) {
 		if (p->id == id && p->key == key)
 			goto Done;
+		/* Sanity check.  These states should never happen but if
+		 * they do we must abort.  Otherwise we'll end up spinning in
+		 * in a tight loop with the lock held.  A similar check is done
+		 * in pystate.c tstate_delete_common().  */
+		if (p == prev_p)
+			Py_FatalError("tls find_key: small circular list(!)");
+		prev_p = p;
+		if (p->next == keyhead)
+			Py_FatalError("tls find_key: circular list(!)");
 	}
 	if (value == NULL) {
 		assert(p == NULL);