From 3c2bf80fb10384b17f2e3f622a0506e5d2969fe6 Mon Sep 17 00:00:00 2001 From: Oliver Burnett-Hall Date: Sun, 6 Oct 2024 18:22:37 +0100 Subject: [PATCH] Move the auto-router path timeout check to an inner loop The auto-router's path timeout is supposed to stop too much time being spent on building up the set of possible routes for trains. It was being checked once in each iteration of possible starting nodes for routes. However, in particularly degenerate cases this meant that far too long was elapsing between timeout checks: issue tobymao#11252 had more than ten hours pass before the first and second timeout checks. This moves the timeout check to an inner loop, so it is checked more frequently. --- lib/engine/auto_router.rb | 14 +++++++------- lib/engine/game_error.rb | 5 +++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/engine/auto_router.rb b/lib/engine/auto_router.rb index 244a83df95..2bd4e8cf93 100644 --- a/lib/engine/auto_router.rb +++ b/lib/engine/auto_router.rb @@ -64,16 +64,12 @@ def compute_for_train_group(trains, corporation, **opts) @next_hexside_bit = 0 nodes.each do |node| - if Time.now - now > path_timeout - LOGGER.debug('Path timeout reached') - path_walk_timed_out = true - break - else - LOGGER.debug { "Path search: #{nodes.index(node)} / #{nodes.size} - paths starting from #{node.hex.name}" } - end + LOGGER.debug { "Path search: #{nodes.index(node)} / #{nodes.size} - paths starting from #{node.hex.name}" } walk_corporation = graph.no_blocking? ? nil : corporation node.walk(corporation: walk_corporation, skip_paths: skip_paths) do |_, vp| + raise Timeout if Time.now - now > path_timeout + paths = vp.keys chains = [] chain = [] @@ -163,6 +159,10 @@ def compute_for_train_group(trains, corporation, **opts) next :abort if path_abort.empty? end + rescue Timeout + LOGGER.debug('Path timeout reached') + path_walk_timed_out = true + break end # Check that there are no duplicate hexside bits (algorithm error) diff --git a/lib/engine/game_error.rb b/lib/engine/game_error.rb index c62fd1becb..7eaf0aae17 100644 --- a/lib/engine/game_error.rb +++ b/lib/engine/game_error.rb @@ -17,4 +17,9 @@ class RouteTooLong < GameError class ReusesCity < GameError end + + # errors generated by the auto_router + + class Timeout < GameError + end end