The interaction between defers and t.Cleanup can be delicate. For this case, which regularly flakes in CI, be explicit: Defer the closes and waits before making any attempt to leaktest.