Even more mysteriously, some units stop right at the exit of their factory, even if they exited in the right direction and the way is perfectly clear. It happens more when the rally point is not directed toward the factory exit (as on the following screenshot), but before taking the screenshot I tried different configuration of rally points (including shift-clicking a path for the units) and they always got stuck like that:

I noticed this especially when there are a lot of units on the map (500 just counting my own), but in this case that factory had the problem as early as I built it, if I can remember. I have the replay available (game against 3 AAI) if someone wants to get into a deeper analysis.