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

WebSocketManager trying to access shard info while shard or shardId could be undefined #10410

Closed
CuteNikki opened this issue Jul 26, 2024 · 6 comments
Assignees

Comments

@CuteNikki
Copy link

Which package is this bug report for?

discord.js

Issue description

Steps to reproduce:
Not always reproducable (the issue randomly shows up and disappears both locally and on my server)
Repo: https://github.com/CuteNikki/discord-bot/

I am using a third-party ShardingManager called discord-hybrid-sharding
but this issue also occurred to me without any kind of Sharding?

So the error I am getting is the following:

C:\Users\nikki\Documents\Code\discord_bots\discord-bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:268
      if (shard.sessionInfo) {
                ^

TypeError: Cannot read properties of undefined (reading 'sessionInfo')
    at WebSocketManager.<anonymous> (C:\Users\nikki\Documents\Code\discord_bots\discord-bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:268:17)
    at WebSocketManager.emit (C:\Users\nikki\Documents\Code\discord_bots\discord-bot\node_modules\@vladfrangu\async_event_emitter\src\index.ts:360:28)
    at WebSocketShard.<anonymous> (C:\Users\nikki\Documents\Code\discord_bots\discord-bot\node_modules\@discordjs\ws\src\strategies\sharding\SimpleShardingStrategy.ts:32:47)
    at WebSocketShard.emit (C:\Users\nikki\Documents\Code\discord_bots\discord-bot\node_modules\@vladfrangu\async_event_emitter\src\index.ts:371:33)
    at WebSocketShard.onMessage (C:\Users\nikki\Documents\Code\discord_bots\discord-bot\node_modules\@discordjs\ws\src\ws\WebSocketShard.ts:755:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

The error seems to be happening right here:

this._ws.on(WSWebSocketShardEvents.Hello, ({ shardId }) => {
    const shard = this.shards.get(shardId);
    if (shard.sessionInfo) {
        shard.closeSequence = shard.sessionInfo.sequence;
        shard.status = Status.Resuming;
    } else {
        shard.status = Status.Identifying;
    }
});

My assumption is that either the shard or the shardId was undefined.
In my opinion, shard should first be if checked to see if it even exists before trying to access its properties.

Code sample

No response

Versions

  • discord.js 14.15.3 and multiple dev release up to 14.16.0-dev.1721607097-b2970bb2d
  • typescript 5.4.5 and 5.5.3
  • node from 20.11.0 and 22.3.0

Issue priority

High (immediate attention needed)

Which partials do you have configured?

User, Channel, GuildMember, Message, Reaction, GuildScheduledEvent, ThreadMember

Which gateway intents are you subscribing to?

Guilds, GuildMembers, GuildModeration, GuildEmojisAndStickers, GuildInvites, GuildVoiceStates, GuildPresences, MessageContent, GuildScheduledEvents, AutoModerationConfiguration, AutoModerationExecution

I have tested this issue on a development release

b2970bb

@Qjuh
Copy link
Contributor

Qjuh commented Jul 26, 2024

Provide me with a reproducible code sample causing this or if that’s not possible at least console.log(this.shards, shardId) in there when the issue happens for further debugging.

@CuteNikki
Copy link
Author

if that’s not possible at least console.log(this.shards, shardId) in there when the issue happens for further debugging.

A console log shows that both shardId and shard is undefined.
Printing this.shards results in this:

<ref *1> Collection(1) [Map] {
  0 => WebSocketShard {
    _events: [Object: null prototype] { allReady: [Function (anonymous)] },
    _eventsCount: 1,
    _maxListeners: undefined,
    manager: WebSocketManager {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      gateway: 'wss://gateway.discord.gg/',
      shards: [Circular *1],
      status: 3,
      destroyed: false,
      _ws: [WebSocketManager],
      totalShards: 1,
      [Symbol(shapeMode)]: false,
      [Symbol(kCapture)]: false
    },
    id: 0,
    status: 1,
    closeSequence: 0,
    ping: -1,
    lastPingTimestamp: -1,
    [Symbol(shapeMode)]: false,
    [Symbol(kCapture)]: false
  }
}

@didinele
Copy link
Member

didinele commented Jul 26, 2024

Are you using overrides of some sort to use a dev build of @discordjs/ws? Can you run npm ls @discordjs/ws?

@CuteNikki
Copy link
Author

Are you using overrides of some sort to use a dev build of @discordjs/ws? Can you run npm ls @discordjs/ws?

Yes, I am using all these overrrides:

  "overrides": {
    "@discordjs/brokers": "dev",
    "@discordjs/builders": "dev",
    "@discordjs/collection": "dev",
    "@discordjs/core": "dev",
    "@discordjs/formatters": "dev",
    "@discordjs/proxy": "dev",
    "@discordjs/rest": "dev",
    "@discordjs/voice": "dev",
    "@discordjs/util": "dev",
    "@discordjs/ws": "dev"
  },

@CuteNikki
Copy link
Author

I just tried with only the builders override since I need that one and the issue appears to be gone at the moment.
But since it's not reliably reproducable, I can't say for sure.

@didinele
Copy link
Member

didinele commented Jul 26, 2024

Yes, we merged a pull request into /ws that changes how we do events entirely. What you're experiencing here is discord.js not being updated to handle that (on purpose at this time), there isn't actually a bug.

Feel free to re-open if you can reproduce without the dev version of ws being used.

@didinele didinele closed this as not planned Won't fix, can't repro, duplicate, stale Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants