subinterpreters: FIX a memory leak by JulienPalard · Pull Request #13168 · python/cpython

I'm currently having hard times writing a test for this.
Using this test script:

import gc
import sys
import _xxsubinterpreters as interpreters

gettotalrefcount = sys.gettotalrefcount

for i in range(14):
    gc.collect()
    refs_before = gettotalrefcount()
    sub = interpreters.create()
    del sub
    gc.collect()
    refs_after = gettotalrefcount()
    if i > 3:
        print(refs_before == refs_after, refs_before, refs_after)

On master it goes OK:

True 49434 49434
True 49438 49438
True 49438 49438
True 49438 49438
True 49438 49438
True 49438 49438
True 49438 49438
True 49438 49438
True 49438 49438
True 49438 49438

followed by address sanitizer reporting: SUMMARY: AddressSanitizer: 980 byte(s) leaked in 28 allocation(s).

On this PR's branch it goes well too:

True 49432 49432
True 49436 49436
True 49436 49436
True 49436 49436
True 49436 49436
True 49436 49436
True 49436 49436
True 49436 49436
True 49436 49436
True 49436 49436

But here, address sanitizer don't spot any leak.

So there's an enhancement, I still just cannot find it using code.

If I change the number of iterations in the loop from 5 to 50, I'm getting exactly 10 times more leaks reported by address sanitizer. Maybe gettotalrefcount is not the right tool here?