From 5e81d85d7e8eeca1b6c86ea028353d7c55361961 Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 14 Jun 2025 20:33:22 -1030 Subject: [PATCH] add note about current_thread to azalea readme --- azalea-client/src/client.rs | 2 +- azalea-client/src/plugins/interact.rs | 2 +- azalea-physics/src/travel.rs | 2 +- azalea/README.md | 6 +++++- azalea/examples/echo.rs | 2 +- azalea/examples/nearest_entity.rs | 2 +- azalea/examples/steal.rs | 2 +- azalea/examples/testbot/main.rs | 4 +--- azalea/examples/todo/craft_dig_straight_down.rs | 5 ++--- azalea/examples/todo/mine_a_chunk.rs | 2 +- azalea/examples/todo/pvp.rs | 10 +++++----- azalea/src/lib.rs | 2 +- azalea/src/swarm/mod.rs | 2 +- 13 files changed, 22 insertions(+), 21 deletions(-) diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index d9fec523..c9cc5259 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -153,7 +153,7 @@ impl Client { /// ```rust,no_run /// use azalea_client::{Account, Client}; /// - /// #[tokio::main] + /// #[tokio::main(flavor = "current_thread")] /// async fn main() -> Result<(), Box> { /// let account = Account::offline("bot"); /// let (client, rx) = Client::join(account, "localhost").await?; diff --git a/azalea-client/src/plugins/interact.rs b/azalea-client/src/plugins/interact.rs index d91597d6..da1fa78e 100644 --- a/azalea-client/src/plugins/interact.rs +++ b/azalea-client/src/plugins/interact.rs @@ -144,7 +144,7 @@ impl BlockStatePredictionHandler { .or_insert(ServerVerifiedState { seq: self.seq, block_state: old_state, - player_pos: player_pos, + player_pos, }); } diff --git a/azalea-physics/src/travel.rs b/azalea-physics/src/travel.rs index dba991d5..9af3ed27 100644 --- a/azalea-physics/src/travel.rs +++ b/azalea-physics/src/travel.rs @@ -215,7 +215,7 @@ fn travel_in_fluid( move_relative(physics, direction, speed, acceleration); move_colliding( MoverType::Own, - physics.velocity.clone(), + physics.velocity, world, &mut position, physics, diff --git a/azalea/README.md b/azalea/README.md index 26a06dde..4f7506d1 100644 --- a/azalea/README.md +++ b/azalea/README.md @@ -46,7 +46,7 @@ use std::sync::Arc; use azalea::prelude::*; use parking_lot::Mutex; -#[tokio::main] +#[tokio::main(flavor = "current_thread")] async fn main() { let account = Account::offline("bot"); // or Account::microsoft("example@example.com").await.unwrap(); @@ -110,5 +110,9 @@ If your code is simply hanging, it might be a deadlock. Enable `parking_lot`'s ` Backtraces are also useful, though they're sometimes hard to read and don't always contain the actual location of the error. Run your code with `RUST_BACKTRACE=1` to enable full backtraces. If it's very long, often searching for the keyword "azalea" will help you filter out unrelated things and find the actual source of the issue. +# Using a single-threaded Tokio runtime + +Due to the fact that Azalea clients store the ECS in a Mutex that's frequently locked and unlocked, bots that rely on the `Client` or `Swarm` types may run into race condition bugs (like out-of-order events and ticks happening at suboptimal moments) if they do not set Tokio to use a single thread with `#[tokio::main(flavor = "current_thread")]`. This may change in a future version of Azalea. Setting this option will usually not result in a performance hit, and Azalea internally will keep using multiple threads for running the ECS itself (because Tokio is not used for this). + [`azalea_client`]: https://docs.rs/azalea-client [`bevy_log`]: https://docs.rs/bevy_log diff --git a/azalea/examples/echo.rs b/azalea/examples/echo.rs index 80b0cb15..09c3d5d3 100644 --- a/azalea/examples/echo.rs +++ b/azalea/examples/echo.rs @@ -2,7 +2,7 @@ use azalea::prelude::*; -#[tokio::main] +#[tokio::main(flavor = "current_thread")] async fn main() { let account = Account::offline("bot"); // or let account = Account::microsoft("email").await.unwrap(); diff --git a/azalea/examples/nearest_entity.rs b/azalea/examples/nearest_entity.rs index 2e6973cf..8774829e 100644 --- a/azalea/examples/nearest_entity.rs +++ b/azalea/examples/nearest_entity.rs @@ -12,7 +12,7 @@ use bevy_ecs::{ system::Query, }; -#[tokio::main] +#[tokio::main(flavor = "current_thread")] async fn main() { let account = Account::offline("bot"); diff --git a/azalea/examples/steal.rs b/azalea/examples/steal.rs index 899c2568..87a1561b 100644 --- a/azalea/examples/steal.rs +++ b/azalea/examples/steal.rs @@ -6,7 +6,7 @@ use azalea::{BlockPos, pathfinder::goals::RadiusGoal, prelude::*}; use azalea_inventory::{ItemStack, operations::QuickMoveClick}; use parking_lot::Mutex; -#[tokio::main] +#[tokio::main(flavor = "current_thread")] async fn main() { let account = Account::offline("bot"); // or let bot = Account::microsoft("email").await.unwrap(); diff --git a/azalea/examples/testbot/main.rs b/azalea/examples/testbot/main.rs index 8a35a281..8fb96411 100644 --- a/azalea/examples/testbot/main.rs +++ b/azalea/examples/testbot/main.rs @@ -20,8 +20,6 @@ //! only have this on if the bot has operator permissions, otherwise it'll //! just spam the server console unnecessarily. -#![feature(trivial_bounds)] - mod commands; pub mod killaura; @@ -34,7 +32,7 @@ use azalea::{ use commands::{CommandSource, register_commands}; use parking_lot::Mutex; -#[tokio::main] +#[tokio::main(flavor = "current_thread")] async fn main() { let args = parse_args(); diff --git a/azalea/examples/todo/craft_dig_straight_down.rs b/azalea/examples/todo/craft_dig_straight_down.rs index 0dc8e16d..bf312331 100644 --- a/azalea/examples/todo/craft_dig_straight_down.rs +++ b/azalea/examples/todo/craft_dig_straight_down.rs @@ -1,7 +1,6 @@ use std::sync::Arc; -use azalea::pathfinder; -use azalea::prelude::*; +use azalea::{pathfinder, prelude::*}; use parking_lot::Mutex; #[derive(Default, Clone, Component)] @@ -9,7 +8,7 @@ struct State { pub started: Arc>, } -#[tokio::main] +#[tokio::main(flavor = "current_thread")] async fn main() { let account = Account::offline("bot"); // or let bot = Account::microsoft("email").await; diff --git a/azalea/examples/todo/mine_a_chunk.rs b/azalea/examples/todo/mine_a_chunk.rs index 0c439f26..eb7fafd4 100644 --- a/azalea/examples/todo/mine_a_chunk.rs +++ b/azalea/examples/todo/mine_a_chunk.rs @@ -1,6 +1,6 @@ use azalea::{prelude::*, swarm::prelude::*}; -#[tokio::main] +#[tokio::main(flavor = "current_thread")] async fn main() { let mut accounts = Vec::new(); let mut states = Vec::new(); diff --git a/azalea/examples/todo/pvp.rs b/azalea/examples/todo/pvp.rs index fb5a768d..0639d86b 100644 --- a/azalea/examples/todo/pvp.rs +++ b/azalea/examples/todo/pvp.rs @@ -1,11 +1,11 @@ use std::time::Duration; -use azalea::ecs::query::With; -use azalea::entity::metadata::Player; -use azalea::{pathfinder, Account, Client, Event, GameProfileComponent}; -use azalea::{prelude::*, swarm::prelude::*}; +use azalea::{ + Account, Client, Event, GameProfileComponent, ecs::query::With, entity::metadata::Player, + pathfinder, prelude::*, swarm::prelude::*, +}; -#[tokio::main] +#[tokio::main(flavor = "current_thread")] async fn main() { let mut accounts = Vec::new(); let mut states = Vec::new(); diff --git a/azalea/src/lib.rs b/azalea/src/lib.rs index b3e8a7d9..cdf0afbf 100644 --- a/azalea/src/lib.rs +++ b/azalea/src/lib.rs @@ -59,7 +59,7 @@ pub enum StartError { /// /// ```no_run /// # use azalea::prelude::*; -/// # #[tokio::main] +/// # #[tokio::main(flavor = "current_thread")] /// # async fn main() { /// ClientBuilder::new() /// .set_handler(handle) diff --git a/azalea/src/swarm/mod.rs b/azalea/src/swarm/mod.rs index 35007b9e..ff85e2c1 100644 --- a/azalea/src/swarm/mod.rs +++ b/azalea/src/swarm/mod.rs @@ -636,7 +636,7 @@ pub type BoxSwarmHandleFn = /// #[derive(Default, Clone, Resource)] /// struct SwarmState {} /// -/// #[tokio::main] +/// #[tokio::main(flavor = "current_thread")] /// async fn main() { /// let mut accounts = Vec::new(); /// let mut states = Vec::new();