From a579cb4f74ea90409f621547cdf4465f9167a3d4 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Wed, 21 Oct 2020 10:23:56 -0700 Subject: [PATCH] Add a sanity check to catch live map edit bugs faster. Deleted turns aren't getting cleaned up properly. Also fix broken headless build, woops. --- headless/src/main.rs | 2 +- sim/src/mechanics/intersection.rs | 28 ++++++++++++++++++++++++++++ sim/src/sim/mod.rs | 2 ++ traffic_seitan/src/main.rs | 4 ++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/headless/src/main.rs b/headless/src/main.rs index cb3d73f563..ab5b3e6ead 100644 --- a/headless/src/main.rs +++ b/headless/src/main.rs @@ -295,7 +295,7 @@ fn handle_command( .get_unzoomed_agents(map) .into_iter() .map(|a| AgentPosition { - vehicle_type: a.vehicle_type, + vehicle_type: a.id.to_vehicle_type(), pos: a.pos.to_gps(map.get_gps_bounds()), person: a.person, }) diff --git a/sim/src/mechanics/intersection.rs b/sim/src/mechanics/intersection.rs index 90495cc047..aa7ef891a7 100644 --- a/sim/src/mechanics/intersection.rs +++ b/sim/src/mechanics/intersection.rs @@ -549,6 +549,34 @@ impl IntersectionSimState { } } } + + pub fn handle_live_edits(&self, map: &Map) { + // Just sanity check that we don't have any references to deleted turns + let mut errors = Vec::new(); + for state in self.state.values() { + for req in &state.accepted { + if map.maybe_get_t(req.turn).is_none() { + errors.push(format!("{} accepted for {}", req.agent, req.turn)); + } + } + for req in state.waiting.keys() { + if map.maybe_get_t(req.turn).is_none() { + errors.push(format!("{} waiting for {}", req.agent, req.turn)); + } + } + for req in &state.reserved { + if map.maybe_get_t(req.turn).is_none() { + errors.push(format!("{} has reserved {}", req.agent, req.turn)); + } + } + } + if !errors.is_empty() { + for x in errors { + error!("{}", x); + } + panic!("After live map edits, intersection state refers to deleted turns!"); + } + } } impl IntersectionSimState { diff --git a/sim/src/sim/mod.rs b/sim/src/sim/mod.rs index 1ae7fa72a1..d4f6209829 100644 --- a/sim/src/sim/mod.rs +++ b/sim/src/sim/mod.rs @@ -926,6 +926,8 @@ impl Sim { AgentID::BusPassenger(_, _) => unreachable!(), } } + + self.intersections.handle_live_edits(map); } fn find_trips_affected_by_live_edits(&mut self, map: &Map) -> Vec<(AgentID, TripID)> { diff --git a/traffic_seitan/src/main.rs b/traffic_seitan/src/main.rs index 687b2feb78..6f5ed84319 100644 --- a/traffic_seitan/src/main.rs +++ b/traffic_seitan/src/main.rs @@ -34,6 +34,9 @@ fn main() { if let Err(err) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { run(&mut map, &mut sim, &mut rng, &mut timer); })) { + let mut edits = map.get_edits().clone(); + edits.edits_name = "traffic_seitan_crash".to_string(); + map.must_apply_edits(edits, &mut timer); map.save_edits(); println!("Crashed at {}", sim.time()); @@ -49,6 +52,7 @@ fn run(map: &mut Map, sim: &mut Sim, rng: &mut XorShiftRng, timer: &mut Timer) { println!(""); sim.timed_step(map, edit_frequency, &mut None, timer); sim.save(); + map.save_edits(); let mut edits = map.get_edits().clone(); nuke_random_parking(map, rng, &mut edits);