bpo-31095: Fix potential crash during GC (GH-3197)

(cherry picked from commit a6296d34a478b4f697ea9db798146195075d496c)
10 files changed