Issue #7544: Preallocate thread memory before creating the thread to avoid a
fatal error in low memory condition.
diff --git a/Include/pystate.h b/Include/pystate.h
index 4919d99..b9fe61e 100644
--- a/Include/pystate.h
+++ b/Include/pystate.h
@@ -105,6 +105,8 @@
 PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);
 
 PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *);
+PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
+PyAPI_FUNC(void) _PyThreadState_Init(PyThreadState *);
 PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *);
 PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *);
 #ifdef WITH_THREAD
diff --git a/Misc/NEWS b/Misc/NEWS
index 5636528..cfdd364 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@
 Core and Builtins
 -----------------
 
+- Issue #7544: Preallocate thread memory before creating the thread to avoid
+  a fatal error in low memory condition.
+
 - Issue #7820: The parser tokenizer restores all bytes in the right if
   the BOM check fails.
 
diff --git a/Modules/threadmodule.c b/Modules/threadmodule.c
index b65360e..6494f49 100644
--- a/Modules/threadmodule.c
+++ b/Modules/threadmodule.c
@@ -428,6 +428,7 @@
 	PyObject *func;
 	PyObject *args;
 	PyObject *keyw;
+	PyThreadState *tstate;
 };
 
 static void
@@ -437,8 +438,9 @@
 	PyThreadState *tstate;
 	PyObject *res;
 
-	tstate = PyThreadState_New(boot->interp);
-
+	tstate = boot->tstate;
+	tstate->thread_id = PyThread_get_thread_ident();
+	_PyThreadState_Init(tstate);
 	PyEval_AcquireThread(tstate);
 	nb_threads++;
 	res = PyEval_CallObjectWithKeywords(
@@ -503,6 +505,11 @@
 	boot->func = func;
 	boot->args = args;
 	boot->keyw = keyw;
+	boot->tstate = _PyThreadState_Prealloc(boot->interp);
+	if (boot->tstate == NULL) {
+		PyMem_DEL(boot);
+		return PyErr_NoMemory();
+	}
 	Py_INCREF(func);
 	Py_INCREF(args);
 	Py_XINCREF(keyw);
@@ -513,6 +520,7 @@
 		Py_DECREF(func);
 		Py_DECREF(args);
 		Py_XDECREF(keyw);
+		PyThreadState_Clear(boot->tstate);
 		PyMem_DEL(boot);
 		return NULL;
 	}
diff --git a/Python/pystate.c b/Python/pystate.c
index da417c1..343a97b 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -154,8 +154,8 @@
 	return self->frame;
 }
 
-PyThreadState *
-PyThreadState_New(PyInterpreterState *interp)
+static PyThreadState *
+new_threadstate(PyInterpreterState *interp, int init)
 {
 	PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
 
@@ -193,9 +193,8 @@
 		tstate->c_profileobj = NULL;
 		tstate->c_traceobj = NULL;
 
-#ifdef WITH_THREAD
-		_PyGILState_NoteThreadState(tstate);
-#endif
+		if (init)
+			_PyThreadState_Init(tstate);
 
 		HEAD_LOCK();
 		tstate->next = interp->tstate_head;
@@ -206,6 +205,25 @@
 	return tstate;
 }
 
+PyThreadState *
+PyThreadState_New(PyInterpreterState *interp)
+{
+	return new_threadstate(interp, 1);
+}
+
+PyThreadState *
+_PyThreadState_Prealloc(PyInterpreterState *interp)
+{
+	return new_threadstate(interp, 0);
+}
+
+void
+_PyThreadState_Init(PyThreadState *tstate)
+{
+#ifdef WITH_THREAD
+	_PyGILState_NoteThreadState(tstate);
+#endif
+}
 
 void
 PyThreadState_Clear(PyThreadState *tstate)