Skip to content

perf(Config): retain stateless config instances in Worker Mode - #10725

Closed
gr8man wants to merge 1 commit into
codeigniter4:developfrom
gr8man:perf/worker-persistent-factories
Closed

gr8man wants to merge 1 commit into
codeigniter4:developfrom
gr8man:perf/worker-persistent-factories

Conversation

@gr8man

@gr8man gr8man commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Description
In FrankenPHP Worker Mode, Factories::reset() was executed after every request, destroying all shared instances including Config\* objects (Config\App, Config\Database, Config\Security, etc.). This forced each subsequent request in the worker lifecycle to rediscover, re-instantiate, and re-parse environment variables for stateless configuration classes.

This PR introduces selective factory retention:

  • Added Factories::resetExcept(array $components): void to selectively reset factories while preserving specified components.
  • Added Factories::resetForWorkerMode(?WorkerMode $config = null): void to reset factories between requests while retaining persistent components configured in $persistentFactories.
  • Added $persistentFactories property to Config\WorkerMode (defaulting to ['config']).
  • Updated frankenphp-worker.php.tpl to call Factories::resetForWorkerMode($workerConfig).
  • Updated Worker Mode documentation, upgrading guide, and changelog.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

- add Factories::resetExcept() to selectively clear factory components
- add Factories::resetForWorkerMode() preserving persistent factories
- add $persistentFactories property to Config\WorkerMode (default: ['config'])
- update frankenphp-worker.php.tpl to call Factories::resetForWorkerMode($workerConfig)
- update Worker Mode documentation, upgrading guide, and changelog
- add unit tests covering selective reset, worker mode persistence, and state isolation
@carson-codeigniter4 carson-codeigniter4 Bot added the enhancement PRs that improve existing functionalities label Nov 4, 2026

@michalsn michalsn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can leak configuration between requests. Config properties are mutable, and Registrars may return request-specific values. In Worker Mode, a later request could receive a config instance initialized for a previous client, without running its Registrar again. Our existing warning against Config Caching in Worker Mode reflects this risk. File Locator Caching is also disabled because of stale data concerns.

@gr8man

gr8man commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for pointing this out. You are completely right — I overlooked that Config instances in CI4 do not enforce immutability and that third-party Registrars can be request-dependent. Given the existing warning against Config Caching in Worker Mode and the risk of cross-request state leakage, keeping Config instances persistent across requests is indeed unsafe for the framework.

I am closing this PR. Thank you for the thorough review and explanation!

@gr8man gr8man closed this Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement PRs that improve existing functionalities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants