diff --git a/flatland/envs/generators.py b/flatland/envs/generators.py
index 127e8790c6ceeac4a295d9e1287c23a3eac8382b..bfcfdf71ed5a9c608e588f0dbd759921b7450c43 100644
--- a/flatland/envs/generators.py
+++ b/flatland/envs/generators.py
@@ -923,6 +923,10 @@ def realistic_rail_generator(nr_start_goal=1, seed=0, add_max_dead_end=4, goals_
             agents_targets.pop(t)
             agents_target.append((tp[0], tp[1]))
 
+            if len(agents_position) == 0:
+                print("no more position left")
+                break
+
             sel = np.random.choice(range(len(agents_positions)))
             # backward
             p = agents_positions[sel]