Fixing race in wait_xxx