Backport of r64212
Issue #1683: prevent forking from interfering in threading storage.
diff --git a/Include/pythread.h b/Include/pythread.h
index f26db16..b5a6ec3 100644
--- a/Include/pythread.h
+++ b/Include/pythread.h
@@ -40,6 +40,9 @@
 PyAPI_FUNC(void *) PyThread_get_key_value(int);
 PyAPI_FUNC(void) PyThread_delete_key_value(int key);
 
+/* Cleanup after a fork */
+PyAPI_FUNC(void) PyThread_ReInitTLS(void);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/Misc/NEWS b/Misc/NEWS
index 9993666..b83c731 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@
 Core and builtins
 -----------------
 
+- Issue #1683: prevent forking from interfering in threading storage.
+
 - Issue #4597: Fixed several opcodes that weren't always propagating
   exceptions.
 
diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
index 9d223d5..352e739 100644
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -693,5 +693,6 @@
 	main_thread = PyThread_get_thread_ident();
 	main_pid = getpid();
 	_PyImport_ReInitLock();
+	PyThread_ReInitTLS();
 #endif
 }
diff --git a/Parser/intrcheck.c b/Parser/intrcheck.c
index e0f3252..1356191 100644
--- a/Parser/intrcheck.c
+++ b/Parser/intrcheck.c
@@ -2,6 +2,7 @@
 /* Check for interrupts */
 
 #include "Python.h"
+#include "pythread.h"
 
 #ifdef QUICKWIN
 
@@ -172,5 +173,6 @@
 {
 #ifdef WITH_THREAD
 	PyEval_ReInitThreads();
+	PyThread_ReInitTLS();
 #endif
 }
diff --git a/Python/thread.c b/Python/thread.c
index be4d092..9cf509c 100644
--- a/Python/thread.c
+++ b/Python/thread.c
@@ -391,4 +391,35 @@
 	PyThread_release_lock(keymutex);
 }
 
+/* Forget everything not associated with the current thread id.
+ * This function is called from PyOS_AfterFork().  It is necessary
+ * because other thread ids which were in use at the time of the fork
+ * may be reused for new threads created in the forked process.
+ */
+void
+PyThread_ReInitTLS(void)
+{
+	long id = PyThread_get_thread_ident();
+	struct key *p, **q;
+
+	if (!keymutex)
+		return;
+	
+	/* As with interpreter_lock in PyEval_ReInitThreads()
+	   we just create a new lock without freeing the old one */
+	keymutex = PyThread_allocate_lock();
+
+	/* Delete all keys which do not match the current thread id */
+	q = &keyhead;
+	while ((p = *q) != NULL) {
+		if (p->id != id) {
+			*q = p->next;
+			free((void *)p);
+			/* NB This does *not* free p->value! */
+		}
+		else
+			q = &p->next;
+	}
+}
+
 #endif /* Py_HAVE_NATIVE_TLS */