Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Race condition when generating CustomFieldBehavior files #7379

Closed
mattgrayisok opened this issue Jan 12, 2021 · 2 comments
Closed

Race condition when generating CustomFieldBehavior files #7379

mattgrayisok opened this issue Jan 12, 2021 · 2 comments
Labels

Comments

@mattgrayisok
Copy link

Description

If multiple simultaneous requests are received by Craft after compiled classes are cleared it's possible to hit a race condition when generating CustomFieldBehavior files. Resulting in:

2021-01-11 18:02:57 [-][-][-][error][yii\base\ErrorException:2] yii\base\ErrorException: rename(/var/www/html/storage/runtime/compiled_classes/CustomFieldBehavior_kcrkgabnyotw5ffc92d1058d85.79986753.php,/var/www/html/storage/runtime/compiled_classes/CustomFieldBehavior_kcrkgabnyotw.php): No such file or directory in /var/www/html/vendor/craftcms/cms/src/Craft.php:277

Stack trace:
#0 /var/www/html/vendor/craftcms/cms/src/web/ErrorHandler.php(76): yii\base\ErrorHandler->handleError(2, 'rename(/var/www...', '/var/www/html/v...', 277)
#1 [internal function]: craft\web\ErrorHandler->handleError(2, 'rename(/var/www...', '/var/www/html/v...', 277, Array)
#2 /var/www/html/vendor/craftcms/cms/src/Craft.php(277): rename('/var/www/html/s...', '/var/www/html/s...')
#3 /var/www/html/vendor/craftcms/cms/src/Craft.php(221): Craft::_generateCustomFieldBehavior(Array, '/var/www/html/s...', true, false)
#4 /var/www/html/vendor/craftcms/cms/src/Craft.php(169): Craft::_autoloadCustomFieldBehavior()

The offending code is here:

$tmpFile = $dir . DIRECTORY_SEPARATOR . uniqid(pathinfo($filePath, PATHINFO_FILENAME), true) . '.php';

The problem exists because the temporary files being generated are prefixed with the same filename pattern that is used to clean up any old files. This causes the first request which exits the writeToFile mutex lock to delete the temp files created by any other simultaneously processing requests, subsequently causing them to error out on the following rename().

🥩

Steps to reproduce

  1. Clear the compiled classes cache
  2. Send some simultaneous requests to Craft

Additional info

  • Craft version: 3.5
  • PHP version: 7.4
  • Database driver & version:
  • Plugins & versions:
@brandonkelly
Copy link
Member

Thanks for reporting that! Just fixed for the next release. Now Craft will only delete other CustomFieldBehavior classes that are over 10 seconds old.

@brandonkelly
Copy link
Member

Craft 3.5.18 is out now with that fix ✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants