@@ -3310,6 +3310,50 @@ test_critical_sections(PyObject *module, PyObject *Py_UNUSED(args))
33103310 Py_RETURN_NONE ;
33113311}
33123312
3313+
3314+ // Used by `finalize_thread_hang`.
3315+ #ifdef _POSIX_THREADS
3316+ static void finalize_thread_hang_cleanup_callback (void * Py_UNUSED (arg )) {
3317+ // Should not reach here.
3318+ Py_FatalError ("pthread thread termination was triggered unexpectedly" );
3319+ }
3320+ #endif
3321+
3322+ // Tests that finalization does not trigger pthread cleanup.
3323+ //
3324+ // Must be called with a single nullary callable function that should block
3325+ // (with GIL released) until finalization is in progress.
3326+ static PyObject *
3327+ finalize_thread_hang (PyObject * self , PyObject * callback )
3328+ {
3329+ // WASI builds some pthread stuff but doesn't have these APIs today?
3330+ #if defined(_POSIX_THREADS ) && !defined(__wasi__ )
3331+ pthread_cleanup_push (finalize_thread_hang_cleanup_callback , NULL );
3332+ #endif
3333+ PyObject_CallNoArgs (callback );
3334+ // Should not reach here.
3335+ Py_FatalError ("thread unexpectedly did not hang" );
3336+ #if defined(_POSIX_THREADS ) && !defined(__wasi__ )
3337+ pthread_cleanup_pop (0 );
3338+ #endif
3339+ Py_RETURN_NONE ;
3340+ }
3341+
3342+
3343+ static PyObject *
3344+ type_freeze (PyObject * module , PyObject * args )
3345+ {
3346+ PyTypeObject * type ;
3347+ if (!PyArg_ParseTuple (args , "O!" , & PyType_Type , & type )) {
3348+ return NULL ;
3349+ }
3350+ if (PyType_Freeze (type ) < 0 ) {
3351+ return NULL ;
3352+ }
3353+ Py_RETURN_NONE ;
3354+ }
3355+
3356+
33133357static PyMethodDef TestMethods [] = {
33143358 {"set_errno" , set_errno , METH_VARARGS },
33153359 {"test_config" , test_config , METH_NOARGS },
@@ -3449,6 +3493,8 @@ static PyMethodDef TestMethods[] = {
34493493 {"test_weakref_capi" , test_weakref_capi , METH_NOARGS },
34503494 {"function_set_warning" , function_set_warning , METH_NOARGS },
34513495 {"test_critical_sections" , test_critical_sections , METH_NOARGS },
3496+ {"finalize_thread_hang" , finalize_thread_hang , METH_O , NULL },
3497+ {"type_freeze" , type_freeze , METH_VARARGS },
34523498 {NULL , NULL } /* sentinel */
34533499};
34543500
0 commit comments