1) fix spurious wakeup case
2) fix possible infinity wait