bpo-30805: Avoid race condition with debug logging (GH-7545)

Supersedes https://github.com/python/cpython/pull/2490
2 files changed