You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PR #59136 and #54666 added the ability to reset env vars. In theory it works but in practice it doesn't since all env var loaders are loaded way too early because many symfony services define them dynamically, for example, these env vars APP_RUNTIME_MODE, APP_RUNTIME_ENV, VAR_DUMPER_SERVER, maybe even more.
As such I suggest EnvVarLoaders to define the env keys that it will provide a value for, thereby, preventing loading them unnecessarily.
How to reproduce
Create a new symfony project, define an EnvVarLoader which loads an env var whose value can change based on certain conditions like tenant id, use the env in your messenger handler, it will always use the first value since all env var loaders are loaded very early in the process, even before a messenger message is handled.
Possible Solution
EnvVarLoaders should be loaded on demand, they should define the keys that they can provide values for.
and EnvProcessor::getEnv shouldn't go through all EnvLoaders, it should only go through env loaders which can provide the value for current key.
For BC reasons envKeys cannot be added to EnvVarLoaderInterface but can possibly be added via phpdoc.
Additional Context
I've created a bug ticket instead of feature, because previous PRs aren't enough to make it work as expected even though the possible solution would lead to a new feature.
The text was updated successfully, but these errors were encountered:
faizanakram99
changed the title
[DependencyInjection] Env vars are not reset with kernel.reset
[DependencyInjection] Env vars are not reset with kernel.resetJun 13, 2025
Your use case looks broken to me. Resetting services will not remove the instances managed by the container. It is about resetting the state of stateful services.
Any scalar value injecting in the service constructor using a %env()% placeholder has no chance to be resolved again using a new value of an env variable, because the constructor is not used again.
If you need to deal with values that depend on the message being consumed, using an EnvVarLoader is the wrong implementation.
Actual environment variables cannot be changed for a process that is running (writing into $_ENV does not actually change the environment variables of a process. It only changes a special PHP variable that was initialized based on env variables), and custom EnvVarLoader are suited only for use cases that follow a similar lifecycle.
Symfony version(s) affected
7.3
Description
PR #59136 and #54666 added the ability to reset env vars. In theory it works but in practice it doesn't since all env var loaders are loaded way too early because many symfony services define them dynamically, for example, these env vars APP_RUNTIME_MODE, APP_RUNTIME_ENV, VAR_DUMPER_SERVER, maybe even more.
As such I suggest EnvVarLoaders to define the env keys that it will provide a value for, thereby, preventing loading them unnecessarily.
How to reproduce
Create a new symfony project, define an EnvVarLoader which loads an env var whose value can change based on certain conditions like tenant id, use the env in your messenger handler, it will always use the first value since all env var loaders are loaded very early in the process, even before a messenger message is handled.
Possible Solution
EnvVarLoaders should be loaded on demand, they should define the keys that they can provide values for.
Something like or maybe even an attribute
and
EnvProcessor::getEnv
shouldn't go through all EnvLoaders, it should only go through env loaders which can provide the value for current key.For BC reasons
envKeys
cannot be added toEnvVarLoaderInterface
but can possibly be added via phpdoc.Additional Context
I've created a bug ticket instead of feature, because previous PRs aren't enough to make it work as expected even though the possible solution would lead to a new feature.
The text was updated successfully, but these errors were encountered: