#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
diff --git a/Modules/threadmodule.c b/Modules/threadmodule.c
index ad84a1e..f6d7ee4 100644
--- a/Modules/threadmodule.c
+++ b/Modules/threadmodule.c
@@ -244,7 +244,6 @@
 static int
 local_clear(localobject *self)
 {
-	Py_CLEAR(self->key);
 	Py_CLEAR(self->args);
 	Py_CLEAR(self->kw);
 	Py_CLEAR(self->dict);
@@ -266,6 +265,7 @@
 				PyDict_DelItem(tstate->dict, self->key);
 	}
 
+	Py_XDECREF(self->key);
 	local_clear(self);
 	Py_TYPE(self)->tp_free((PyObject*)self);
 }