Do not create cache_dir for non-filesystem cache backends (#41157) - #41158
Open
lbajsarowicz wants to merge 1 commit into
Open
Do not create cache_dir for non-filesystem cache backends (#41157)#41158lbajsarowicz wants to merge 1 commit into
lbajsarowicz wants to merge 1 commit into
Conversation
) Cache\Frontend\Factory::resolveCacheDir() created every configured cache_dir under var/ before the backend type was resolved. Because di.xml hardcodes cache_dir for page_cache, an empty var/page_cache was created even with all frontends on Redis, breaking split CLI/PHP-FPM ownership setups. The path is now only resolved to an absolute one; the directory is created by the backend that actually uses it.
|
Hi @lbajsarowicz. Thank you for your contribution!
Allowed build names are:
You can find more information about the builds here For more details, review the Code Contributions documentation. |
5 tasks
Contributor
Author
|
@magento run all tests |
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description (*)
When every cache frontend is configured with a non-file backend (e.g.
'backend' => 'redis'for bothdefaultandpage_cache), Magento still creates an emptyvar/page_cachedirectory on everycache:flush,setup:upgradeor first request. On deployments where CLI commands run as a deploy user and PHP-FPM aswww-data, PHP-FPM then cannot write into that directory and the storefront fails with HTTP 500 before the logger is available.Root cause.
app/etc/di.xmlhardcodesbackend_options.cache_dir = page_cachefor thepage_cachefrontend, andPool::_getCacheSettings()intentionally keeps it when mergingenv.php(#22228).Factory::resolveCacheDir()then callsgetDirectoryWrite(VAR_DIR)->create($path)unconditionally — before the backend type is resolved — so the directory is created for a Redis frontend that never uses it.Fix (minimal, backwards-compatible).
Factory::resolveCacheDir()no longer creates the directory; it only resolves the relativecache_dirto an absolute path undervar/. The backend that actually uses the path creates it on first use: SymfonyFilesystemAdapter::init()doesmkdir(..., 0777, true), andSymfonyAdapterProvider::createFilesystemAdapter()already creates the defaultvar/cache. File backends therefore behave exactly as before; Redis/Valkey/Memcached/Database/APCu frontends leavevar/untouched.This is the smallest change that fixes the report. An alternative that makes the decision explicit in the factory (create the directory only when the resolved backend is file-based, with the check living in
SymfonyAdapterProvider) is proposed in the linked PR — maintainers may prefer either; they are mutually exclusive.Related Pull Requests
Fixed Issues (if relevant)
Manual testing scenarios (*)
app/etc/env.phpwith'backend' => 'redis'for bothcache.frontend.defaultandcache.frontend.page_cache(bin/magento setup:config:set --cache-backend=redis --page-cache=redis ...).var/page_cacheandvar/cacheif present.bin/magento cache:flushand open any storefront page.var/page_cachedirectory is created. After the fix: neithervar/page_cachenorvar/cacheexists.page_cacheback to'backend' => 'file', runbin/magento cache:flushand open a storefront page:var/page_cache/<prefix>/...is created and populated as before.Questions or comments
Only
Factory::resolveCacheDir()changes;_getBackendOptions()still createsvar/cachefor the default file backend and for the local level ofremote_synchronized_cache.Contribution checklist (*)