From ff692c19b77a7dcae07dcb71f33c4753db106f5d Mon Sep 17 00:00:00 2001 From: Zane Helton Date: Sat, 28 Jun 2025 18:02:54 -0400 Subject: [PATCH] Await guild creation to fix a race condition `Promise.all(...)` runs these simultaneously, yet role depends on the guild to be created first. This can lead to a race condition. I've awaited role creation too because the call to `Member.addToGuild(...)` relies on the role to be created. --- src/api/routes/guilds/templates/index.ts | 37 ++++++++++++------------ 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/src/api/routes/guilds/templates/index.ts b/src/api/routes/guilds/templates/index.ts index a611f421..c238b1d1 100644 --- a/src/api/routes/guilds/templates/index.ts +++ b/src/api/routes/guilds/templates/index.ts @@ -140,25 +140,24 @@ router.post( const guild_id = Snowflake.generate(); - const [guild] = await Promise.all([ - Guild.create({ - ...body, - ...template.serialized_source_guild, - id: guild_id, - owner_id: req.user_id, - }).save(), - Role.create({ - id: guild_id, - guild_id: guild_id, - color: 0, - hoist: false, - managed: true, - mentionable: true, - name: "@everyone", - permissions: BigInt("2251804225").toString(), // TODO: where did this come from? - position: 0, - }).save(), - ]); + const guild = await Guild.create({ + ...body, + ...template.serialized_source_guild, + id: guild_id, + owner_id: req.user_id, + }).save(); + + await Role.create({ + id: guild_id, + guild_id: guild_id, + color: 0, + hoist: false, + managed: true, + mentionable: true, + name: "@everyone", + permissions: BigInt("2251804225").toString(), // TODO: where did this come from? + position: 0, + }).save(); await Member.addToGuild(req.user_id, guild_id);