Skip to content

Commit

Permalink
Move the auto-router path timeout check to an inner loop
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ollybh committed Oct 6, 2024
1 parent 78af85e commit 82f06c5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/engine/auto_router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 PathTimeout if Time.now - now > path_timeout

paths = vp.keys
chains = []
chain = []
Expand Down Expand Up @@ -163,6 +159,10 @@ def compute_for_train_group(trains, corporation, **opts)

next :abort if path_abort.empty?
end
rescue PathTimeout
LOGGER.debug('Path timeout reached')
path_walk_timed_out = true
break
end

# Check that there are no duplicate hexside bits (algorithm error)
Expand Down
5 changes: 5 additions & 0 deletions lib/engine/game_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ class RouteTooLong < GameError

class ReusesCity < GameError
end

# errors generated by the auto_router

class PathTimeout < GameError
end
end

0 comments on commit 82f06c5

Please sign in to comment.