|
8 | 8 | #include <Python.h> |
9 | 9 | #include "pycore_initconfig.h" // _PyConfig_InitCompatConfig() |
10 | 10 | #include "pycore_runtime.h" // _PyRuntime |
| 11 | +#include "pycore_lock.h" // PyEvent |
11 | 12 | #include "pycore_pythread.h" // PyThread_start_joinable_thread() |
12 | 13 | #include "pycore_import.h" // _PyImport_FrozenBootstrap |
13 | 14 | #include <inttypes.h> |
@@ -2312,6 +2313,32 @@ test_get_incomplete_frame(void) |
2312 | 2313 | return result; |
2313 | 2314 | } |
2314 | 2315 |
|
| 2316 | +static void |
| 2317 | +do_gilstate_ensure(void *event_ptr) |
| 2318 | +{ |
| 2319 | + PyEvent *event = (PyEvent *)event_ptr; |
| 2320 | + // Signal to the calling thread that we've started |
| 2321 | + _PyEvent_Notify(event); |
| 2322 | + PyGILState_Ensure(); // This should hang |
| 2323 | + assert(NULL); |
| 2324 | +} |
| 2325 | + |
| 2326 | +static int |
| 2327 | +test_gilstate_after_finalization(void) |
| 2328 | +{ |
| 2329 | + _testembed_initialize(); |
| 2330 | + Py_Finalize(); |
| 2331 | + PyThread_handle_t handle; |
| 2332 | + PyThread_ident_t ident; |
| 2333 | + PyEvent event = {0}; |
| 2334 | + if (PyThread_start_joinable_thread(&do_gilstate_ensure, &event, &ident, &handle) < 0) { |
| 2335 | + return -1; |
| 2336 | + } |
| 2337 | + PyEvent_Wait(&event); |
| 2338 | + // We're now pretty confident that the thread went for |
| 2339 | + // PyGILState_Ensure(), but that means it got hung. |
| 2340 | + return PyThread_detach_thread(handle); |
| 2341 | +} |
2315 | 2342 |
|
2316 | 2343 | /* ********************************************************* |
2317 | 2344 | * List of test cases and the function that implements it. |
@@ -2402,7 +2429,7 @@ static struct TestCase TestCases[] = { |
2402 | 2429 | {"test_frozenmain", test_frozenmain}, |
2403 | 2430 | #endif |
2404 | 2431 | {"test_get_incomplete_frame", test_get_incomplete_frame}, |
2405 | | - |
| 2432 | + {"test_gilstate_after_finalization", test_gilstate_after_finalization}, |
2406 | 2433 | {NULL, NULL} |
2407 | 2434 | }; |
2408 | 2435 |
|
|
0 commit comments