diff --git a/app/Config/WorkerMode.php b/app/Config/WorkerMode.php index fd8b49434332..3eeba0b58ea7 100644 --- a/app/Config/WorkerMode.php +++ b/app/Config/WorkerMode.php @@ -24,7 +24,6 @@ class WorkerMode * - `commands`: CLI commands registry * - `codeigniter`: Main application instance * - `superglobals`: Superglobals wrapper - * - `routes`: Router configuration * - `cache`: Cache instance * * @var list @@ -36,7 +35,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..c4ce84f9c737 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 [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 bfa87dc80d72..473683c9a186 100644 --- a/docs/installation/upgrade_477.md +++ b/docs/installation/upgrade_477.md @@ -31,7 +31,8 @@ The following files received significant changes (including deprecations or visu #### Config -- @TODO +- **app/Config/WorkerMode.php** + - 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 b00fca047ca7..1f8b13eee180 100644 --- a/docs/installation/worker_mode.md +++ b/docs/installation/worker_mode.md @@ -33,11 +33,12 @@ 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 - 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 @@ -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, 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. 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.', + ); + } +}