leak: fix racy test

Based largely on Mathieu Lonjaret's Gerrit CL 2267.

Change-Id: I14634b6fc892e84cbdc718a9bb5a1aafca1ec4e9