Skip to content

Conversation

Copy link

Copilot AI commented Feb 10, 2026

Fix for Issue mod-playerbots#2103: Arena Teams Continue to Grow

Problem Analysis

The issue is a race condition in PlayerbotFactory::InitArenaTeam(). When multiple bots log in simultaneously during server startup, they all check randomBotArenaTeams.empty() before any of them finish creating teams, causing duplicate team creation and arena teams to grow with each server restart.

Solution

Used a mutex with RAII and proper scoping for thread-safe arena team creation:

  1. Added std::mutex arenaTeamCreationMutex to synchronize team creation
  2. Team creation code is protected by mutex with try_to_lock
  3. Lock is scoped within a block and automatically released after team creation
  4. Threads that don't acquire the lock skip team creation but can still join existing teams
  5. Lock automatically releases on scope exit via RAII, ensuring exception safety
  6. Only one thread creates teams, but all threads can proceed to join teams

Implementation Plan

  • Add a static mutex to prevent concurrent arena team creation
  • Use try_lock to avoid blocking threads
  • Ensure thread safety and exception safety with RAII
  • Scope the lock properly to allow bots to join while creation happens
  • Pass codestyle checks
  • Address all code review feedback
  • Run security scan
  • Remove unrelated files from commit
  • Complete implementation and testing

Changes Made

  • Added #include <mutex> for thread synchronization
  • Added static std::mutex arenaTeamCreationMutex
  • Modified InitArenaTeam() to use scoped std::unique_lock with try_lock
  • Lock is released before team joining logic, allowing all bots to join
  • Only 1 file changed: src/Bot/Factory/PlayerbotFactory.cpp (30 insertions, 20 deletions)
  • All code style checks pass
  • No security vulnerabilities detected

Testing Notes

This fix should be tested by:

  1. Starting a server with a fresh database
  2. Allowing bots to log in and create arena teams
  3. Restarting the server multiple times
  4. Verifying that arena teams are NOT duplicated across restarts
  5. The number of teams should remain constant at the configured values (e.g., 40 for 2v2, 30 for 3v3, 20 for 5v5)

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 4 commits February 10, 2026 18:56
Co-authored-by: kadeshar <28183654+kadeshar@users.noreply.github.com>
Co-authored-by: kadeshar <28183654+kadeshar@users.noreply.github.com>
Co-authored-by: kadeshar <28183654+kadeshar@users.noreply.github.com>
Co-authored-by: kadeshar <28183654+kadeshar@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix issue 2103 with player bot functionality Fix race condition in arena team creation causing duplicates on restart Feb 10, 2026
Copilot AI requested a review from kadeshar February 10, 2026 19:00
@@ -0,0 +1 @@
. No newline at end of file
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot not releated with issue, revert this change

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in commit 505f184

Co-authored-by: kadeshar <28183654+kadeshar@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants