[WIP] bpo-40091: Fix a crash in logging after fork by vstinner · Pull Request #19196 · python/cpython

Oh. A colleague working on the glibc tells me that it's unsafe to release the memory of the old lock without destroying it: "Putting two mutexes at the same address could lead to problems".

We should either leak memory or destroy the old lock.

To destroy the old lock, we need to first unlock it if it's lock. pthread_mutex_trylock() can be used to check if it's locked or not. If it's lock, call pthread_mutex_unlock(). Then pthread_mutex_destroy() and pthread_mutex_init().