Fix potential race condition for port testing