question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Race Condition when Adding Roles (Adding Roles becomes "Replace Roles" instead)

See original GitHub issue

Which package is this bug report for?

discord.js

Issue description

When adding a role, Discord.js disregards additional roles added after the current add role event is run and instead replaces them with the roles that were current at that time. Not sure how I can show code examples (because the issue involves two bots, one is DSB and the other one from Memberful, but I can show you what I see from my debugger, step by step.

To replicate this, you would need to have a Memberful Pro account (a non-paid version can be signed up to test) and that account needs to integrate with the Discord server you’re about to test on. The Gold and Platinum plans are in one plan family.

Bots Involved for Reference ONLY DSB (uses Discord.js) Memberful Bot

Roles Involved for Reference ONLY Basic - 841111442128371712 Gold - 844316656582000680 Platinum - 844286730583801858 @ everyone - 840936982951886887

Step 1:

Start the user with a Gold role (844316656582000680). The user should already be signed up to the Gold plan in Memberful.

Step 2:

Upgrade the user to Platinum in Memberful, where the Memberful bot removes the Gold role (844316656582000680).

Step 3:

This is where the race condition starts. When the Gold role (844316656582000680) is removed, the user is left with no roles. Then an event starts from DSB to add the Basic role (841111442128371712). At this time, the cache only states that the user has the @ everyone (840936982951886887) role.

Screen Shot 2022-05-03 at 11 49 42 AM

Step 4:

Memberful proceeds to add the Platinum role (844286730583801858) to the user.

Screen Shot 2022-05-03 at 11 56 51 AM

Step 5:

DSB runs the add method from GuildMemberRoleManager and proceeds to replace the role added in Step 4. Notice how there isn’t a Platinum role (844286730583801858) in the newRoles constant?

Screen Shot 2022-05-03 at 11 59 43 AM

Code sample

No response

Package version

13.6.0

Node.js version

16.13.2 (typescript 4.6.3)

Operating system

No response

Priority this issue should have

Medium (should be fixed soon)

Which partials do you have configured?

No Partials

Which gateway intents are you subscribing to?

Guilds, GuildMembers, GuildBans, GuildEmojisAndStickers, GuildInvites, GuildVoiceStates, GuildPresences, GuildMessages, GuildMessageReactions, DirectMessages

I have tested this issue on a development release

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
prater12commented, May 4, 2022

If I pre-define the GuildMemberRoleManager before the timeouts, would that also set the cache that’s inside it? It’s nerve racking to think about it because I have code inside the callback that resolves the member and somehow I feel like it’s so unnecessary and bulky.

As long as the member isn’t removed from the cache, the cache belonging to the manager will remain updated. If I’m not wrong, members don’t get uncached normally, only if you’ve used a CacheFactory to override the default cache

2reactions
tipakAcommented, May 4, 2022

Implementing a timeout would actually solve it. The issue comes primarly from the fact that without any timeout, both bots act on current (equal) cache, each one adding a different role. Discord.js does update cache upon receiving events, which means that if you wait “long enough” to receive an event about member having roles updated by a different bot, calling add after that will not produce any issues, as the add will work with cache that already includes the role given by the other bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bot gives the role, and then removes it · Issue #1988 - GitHub
Your issue here is a mixture of a race condition and the how the lib handles removing or adding of multiple roles.
Read more >
Fixing a Race Condition - Medium
I'd like to share my journey of fixing a race condition and the things ... update the existing interviewer instead of creating a...
Read more >
Is SELECT or INSERT in a function prone to race conditions?
It's the recurring problem of SELECT or INSERT under possible concurrent write load, related to (but different from) UPSERT (which is INSERT or...
Read more >
What is a Race Condition? - Veracode
Learn more about race condition vulnerability: what it is, what happens during an attack and how Veracode can aid in the removal of...
Read more >
Social Role Theory - an overview | ScienceDirect Topics
When group members enact social roles that are more tightly linked to the context than gender, such as manager and employee in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found