Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not consistently receiving all GUILD_CREATE events #8726

Closed
SammyWhamy opened this issue Oct 8, 2022 · 5 comments · Fixed by #8731
Closed

Not consistently receiving all GUILD_CREATE events #8726

SammyWhamy opened this issue Oct 8, 2022 · 5 comments · Fixed by #8731

Comments

@SammyWhamy
Copy link

Which package is this bug report for?

ws

Issue description

  1. Test with a bot thats in a few servers, I tested with 2 and then with 6
  2. Run code below (Tested with API version 9 and 10)
  3. Notice how the actual number of guilds received doesnt match the number of expected guilds. If this doesn't happen the first time, restart a few times until the issue occurs
  4. Change the shard count to 1 and notice how the issue does not persist

Code sample

import {WebSocketManager} from "@discordjs/ws";
import {REST} from "@discordjs/rest";

const token = "";
const rest = new REST().setToken(token);

const manager = new WebSocketManager({
    token: token,
    intents: 1,
    rest: rest,
    shardCount: 2,
});

let guildsReceived = 0;

manager.on('ready', ({shardId, data}) => {
    console.log(`Shard ${shardId} ready. Expected guilds: ${JSON.stringify(data.guilds.map(g => g.id))}`);
});

manager.on('dispatch', ({shardId, data}) => {
    console.log(`Dispatch on shard ${shardId}: ${data.t}`);

    if (data.t === "GUILD_CREATE") {
        guildsReceived++;
        console.log(`Guild ${data.d.id} created! (${guildsReceived} total)`);
    }
});

manager.connect();

Package version

0.4.0

Node.js version

16.13.0

Operating system

Windows 10

Priority this issue should have

High (immediate attention needed)

Which partials do you have configured?

Not applicable (subpackage bug)

Which gateway intents are you subscribing to?

Guilds

I have tested this issue on a development release

62332c2 (0.3.1) and 33ec49d (0.4.0)

@SammyWhamy
Copy link
Author

Just tested, I cannot replicate this without using @discordjs/ws, so I'm pretty sure the issue here lies within the subpackage itself and not with the actual API

@t3ned
Copy link

t3ned commented Oct 10, 2022

✅ I can reproduce this issue using the snippet from above.

Test Attempt 1

Shards: 2
Guilds: 7

Case 1:
Shard 0: 4 / 4 guilds present in dispatch
Shard 1: 1 / 2 guilds present in dispatch
(last guild of last shard is missing)

Case 2:
Shard 0: 4 / 4 guilds present in dispatch
Shard 1: 2 / 2 guilds present in dispatch

Case 3:
Shard 0: 4 / 4 guilds present in dispatch
Shard 1: 1 / 2 guilds present in dispatch
(last guild of last shard is missing

Case 4:
Shard 0: 4 / 4 guilds present in dispatch
Shard 1: 1 / 2 guilds present in dispatch
(last guild of last shard is missing)

Case 5:
Shard 0: 4 / 4 guilds present in dispatch
Shard 1: 2 / 2 guilds present in dispatch

Test Attempt 2

Shards: 3
Guilds: 7

Case 1:
Shard 0: 4 / 4 guilds present in dispatch
Shard 1: 1 / 2 guilds present in dispatch
Shard 2: 0 / 1 guilds present in dispatch
(last guild of shard 1 and shard 2 is missing)

Case 2:
Shard 0: 3 / 4 guilds present in dispatch
Shard 1: 2 / 2 guilds present in dispatch
Shard 2: 1 / 1 guilds present in dispatch
(last guild of shard 0 is missing)

Case 3:
Shard 0: 3 / 4 guilds present in dispatch
Shard 1: 1 / 2 guilds present in dispatch
Shard 2: 1 / 1 guilds present in dispatch
(last guild of shard 0 is missing)

Case 4:
Shard 0: 3 / 4 guilds present in dispatch
Shard 1: 2 / 2 guilds present in dispatch
Shard 2: 1 / 1 guilds present in dispatch
(last guild of shard 1 is missing)

Case 5:
Shard 0: 4 / 4 guilds present in dispatch
Shard 1: 2 / 2 guilds present in dispatch
Shard 2: 1 / 1 guilds present in dispatch

@didinele
Copy link
Member

didinele commented Oct 10, 2022

Thanks for the flag! Could you help me out by also listening to the debug event and providing complete logs? This seems like a race condition and I'm just trying to figure out where things go wrong.

@SammyWhamy
Copy link
Author

Here's some logs where the guild 560855214597341217 didn't show up

Debug on shard 0: Connecting to wss://gateway.discord.gg?v=10&encoding=json
Debug on shard 0: Waiting for event hello for 60000ms
Debug on shard 0: Starting to heartbeat every 41250ms
Debug on shard 0: Identifying
        shard id: 0
        shard count: 2
        intents: 1
        compression: none
Debug on shard 0: Waiting for event ready for 15000ms
Shard 0 ready. Expected guilds: ["498983946512629761","560855214597341217","876459594078093373","950327431108100116","1004717981269504072"]
Dispatch on shard 0: GUILD_CREATE
Guild 498983946512629761 created! (1 total)
Dispatch on shard 0: GUILD_CREATE
Guild 876459594078093373 created! (2 total)
Dispatch on shard 0: GUILD_CREATE
Guild 950327431108100116 created! (3 total)
Dispatch on shard 0: GUILD_CREATE
Guild 1004717981269504072 created! (4 total)
Debug on shard 1: Connecting to wss://gateway.discord.gg?v=10&encoding=json
Debug on shard 1: Waiting for event hello for 60000ms
Debug on shard 1: Starting to heartbeat every 41250ms
Debug on shard 1: Identifying
        shard id: 1
        shard count: 2
        intents: 1
        compression: none
Debug on shard 1: Waiting for event ready for 15000ms
Shard 1 ready. Expected guilds: ["630146734181449774","876459675971899402"]
Dispatch on shard 1: GUILD_CREATE
Guild 876459675971899402 created! (5 total)
Dispatch on shard 1: GUILD_CREATE
Guild 630146734181449774 created! (6 total)
Debug on shard 0: Got heartbeat ack after 116ms
Debug on shard 1: Got heartbeat ack after 108ms
Debug on shard 0: Got heartbeat ack after 114ms
Debug on shard 1: Got heartbeat ack after 109ms

@didinele
Copy link
Member

Diagnosed the issue. Was a race condition if Discord sent GUILD_CREATE packet(s) very quickly after READY, in which case the internal status wouldn't be updated, and we would drop the event.

#8731 fixes it.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants