If the queue becomes empty but you have not processed all nodes (order.size < n), it means the remaining nodes form a cycle. They all have in-degree pointing at each other, so none could enter the queue.
This is how you solve Course Schedule. It is exactly the same code, printing "IMPOSSIBLE" if the size check fails. Kahn's algorithm naturally detects cycles without needing a three-state DFS tracker.