Skip to content

Commit 942635e

Browse files
committed
fix: add exception handling in _auto_enter_users loop and task body
1 parent 419a0dc commit 942635e

1 file changed

Lines changed: 27 additions & 14 deletions

File tree

luckypot/game.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ async def process_pot_win(
246246
elif announce_fn:
247247
suffix = f" ({win_type})" if win_type != "DAILY DRAW" else ""
248248
await announce_fn(f"<@{winner_id}> won {winning_amount} STK!{suffix}")
249+
# Fire-and-forget: the delay in _auto_enter_users ensures _guild_locks[guild_id]
250+
# is released by the time enter_pot() is called for opted-in users.
249251
asyncio.create_task(_auto_enter_users(guild_id, announce_fn=announce_fn))
250252
else:
251253
logger.error(f"Failed to send winnings to {winner_id}, pot remains active")
@@ -288,25 +290,36 @@ async def _auto_enter_users(guild_id: str, announce_fn: AnnounceFn = None) -> No
288290
Uses the existing enter_pot() which handles all edge cases (bans,
289291
already-entered, STK account missing, etc.).
290292
"""
291-
await asyncio.sleep(AUTO_ENTER_DELAY_SECONDS)
292-
293-
conn = db.get_connection()
294293
try:
295-
discord_ids = db.get_auto_enter_users(conn, guild_id)
296-
finally:
297-
conn.close()
294+
await asyncio.sleep(AUTO_ENTER_DELAY_SECONDS)
298295

299-
if not discord_ids:
300-
return
296+
conn = db.get_connection()
297+
try:
298+
discord_ids = db.get_auto_enter_users(conn, guild_id)
299+
finally:
300+
conn.close()
301+
302+
if not discord_ids:
303+
return
301304

302-
logger.info(
303-
f"Auto-entering {len(discord_ids)} user(s) into new pot for guild {guild_id}"
304-
)
305-
for discord_id in discord_ids:
306-
result = await enter_pot(discord_id, guild_id, announce_fn=announce_fn)
307305
logger.info(
308-
f"Auto-enter for discord_id={discord_id} guild={guild_id}: status={result['status']}"
306+
f"Auto-entering {len(discord_ids)} user(s) into new pot for guild {guild_id}"
309307
)
308+
for discord_id in discord_ids:
309+
try:
310+
result = await enter_pot(discord_id, guild_id, announce_fn=announce_fn)
311+
logger.info(
312+
f"Auto-enter for discord_id={discord_id} guild={guild_id}: status={result['status']}"
313+
)
314+
except Exception:
315+
logger.exception(
316+
f"Auto-enter failed for discord_id={discord_id} guild={guild_id}"
317+
)
318+
except asyncio.CancelledError:
319+
logger.warning(f"Auto-enter task cancelled for guild {guild_id}")
320+
raise
321+
except Exception:
322+
logger.exception(f"Auto-enter task failed for guild {guild_id}")
310323

311324

312325
async def end_pot_with_winner(

0 commit comments

Comments
 (0)