Skip to content

[4.x] Prevent mkdir() race conditions in FilesystemTenancyBootstrapper#1453

Merged
stancl merged 1 commit intomasterfrom
push-oswunzxspuur
Apr 13, 2026
Merged

[4.x] Prevent mkdir() race conditions in FilesystemTenancyBootstrapper#1453
stancl merged 1 commit intomasterfrom
push-oswunzxspuur

Conversation

@stancl
Copy link
Copy Markdown
Member

@stancl stancl commented Apr 13, 2026

Prevent mkdir() race conditions in FilesystemTenancyBootstrapper

This prevents race conditions that may occur if there are two concurrent
processes trying to create the storage path for the tenant. The
storagePath() method runs during bootstrap() which can easily happen
in two places at once. The race condition specifically occurs in between
the is_dir() check and the mkdir() call, the latter producing an
exception if the dir already exist. We simply ignore any error coming
out of mkdir() and then check for success separately.

We could omit that success check since failure is unlikely and would
only occur due to a server misconfiguration that would manifest itself
in other ways as well, but this way the simple TOC/TOU race condition
is prevented while other errors are still reported.

We apply the same change to the mkdir() in scopeSessions() as the logic
is similar.

Resolves #1452

Summary by CodeRabbit

  • Bug Fixes
    • Suppressed spurious filesystem warnings during concurrent startup and added clearer failure reporting when required cache and session storage directories cannot be created, reducing noisy logs and improving reliability during application boot.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 13, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: b25c04c7-6017-4ca7-9c9b-774672b561c3

📥 Commits

Reviewing files that changed from the base of the PR and between 60dd522 and c344e3d.

📒 Files selected for processing (1)
  • src/Bootstrappers/FilesystemTenancyBootstrapper.php

📝 Walkthrough

Walkthrough

Directory creation in FilesystemTenancyBootstrapper now uses error-suppressed mkdir calls (@mkdir) and performs a post-condition is_dir($path) check; if the directory still does not exist, the code throws an Exception. The Exception class is imported.

Changes

Cohort / File(s) Summary
Filesystem bootstrapper directory handling
src/Bootstrappers/FilesystemTenancyBootstrapper.php
Replaced mkdir() with @mkdir() to suppress concurrent "File exists" warnings; added post-creation is_dir($path) verification and throw Exception when creation fails; added use Exception; import.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 I dug a tunnel, neat and small,

Two paws raced — I heard the call.
A hush of @ keeps clatter down,
If tunnels fail, I'll raise a sound.
Hop on, retry — no reason to frown.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: preventing mkdir() race conditions in FilesystemTenancyBootstrapper by suppressing warnings and re-checking directory existence.
Linked Issues check ✅ Passed The PR implementation matches the requirements from issue #1452: it suppresses mkdir() warnings with @ operator and re-verifies directory existence, addressing both storagePath() and scopeSessions() race conditions.
Out of Scope Changes check ✅ Passed All changes are directly scoped to addressing the TOCTOU race condition in FilesystemTenancyBootstrapper; no unrelated modifications are present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch push-oswunzxspuur

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/Bootstrappers/FilesystemTenancyBootstrapper.php`:
- Line 79: In FilesystemTenancyBootstrapper (the bootstrap routine that calls
`@mkdir`), keep the `@mkdir` for race tolerance but immediately check that the
target directory actually exists after the call; if it does not, throw a generic
\Exception with a clear message including the path so bootstrap fails fast.
Apply the same change to the other occurrence of `@mkdir` in this class (the
second directory-creation site) so neither silent mkdir failure is swallowed.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 9736031c-344f-4e02-b25a-181d6bda6379

📥 Commits

Reviewing files that changed from the base of the PR and between 60dd522 and fcd4d82.

📒 Files selected for processing (1)
  • src/Bootstrappers/FilesystemTenancyBootstrapper.php

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 13, 2026

Codecov Report

❌ Patch coverage is 66.66667% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.03%. Comparing base (60dd522) to head (c344e3d).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...rc/Bootstrappers/FilesystemTenancyBootstrapper.php 66.66% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1453      +/-   ##
============================================
- Coverage     86.08%   86.03%   -0.05%     
- Complexity     1154     1156       +2     
============================================
  Files           184      184              
  Lines          3377     3381       +4     
============================================
+ Hits           2907     2909       +2     
- Misses          470      472       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This comment was marked as outdated.

This prevents race conditions that may occur if there are two concurrent
processes trying to create the storage path for the tenant. The
storagePath() method runs during bootstrap() which can easily happen
in two places at once. The race condition specifically occurs in between
the is_dir() check and the mkdir() call, the latter producing an
exception if the dir already exist. We simply ignore any error coming
out of mkdir() and then check for success separately.

We could omit that success check since failure is unlikely and would
only occur due to a server misconfiguration that would manifest itself
in other ways as well, but this way the simple TOC/TOU race condition
is prevented while other errors are still reported.

We apply the same change to the mkdir() in scopeSessions() as the logic
is similar.

Resolves #1452
@stancl stancl force-pushed the push-oswunzxspuur branch from fcd4d82 to c344e3d Compare April 13, 2026 21:44
@stancl stancl changed the title [4.x] Ignore errors on mkdir() in FilesystemTenancyBootstrapper [4.x] Prevent mkdir() race conditions in FilesystemTenancyBootstrapper Apr 13, 2026
@stancl
Copy link
Copy Markdown
Member Author

stancl commented Apr 13, 2026

@coderabbitai full review

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 13, 2026

✅ Actions performed

Full review triggered.

@stancl stancl merged commit e31249d into master Apr 13, 2026
12 of 14 checks passed
@stancl stancl deleted the push-oswunzxspuur branch April 13, 2026 21:58
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.

Race condition in FilesystemTenancyBootstrapper::storagePath() causes mkdir(): File exists warning

2 participants