From de90f7a94970b107d9d7f2de0b42a49390733a3c Mon Sep 17 00:00:00 2001 From: Alexey Khudyakov Date: Sun, 23 Aug 2026 15:02:39 +0300 Subject: [PATCH] Add workaround for python3.11 Without this that checks that current_thread()==main_thread() fail intermittently for python3.11 In that case thread used by runInMain has correct thread id (as returned by PyThread_get_thread_ident) but isn't considered main by python interpreter. Importing threading module fixes this. Why? It's unclear Fixes #45 --- src/Python/Internal/Eval.hs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Python/Internal/Eval.hs b/src/Python/Internal/Eval.hs index 4eeb7cb..bbed590 100644 --- a/src/Python/Internal/Eval.hs +++ b/src/Python/Internal/Eval.hs @@ -439,6 +439,17 @@ doInitializePythonIO = do goto error; }; PyConfig_Clear(&cfg); + // This is hack for python<=3.11. + // + // Somehow we may end up in stet where thread ID of main thread is not equal + // to main thread's one. Importing threading seems to fix it. However exact + // reason for such behavior is unknown + if( PY_MINOR_VERSION <= 11 ) { + PyObject *threading = PyImport_ImportModule("threading"); + if( PyErr_Occurred() ) { + PyErr_Clear(); + } + } // Release GIL so other threads may take it PyEval_SaveThread(); return 0;