twilight icon indicating copy to clipboard operation
twilight copied to clipboard

Extra request might slip past ratelimit checks

Open jogramming opened this issue 4 years ago • 5 comments

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.

jogramming avatar Nov 25 '21 19:11 jogramming

Assigning to self.

spring4175 avatar Dec 05 '21 18:12 spring4175

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

lemon-sh avatar Dec 09 '21 09:12 lemon-sh

#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" }

spring4175 avatar Dec 24 '21 16:12 spring4175

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

jogramming avatar Jan 03 '22 17:01 jogramming

re-opening so this doesn't get lost

AEnterprise avatar Jan 03 '22 20:01 AEnterprise