prevent writing to stderr from messing up the exception state (closes #14474)
diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c
index ea038de..5f76a7b 100644
--- a/Modules/_threadmodule.c
+++ b/Modules/_threadmodule.c
@@ -994,14 +994,17 @@
             PyErr_Clear();
         else {
             PyObject *file;
+            PyObject *exc, *value, *tb;
             PySys_WriteStderr(
                 "Unhandled exception in thread started by ");
+            PyErr_Fetch(&exc, &value, &tb);
             file = PySys_GetObject("stderr");
             if (file != NULL && file != Py_None)
                 PyFile_WriteObject(boot->func, file, 0);
             else
                 PyObject_Print(boot->func, stderr, 0);
             PySys_WriteStderr("\n");
+            PyErr_Restore(exc, value, tb);
             PyErr_PrintEx(0);
         }
     }