Fix race condition where script was too slow to terminate in tests