twilight-rs / twilight

Powerful, flexible, and scalable ecosystem of Rust libraries for the Discord API.
https://discord.gg/twilight-rs
ISC License
658 stars 129 forks source link

Extra request might slip past ratelimit checks #1305

Open jogramming opened 2 years ago

jogramming commented 2 years ago

After spending some time debugging why i was getting 429's on stuff that i really shouldn't be getting it on i managed to boil it down to a bug in twilight's ratelimit handling and make a pretty simple reproducible case.

The below code will cause a ratelimit error:

use std::{sync::Arc, time::Duration};

use twilight_http::Client;
use twilight_model::id::ChannelId;

#[tokio::main]
async fn main() {
    let token = std::env::var("DISCORD_TOKEN").expect("set DISCORD_TOKEN");
    let client = Arc::new(Client::new(token));

    send_test_message(client.clone()).await;
    tokio::time::sleep(Duration::from_secs(6)).await;

    tokio::join!(
        send_test_message(client.clone()),
        send_test_message(client.clone()),
        send_test_message(client.clone()),
        send_test_message(client.clone()),
        send_test_message(client.clone()),
        send_test_message(client.clone()),
    );
}

async fn send_test_message(client: Arc<Client>) {
    println!("sending req");
    match client
        .create_message(ChannelId::new(913455776704638996).unwrap())
        .content("test")
        .unwrap()
        .exec()
        .await
    {
        Ok(_) => {}
        Err(e) => {
            println!("got error: {}", e);
        }
    }
}

1 request in that join should have been delayed, but somehow slipped past twilight's ratelimit handling.

I suspect it has something to do with the order of checks, how you wait for ratelimits before popping things off the queue but im not knowledgeable enough about twilight's internals to be sure.

zeylahellyer commented 2 years ago

Assigning to self.

lemon-sh commented 2 years ago

I'm having a similar issue, this might be related to #1047 and #600

zeylahellyer commented 2 years ago

1348 will close this, you can give it a try with your test case if you want (it worked for me):

[dependencies]
twilight-http = { branch = "fix-http-ratelimiting-started-at", git = "https://github.com/zeylahellyer/twilight" }
twilight-model = { branch = "fix-http-ratelimiting-started-at", git = "https://github.com/zeylahellyer/twilight" }
jogramming commented 2 years ago

this is still triggering an error for me and looking back at the releases it seems like the fix was reverted (#1357)

AEnterprise commented 2 years ago

re-opening so this doesn't get lost