fix(api): prevent race condition when creating logs directory (Issue #9352)#9353
Conversation
📝 WalkthroughWalkthroughThe LOG_DIR directory creation logic in local development settings was changed from a manual existence check followed by os.makedirs(LOG_DIR) to a single unconditional os.makedirs(LOG_DIR, exist_ok=True) call, preventing a race condition during concurrent container startup. ChangesLocal Settings Fix
Estimated code review effort: 1 (Trivial) | ~2 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
apps/api/plane/settings/local.py (1)
37-37: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick winSame race condition likely exists in
production.py.
apps/api/plane/settings/production.pyuses the same check-then-mkdir pattern forLOG_DIRthis PR fixes here. Consider applying the identicalexist_ok=Truefix there for consistency, since production containers can also start concurrently.♻️ Suggested fix for production.py
LOG_DIR = os.path.join(BASE_DIR, "logs") # noqa -if not os.path.exists(LOG_DIR): - os.makedirs(LOG_DIR) +os.makedirs(LOG_DIR, exist_ok=True)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@apps/api/plane/settings/local.py` at line 37, The same directory-creation race condition fix applied in local settings should be mirrored in production settings: update the LOG_DIR initialization in production.py to use the same atomic mkdir approach with exist_ok=True instead of a check-then-create pattern. Locate the LOG_DIR setup logic in the production settings module and make it consistent with the local.py change so concurrent startup cannot fail there either.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@apps/api/plane/settings/local.py`:
- Line 37: The same directory-creation race condition fix applied in local
settings should be mirrored in production settings: update the LOG_DIR
initialization in production.py to use the same atomic mkdir approach with
exist_ok=True instead of a check-then-create pattern. Locate the LOG_DIR setup
logic in the production settings module and make it consistent with the local.py
change so concurrent startup cannot fail there either.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: c7a3355a-bb14-4022-a20c-c7bc4563e03a
📒 Files selected for processing (1)
apps/api/plane/settings/local.py
Description
Fixes a startup crash in the local Docker environment where backend containers (
api,migrator,worker,beat-worker) concurrently load Django configurations and trigger a race condition on directory creation.Root cause: Multiple Django processes checking
if not os.path.exists(LOG_DIR)simultaneously on boot, resulting in a slower process throwing aFileExistsErroronos.makedirs(LOG_DIR).Fix: Changed
os.makedirs(LOG_DIR)to useexist_ok=Trueto make the directory creation thread-safe and prevent the startup crash.Type of Change
Screenshots and Media (if applicable)
N/A (Backend startup fix)
Test Scenarios
docker compose -f docker-compose-local.yml up -d --build.docker logs plane-migrator-1anddocker logs plane-api-1).FileExistsError: [Errno 17] File exists: '/code/plane/logs'.References
Fixes #9352
Summary by CodeRabbit