At the beginning, where the hidden passage is explained, isn't there a "race condition", where Ali Baba would eventually (across 40 repetitions) run into the thief? The thief picks one branch, Ali Baba the other. The thief uses the passage, then walks right into Ali Baba?
I don't understand how the thief can be sure to never walk into Ali Baba coming down the other path at some time.
What am I missing?