#6990: clear threading.local's key only after its thread state is removed:
fixes local subclasses leaving old state around after a ref cycle GC which
could be recycled by new locals
(backported from r75123)
diff --git a/Modules/threadmodule.c b/Modules/threadmodule.c
index 2726045..c1df6c1 100644
--- a/Modules/threadmodule.c
+++ b/Modules/threadmodule.c
@@ -228,7 +228,6 @@
 static int
 local_clear(localobject *self)
 {
-	Py_CLEAR(self->key);
 	Py_CLEAR(self->args);
 	Py_CLEAR(self->kw);
 	Py_CLEAR(self->dict);
@@ -250,6 +249,7 @@
 				PyDict_DelItem(tstate->dict, self->key);
 	}
 
+	Py_XDECREF(self->key);
 	local_clear(self);
 	self->ob_type->tp_free((PyObject*)self);
 }