fix some semaphore wait semantics (race condition deadlock and error checking)