From 367870d2483a95df545e6b7c42ded1e32d066480 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Fri, 20 Nov 2026 23:50:20 +0100 Subject: [PATCH 1/2] fix: stop persisting routes service in worker mode (hostname/subdomain route options) --- app/Config/WorkerMode.php | 8 ++++-- docs/changelogs/v4.7.7.md | 1 + docs/installation/upgrade_477.md | 38 +++++++++++++++++++++++++- docs/installation/worker_mode.md | 11 +++++--- tests/system/Config/WorkerModeTest.php | 36 ++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 tests/system/Config/WorkerModeTest.php diff --git a/app/Config/WorkerMode.php b/app/Config/WorkerMode.php index fd8b49434332..a134674f5f95 100644 --- a/app/Config/WorkerMode.php +++ b/app/Config/WorkerMode.php @@ -24,9 +24,14 @@ class WorkerMode * - `commands`: CLI commands registry * - `codeigniter`: Main application instance * - `superglobals`: Superglobals wrapper - * - `routes`: Router configuration * - `cache`: Cache instance * + * The `routes` service is intentionally not persistent. `RouteCollection` + * reads the request host and drops the routes limited with the `hostname` + * or `subdomain` options while the route file is loaded. Persisting it + * would make those decisions once, from the first request the worker + * served, so route definitions are loaded again for each request. + * * @var list */ public array $persistentServices = [ @@ -36,7 +41,6 @@ class WorkerMode 'commands', 'codeigniter', 'superglobals', - 'routes', 'cache', ]; diff --git a/docs/changelogs/v4.7.7.md b/docs/changelogs/v4.7.7.md index 01419ee93694..32da40abc0fc 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -50,5 +50,6 @@ Release Date: Unreleased - **Session:** Fixed a bug where `MemcachedHandler` did not reset lock state in `close()` or release an existing lock for a different session ID in `lockSession()`, causing subsequent reads and session ID regeneration on the same handler instance to fail. - **Session:** Fixed a bug where `Session::start()` did not remove an invalid session cookie from `Request`'s cached cookie global if cookies had already been read from `Request`. - **Worker Mode:** Fixed a bug where every request that ended with an uncaught exception, including a `404 Page Not Found` without a 404 override, called `exit()` in the exception handler. This ended the worker script, so FrankenPHP booted a new one, and the worker loop skipped its cleanup after the request. The exception handler now returns to the worker loop. +- **Worker Mode:** Fixed a bug where routes limited with the `hostname` or `subdomain` options were matched against the first request the worker served, so a host-restricted route became reachable from any host, and a route limited to another host stayed unreachable, until the worker restarted. The `routes` service is no longer persistent by default, so route definitions are loaded for each request. See [Worker Mode Routes Are Reloaded Per Request](../installation/upgrade_477.md#worker-mode-routes-are-reloaded-per-request) for migration instructions. See the repo's [CHANGELOG.md](https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md) for a complete list of bugs fixed. diff --git a/docs/installation/upgrade_477.md b/docs/installation/upgrade_477.md index bfa87dc80d72..4cf586e4baae 100644 --- a/docs/installation/upgrade_477.md +++ b/docs/installation/upgrade_477.md @@ -18,6 +18,41 @@ With the default configuration most applications are unaffected, because `/`, `\ ## Breaking Enhancements +## Behavior Changes + +### Worker Mode Routes Are Reloaded Per Request + +`Config\WorkerMode::$persistentServices` no longer contains `routes`. + +`RouteCollection` reads the request host while it loads the route definitions, and drops the routes limited with the `hostname` or `subdomain` options that do not match the current request. While the routes service was persistent, those decisions were made once, from the first request the worker served, and were kept for the lifetime of the process. A route limited to a hostname then became reachable from any `Host` after that first request, and a route that lost the first decision stayed unreachable until the worker restarted. The routes service is not persistent anymore, so the route definitions are loaded for each request and the host is evaluated on every request, as in traditional PHP. + +A new installation gets the updated default. Existing installations have their own copy of **app/Config/WorkerMode.php** in the project space, so it is not changed on upgrade. If your application uses the `hostname` or `subdomain` route options in Worker Mode, remove `routes` from `$persistentServices`: + +```php + + */ + public array $persistentServices = [ + 'autoloader', + 'locator', + 'exceptions', + 'commands', + 'codeigniter', + 'superglobals', + 'cache', + ]; +} +``` + +See [Persistent Services](worker_mode.md#persistent-services) for details. + ## Project Files Some files in the **project space** (root, app, public, writable) received updates. Due to these files being outside of the **system** scope they will not be changed without your intervention. @@ -31,7 +66,8 @@ The following files received significant changes (including deprecations or visu #### Config -- @TODO +- **app/Config/WorkerMode.php** + - `Config\WorkerMode::$persistentServices` no longer contains `routes`. See [Worker Mode Routes Are Reloaded Per Request](#worker-mode-routes-are-reloaded-per-request). ### All Changes diff --git a/docs/installation/worker_mode.md b/docs/installation/worker_mode.md index b00fca047ca7..b1d0251f28f6 100644 --- a/docs/installation/worker_mode.md +++ b/docs/installation/worker_mode.md @@ -33,10 +33,11 @@ In Worker Mode, the lifecycle changes dramatically: 1. Worker process starts and performs one-time initialization: - Loads and parses all required files (cached in OPcache) - - Bootstraps framework: autoloader, configuration, services, routes + - Bootstraps framework: autoloader, configuration, services - Establishes database and cache connections 2. For each incoming request: - Reuses existing connections and cached resources + - Reloads the route definitions, so the routes limited with the `hostname` or `subdomain` options are matched against the current request - Resets only request-specific state (superglobals, request/response objects) - Processes request and sends response - Cleans up request-specific data @@ -118,7 +119,7 @@ Worker Mode typically provides **2-3x performance improvements** for database-dr |----|----| | **Simple endpoints** | Applications returning minimal JSON responses show modest improvements (10-30%), as there's little bootstrap overhead to eliminate. | | **Database queries** | Endpoints performing database queries typically see **2-3x** improvements from connection reuse and reduced initialization overhead. | -| **Complex bootstrap** | Applications with many services, routes, or configuration see the largest gains, as this overhead is eliminated entirely. | +| **Complex bootstrap** | Applications with many services or configuration see the largest gains, as this overhead is eliminated entirely. | ### Worker Count Considerations @@ -136,7 +137,7 @@ All worker mode configuration is managed through the **app/Config/WorkerMode.php | Option | Type | Description | |----|----|----| -| **$persistentServices** | array | Services that persist across requests and are not reset. Services not in this list are destroyed after each request to prevent state leakage. Default: `['autoloader', 'locator', 'exceptions', 'commands', 'codeigniter', 'superglobals', 'routes', 'cache']` | +| **$persistentServices** | array | Services that persist across requests and are not reset. Services not in this list are destroyed after each request to prevent state leakage. Default: `['autoloader', 'locator', 'exceptions', 'commands', 'codeigniter', 'superglobals', 'cache']` | | **$resetEventListeners** | array | Event names whose listeners are removed between requests. Use this when you register event listeners inside other event callbacks rather than at the top level of **Config/Events.php**, which would cause them to accumulate across requests. Default: `[]` | | **$forceGarbageCollection** | bool | Whether to force garbage collection after each request. `true` (default, recommended): Prevents memory leaks. `false`: Relies on PHP's automatic garbage collection. | @@ -152,9 +153,11 @@ The `$persistentServices` array controls which services survive between requests | `commands` | CLI commands registry. Only used during worker startup. | | `codeigniter` | Main application instance. Orchestrates the request/response cycle. | | `superglobals` | Superglobals wrapper. Properly isolated per request internally. | -| `routes` | Router configuration. Route definitions don't change between requests. | | `cache` | Cache service. Maintains connections to cache backends (Redis, Memcached). | +!!! note "Note" + The `routes` service is **not** persistent by default. `RouteCollection` reads the request host while it loads the route definitions, and drops the routes limited with the `hostname` or `subdomain` options that do not match the current request. A persistent route collection would keep the decision made for the first request the worker served, so the routes restricted to a hostname would be reachable from any host, or not reachable at all, until the worker restarted. The route definitions are therefore loaded again for each request. The cost is small compared with the bootstrap work Worker Mode already removes, and it keeps the route behavior identical to traditional PHP. + !!! warning "Warning" Adding services to `$persistentServices` without understanding their state management can cause data leakage between requests. Only persist services that are truly stateless or manage their own request isolation. diff --git a/tests/system/Config/WorkerModeTest.php b/tests/system/Config/WorkerModeTest.php new file mode 100644 index 000000000000..60065c5177a1 --- /dev/null +++ b/tests/system/Config/WorkerModeTest.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Config; + +use CodeIgniter\Test\CIUnitTestCase; +use Config\WorkerMode; +use PHPUnit\Framework\Attributes\Group; + +/** + * @internal + */ +#[Group('Others')] +final class WorkerModeTest extends CIUnitTestCase +{ + public function testRoutesServiceIsNotPersistentByDefault(): void + { + $config = new WorkerMode(); + + $this->assertNotContains( + 'routes', + $config->persistentServices, + 'The routes service must not be persistent, or the hostname/subdomain route options are decided once per worker process instead of once per request.', + ); + } +} From bdd863385bedffb18efb553a78610ebf7abc50dc Mon Sep 17 00:00:00 2001 From: Bogdan Date: Sun, 22 Nov 2026 21:53:32 +0100 Subject: [PATCH 2/2] docs: apply review feedback on worker mode route persistence - Remove the duplicated routes explanation from the Config\WorkerMode::$persistentServices docblock. - Drop the Behavior Changes section from the 4.7.7 upgrade guide. - Shorten the Config bullet and link it to Persistent Services instead. - Move the route reload bullet after the request state reset and drop an unmeasured cost claim in worker_mode.md. - Retarget the 4.7.7 changelog Worker Mode link to Persistent Services. --- app/Config/WorkerMode.php | 6 ------ docs/changelogs/v4.7.7.md | 2 +- docs/installation/upgrade_477.md | 37 +------------------------------- docs/installation/worker_mode.md | 4 ++-- 4 files changed, 4 insertions(+), 45 deletions(-) diff --git a/app/Config/WorkerMode.php b/app/Config/WorkerMode.php index a134674f5f95..3eeba0b58ea7 100644 --- a/app/Config/WorkerMode.php +++ b/app/Config/WorkerMode.php @@ -26,12 +26,6 @@ class WorkerMode * - `superglobals`: Superglobals wrapper * - `cache`: Cache instance * - * The `routes` service is intentionally not persistent. `RouteCollection` - * reads the request host and drops the routes limited with the `hostname` - * or `subdomain` options while the route file is loaded. Persisting it - * would make those decisions once, from the first request the worker - * served, so route definitions are loaded again for each request. - * * @var list */ public array $persistentServices = [ diff --git a/docs/changelogs/v4.7.7.md b/docs/changelogs/v4.7.7.md index 32da40abc0fc..c4ce84f9c737 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -50,6 +50,6 @@ Release Date: Unreleased - **Session:** Fixed a bug where `MemcachedHandler` did not reset lock state in `close()` or release an existing lock for a different session ID in `lockSession()`, causing subsequent reads and session ID regeneration on the same handler instance to fail. - **Session:** Fixed a bug where `Session::start()` did not remove an invalid session cookie from `Request`'s cached cookie global if cookies had already been read from `Request`. - **Worker Mode:** Fixed a bug where every request that ended with an uncaught exception, including a `404 Page Not Found` without a 404 override, called `exit()` in the exception handler. This ended the worker script, so FrankenPHP booted a new one, and the worker loop skipped its cleanup after the request. The exception handler now returns to the worker loop. -- **Worker Mode:** Fixed a bug where routes limited with the `hostname` or `subdomain` options were matched against the first request the worker served, so a host-restricted route became reachable from any host, and a route limited to another host stayed unreachable, until the worker restarted. The `routes` service is no longer persistent by default, so route definitions are loaded for each request. See [Worker Mode Routes Are Reloaded Per Request](../installation/upgrade_477.md#worker-mode-routes-are-reloaded-per-request) for migration instructions. +- **Worker Mode:** Fixed a bug where routes limited with the `hostname` or `subdomain` options were matched against the first request the worker served, so a host-restricted route became reachable from any host, and a route limited to another host stayed unreachable, until the worker restarted. The `routes` service is no longer persistent by default, so route definitions are loaded for each request. See [Persistent Services](../installation/worker_mode.md#persistent-services) for details. See the repo's [CHANGELOG.md](https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md) for a complete list of bugs fixed. diff --git a/docs/installation/upgrade_477.md b/docs/installation/upgrade_477.md index 4cf586e4baae..473683c9a186 100644 --- a/docs/installation/upgrade_477.md +++ b/docs/installation/upgrade_477.md @@ -18,41 +18,6 @@ With the default configuration most applications are unaffected, because `/`, `\ ## Breaking Enhancements -## Behavior Changes - -### Worker Mode Routes Are Reloaded Per Request - -`Config\WorkerMode::$persistentServices` no longer contains `routes`. - -`RouteCollection` reads the request host while it loads the route definitions, and drops the routes limited with the `hostname` or `subdomain` options that do not match the current request. While the routes service was persistent, those decisions were made once, from the first request the worker served, and were kept for the lifetime of the process. A route limited to a hostname then became reachable from any `Host` after that first request, and a route that lost the first decision stayed unreachable until the worker restarted. The routes service is not persistent anymore, so the route definitions are loaded for each request and the host is evaluated on every request, as in traditional PHP. - -A new installation gets the updated default. Existing installations have their own copy of **app/Config/WorkerMode.php** in the project space, so it is not changed on upgrade. If your application uses the `hostname` or `subdomain` route options in Worker Mode, remove `routes` from `$persistentServices`: - -```php - - */ - public array $persistentServices = [ - 'autoloader', - 'locator', - 'exceptions', - 'commands', - 'codeigniter', - 'superglobals', - 'cache', - ]; -} -``` - -See [Persistent Services](worker_mode.md#persistent-services) for details. - ## Project Files Some files in the **project space** (root, app, public, writable) received updates. Due to these files being outside of the **system** scope they will not be changed without your intervention. @@ -67,7 +32,7 @@ The following files received significant changes (including deprecations or visu #### Config - **app/Config/WorkerMode.php** - - `Config\WorkerMode::$persistentServices` no longer contains `routes`. See [Worker Mode Routes Are Reloaded Per Request](#worker-mode-routes-are-reloaded-per-request). + - The default `$persistentServices` no longer contains `routes`. Existing installations keep their own copy of this file. If you use the `hostname` or `subdomain` route options in Worker Mode, remove `routes` from `$persistentServices` so routes are reloaded for each request. See [Persistent Services](worker_mode.md#persistent-services). ### All Changes diff --git a/docs/installation/worker_mode.md b/docs/installation/worker_mode.md index b1d0251f28f6..1f8b13eee180 100644 --- a/docs/installation/worker_mode.md +++ b/docs/installation/worker_mode.md @@ -37,8 +37,8 @@ In Worker Mode, the lifecycle changes dramatically: - Establishes database and cache connections 2. For each incoming request: - Reuses existing connections and cached resources - - Reloads the route definitions, so the routes limited with the `hostname` or `subdomain` options are matched against the current request - Resets only request-specific state (superglobals, request/response objects) + - Reloads the route definitions, so the routes limited with the `hostname` or `subdomain` options are matched against the current request - Processes request and sends response - Cleans up request-specific data 3. Worker continues running, ready for the next request @@ -156,7 +156,7 @@ The `$persistentServices` array controls which services survive between requests | `cache` | Cache service. Maintains connections to cache backends (Redis, Memcached). | !!! note "Note" - The `routes` service is **not** persistent by default. `RouteCollection` reads the request host while it loads the route definitions, and drops the routes limited with the `hostname` or `subdomain` options that do not match the current request. A persistent route collection would keep the decision made for the first request the worker served, so the routes restricted to a hostname would be reachable from any host, or not reachable at all, until the worker restarted. The route definitions are therefore loaded again for each request. The cost is small compared with the bootstrap work Worker Mode already removes, and it keeps the route behavior identical to traditional PHP. + The `routes` service is **not** persistent by default. `RouteCollection` reads the request host while it loads the route definitions, and drops the routes limited with the `hostname` or `subdomain` options that do not match the current request. A persistent route collection would keep the decision made for the first request the worker served, so the routes restricted to a hostname would be reachable from any host, or not reachable at all, until the worker restarted. The route definitions are therefore loaded again for each request, as in traditional PHP. !!! warning "Warning" Adding services to `$persistentServices` without understanding their state management can cause data leakage between requests. Only persist services that are truly stateless or manage their own request isolation.