Fix potential race in test cleanup