bpo-31095: fix potential crash during GC (GH-3195)

(cherry picked from commit a6296d34a478b4f697ea9db798146195075d496c)
14 files changed