Skip to content

Commit

Permalink
fix: handle mutex poisonning
Browse files Browse the repository at this point in the history
  • Loading branch information
Leikoe committed Dec 30, 2024
1 parent 4a538be commit 4fd428b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub async fn strike_alone(world: &World, robot: &AllyRobot, ball: &Ball) {
AvoidanceMode::AvoidRobotsAndBall,
)
.await
.unwrap(); // will fail if we are against the ball
.expect("couldn't goto the ball, maybe we were against the ball ?");
robot.enable_dribbler();
select! {
_ = robot
Expand Down
26 changes: 25 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#[allow(async_fn_in_trait)]
#[deny(clippy::unwrap_used)]
pub mod actions;
pub mod controllers;
pub mod game_controller;
Expand All @@ -11,7 +12,12 @@ pub mod viewer;
pub mod vision;
pub mod world;

use std::{collections::HashMap, fmt::Debug, sync::Arc, time::Duration};
use std::{
collections::HashMap,
fmt::Debug,
sync::{Arc, LockResult},
time::Duration,
};

use controllers::RobotController;
use league_protocols::simulation_packet::RobotFeedback;
Expand All @@ -21,6 +27,24 @@ use world::{AllyRobot, RobotId, World};
pub const CONTROL_PERIOD: Duration = Duration::from_millis(10);
pub const DETECTION_SCALING_FACTOR: f64 = 1000.;

pub trait IgnoreMutexErr<T> {
fn unwrap_ignore_poison(self) -> T;
}

impl<T> IgnoreMutexErr<T> for LockResult<T> {
fn unwrap_ignore_poison(self) -> T {
match self {
Ok(r) => r,
Err(poisoned) => {
// Handle mutex poisoning
let guard = poisoned.into_inner();
println!("[WARNING] mutex was poisoned, recovering from mutex poisoning");
guard
}
}
}
}

async fn control_loop<
E: Debug,
C: RobotController<HashMap<RobotId, RobotFeedback>, E> + Send + 'static,
Expand Down
12 changes: 9 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use crabe_async::{
viewer::{self, ViewerObject},
vision::Vision,
world::{AllyRobot, EnnemyRobot, TeamColor, World},
CONTROL_PERIOD, DETECTION_SCALING_FACTOR,
IgnoreMutexErr, CONTROL_PERIOD, DETECTION_SCALING_FACTOR,
};
use std::time::Duration;
use std::time::{Duration, Instant};
use tokio::{select, time::sleep};

// #[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -330,7 +330,13 @@ async fn main() {
tokio::spawn(update_world_with_vision_forever(world.clone(), real));
let (control_loop_thread_stop_notifier, control_loop_thread_handle) =
launch_control_thread(world.clone(), controller);
sleep(CONTROL_PERIOD * 10).await; // AWAIT ROBOTS DETECTION
// AWAIT ROBOTS DETECTION
{
while world.team.lock().unwrap_ignore_poison().is_empty() {
println!("[WARNING] not detecting any ally robots yet, waiting 1s.");
sleep(Duration::from_secs(1)).await;
}
}

select! {
_ = play(world, gc) => {}
Expand Down

0 comments on commit 4fd428b

Please sign in to comment.