Reset Webapi\Request state between requests (#1 in frankenphp-base)#2
Open
acourtiol wants to merge 1 commit into
Open
Reset Webapi\Request state between requests (#1 in frankenphp-base)#2acourtiol wants to merge 1 commit into
acourtiol wants to merge 1 commit into
Conversation
…p-base#1)
Closes the worker-mode state-leak vector in `Magento\Framework\Webapi\Request`
(REST + SOAP request object).
What's leaking and why
----------------------
`Magento\Framework\Webapi\Request` extends `Magento\Framework\HTTP\PhpEnvironment\Request`
(which extends `Laminas\Http\PhpEnvironment\Request`). The parent chain holds
per-request state on protected fields: $module, $controller, $action,
$pathInfo, $requestString, $params, $aliases, $dispatched, $forwarded,
$baseUrl, $basePath, $requestUri, $method, $headers, $metadata, $content,
and more.
`Magento\Framework\App\Request\Http` (the SIBLING class used for the
/index.php path) implements ResetAfterRequestInterface and resets all of
those fields between requests. But `Webapi\Request` does NOT implement
the interface — neither does the shared parent. So in worker mode:
- /index.php requests stay clean across the worker's lifetime.
- /rest/V1/... and /soap/... requests on the same worker inherit
state from previous Webapi requests on that worker.
This has historically been the source of intermittent "wrong store",
"wrong area", "auth context leaks" reports under Swoole / RoadRunner /
FrankenPHP worker setups.
The fix
-------
Add `Opengento\Application\Webapi\Request` — a subclass that implements
ResetAfterRequestInterface with a `_resetState()` body mirroring
`Magento\Framework\App\Request\Http::_resetState()`. Both classes have
the same parent and the same per-request fields, so the same reset body
applies cleanly.
Wired in via etc/di.xml preference so DI consumers of
`Magento\Framework\Webapi\Request` receive our subclass transparently.
Compatibility
-------------
`Magento\Framework\Webapi\Request` is byte-identical between Magento
2.4.7 and 2.4.9-beta1 (Copyright header aside). The protected fields
we touch are declared by the shared `HTTP\PhpEnvironment\Request` parent
and are stable across the 2.4.x line.
Scope notes for review
----------------------
This is the most concrete "Laminas Http patch" candidate I identified
for issue opengento/magento2-frankenphp-base#1 ("Add Laminas Http
Patch"). There are other Laminas-extending Magento classes that may
deserve similar treatment (`Magento\Framework\HTTP\Client\*` outbound
HTTP clients hold cookies/auth across requests in worker mode), but
those are out of scope for this PR — happy to expand on follow-up.
acourtiol
added a commit
to acourtiol/magento2-application
that referenced
this pull request
May 11, 2026
Adds Opengento\Application\App\Media — the missing third worker-mode entry point alongside App\Http (pub/index.php) and App\StaticResource (pub/static.php). Closes the get.php gap that's currently behind two open issues on magento2-frankenphp-base: - opengento#2 "Product Image Cache generation failure" (PLP/PDP image 404s) - #3 "Make all entry points benefit of shared bootstrap" How it works ------------ App\Media implements AppInterface. Per request, it: 1. Snapshots State::$_areaCode (via reflection — see "Why reflection") 2. Clears it 3. Constructs Magento\MediaStorage\App\Media via the worker's shared ObjectManager with request-derived (mediaDirectory, configCacheFile, isAllowed, relativeFileName) arguments — same contract stock pub/get.php uses 4. Delegates to MagentoMedia::launch() (which sets area to GLOBAL internally, materializes the file, returns the FileResponse) 5. In finally{}, restores the original area code so the next /index.php or /static.php request on the same worker sees the area BootstrapPool set up for it The Magento\MediaStorage\App\Media constructor signature is byte-identical between 2.4.8-p4 and 2.4.9-beta1 (verified by diffing both sources), so this layer is dual-version compatible. Why reflection -------------- MagentoMedia::launch() calls State::setAreaCode(AREA_GLOBAL) unconditionally on every invocation. In worker mode the State singleton persists across requests and State::setAreaCode() throws "Area code is already set" on the second call — State doesn't implement ResetAfterRequestInterface (and isn't auto-tracked by AppObjectManager's Resetter since it's resolved via get(), not create()), so the Resetter never clears it between requests. State exposes no public reset API: setAreaCode throws, emulateAreaCode wraps and restores, getAreaCode throws when unset. Reflection on the private _areaCode field is the smallest contained workaround. The save+restore is in a finally{} so a media-app failure can't leave the worker in a broken area state for other endpoints. If/when Magento adds ResetAfterRequestInterface to State (or exposes a clear() method), the reflection here can be deleted. composer.json ------------- Also bumps require constraints to match the "PHP 8.4+, Magento 2.4.8+" support matrix: - php: ^8.3 -> ^8.4 - magento/framework: * -> ^103.0 (covers 2.4.8 + 2.4.9 dev) - magento/module-media-storage: * -> ^100.4 - psr/log: * -> ^3.0
014ff83 to
aef9323
Compare
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.
Addresses the worker-mode state-leak vector in
Magento\Framework\Webapi\Request— the most concrete "Laminas Http patch" candidate I identified while looking at opengento/magento2-frankenphp-base#1.What's leaking and why
Magento\Framework\Webapi\RequestextendsMagento\Framework\HTTP\PhpEnvironment\Request(which extendsLaminas\Http\PhpEnvironment\Request). The parent chain holds per-request state on protected fields:$module,$controller,$action,$pathInfo,$requestString,$params,$aliases,$dispatched,$forwarded,$baseUrl,$basePath,$requestUri,$method,$headers,$metadata,$content, and more.Magento\Framework\App\Request\Http(the sibling class used for the/index.phppath) implementsResetAfterRequestInterfaceand resets all of those fields between requests. ButWebapi\Requestdoesn't implement the interface — and neither does the shared parent.So in worker mode:
/index.phprequests stay clean across the worker's lifetime./rest/V1/...and/soap/...requests on the same worker inherit state from previous Webapi requests on that worker.This is historically the source of intermittent "wrong store", "wrong area", "auth context leaks" reports under long-running PHP runtimes (Swoole / RoadRunner / FrankenPHP worker).
The fix
App/Webapi/Request.php— subclass that implementsResetAfterRequestInterfacewith a_resetState()body mirroringMagento\Framework\App\Request\Http::_resetState(). Same parent, same per-request fields, same reset body applies cleanly.Wired via
etc/di.xmlpreference so all DI consumers ofMagento\Framework\Webapi\Requestget our subclass.Compatibility
Magento\Framework\Webapi\Requestis byte-identical between Magento 2.4.7 and 2.4.9-beta1 (Copyright header aside). The protected fields we touch are declared by the sharedHTTP\PhpEnvironment\Requestparent and are stable across the 2.4.x line.Scope notes for review
This is the most concrete state-leak vector I identified. There are other Laminas-extending Magento classes that may deserve similar treatment:
Magento\Framework\HTTP\Client\*— outbound HTTP clients hold cookies / auth state across requests.HTTP\PhpEnvironment\Requestwithout implementingResetAfterRequestInterface.Those are out of scope for this PR; happy to expand on a follow-up.
Stacking
Independent of PR #1 (App\Media) — different files, no overlap.