3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-16 22:21:51 +00:00

Add a shared lock for Persist and ReactionRoles plugins to prevent race conditions

This commit is contained in:
Dragory 2020-05-22 20:35:15 +03:00
parent a5178dc78f
commit 47c5722060
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
2 changed files with 14 additions and 3 deletions

View file

@ -71,8 +71,13 @@ export class PersistPlugin extends ZeppelinPlugin<TConfigSchema> {
@d.event("guildMemberAdd") @d.event("guildMemberAdd")
async onGuildMemberAdd(_, member: Member) { async onGuildMemberAdd(_, member: Member) {
const memberRolesLock = await this.locks.acquire(`member-roles-${member.id}`);
const persistedData = await this.persistedData.find(member.id); const persistedData = await this.persistedData.find(member.id);
if (!persistedData) return; if (!persistedData) {
memberRolesLock.unlock();
return;
}
const toRestore: MemberOptions = {}; const toRestore: MemberOptions = {};
const config = this.getConfig(); const config = this.getConfig();
@ -101,5 +106,7 @@ export class PersistPlugin extends ZeppelinPlugin<TConfigSchema> {
restoredData: restoredData.join(", "), restoredData: restoredData.join(", "),
}); });
} }
memberRolesLock.unlock();
} }
} }

View file

@ -189,6 +189,10 @@ export class ReactionRolesPlugin extends ZeppelinPlugin<TConfigSchema> {
timeout: null, timeout: null,
changes: [], changes: [],
applyFn: async () => { applyFn: async () => {
this.pendingRoleChanges.delete(memberId);
const lock = await this.locks.acquire(`member-roles-${memberId}`);
const member = await this.getMember(memberId); const member = await this.getMember(memberId);
if (member) { if (member) {
const newRoleIds = new Set(member.roles); const newRoleIds = new Set(member.roles);
@ -206,9 +210,9 @@ export class ReactionRolesPlugin extends ZeppelinPlugin<TConfigSchema> {
`Failed to apply role changes to ${member.username}#${member.discriminator} (${member.id}): ${e.message}`, `Failed to apply role changes to ${member.username}#${member.discriminator} (${member.id}): ${e.message}`,
); );
} }
this.pendingRoleChanges.delete(memberId);
} }
lock.unlock();
}, },
}; };