-
Notifications
You must be signed in to change notification settings - Fork 794
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hang instead of pthread_exit during interpreter shutdown
- Loading branch information
Ariel Ben-Yehuda
committed
Jan 26, 2025
1 parent
5c363b5
commit 346b4de
Showing
4 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#if defined(_WIN32) | ||
#include <windows.h> | ||
#include <synchapi.h> | ||
#else | ||
#include <unistd.h> | ||
#endif | ||
|
||
#if defined(PYTHON_IS_PYPY) | ||
#define gil_func_name PyPyGILState_Ensure | ||
#define wrapped_func_name PyPyGILState_Ensure_Safe | ||
#else | ||
#define gil_func_name PyGILState_Ensure | ||
#define wrapped_func_name PyGILState_Ensure_Safe | ||
#endif | ||
|
||
extern "C" { | ||
int wrapped_func_name(void); | ||
int gil_func_name(void); | ||
}; | ||
|
||
#if !defined(_WIN32) | ||
// mark the wrapped function as visibility("hidden") to avoid causing namespace pollution | ||
__attribute__((visibility("hidden"))) | ||
#endif | ||
int wrapped_func_name(void) { | ||
// Do the equivalent of https://github.com/python/cpython/issues/87135 (included | ||
// in Python 3.14) to avoid pthread_exit unwinding the current thread, which tends | ||
// to cause undefined behavior in Rust. | ||
// | ||
// Unfortunately, I don't know of a way to do a catch(...) from Rust. | ||
try { | ||
return gil_func_name(); | ||
} catch(...) { | ||
while(1) { | ||
#if defined(_WIN32) | ||
SleepEx(INFINITE, TRUE); | ||
#elif defined(__wasi__) | ||
sleep(9999999); // WASI doesn't have pause() ?! | ||
#else | ||
pause(); | ||
#endif | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters