Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

env() function can fail under high server load due to thread safety issues #9

Open
anthony-n-brown opened this issue Nov 17, 2021 · 0 comments

Comments

@anthony-n-brown
Copy link

I stumbled upon an issue in my environment where calls to env() would intermittently fail to return the expected value.

Ultimately, I root caused this to the use of getenv() within that function, which is not thread safe in PHP. Under high server load scenarios where multiple requests are being processed concurrently, I can consistently reproduce this.

I fixed this by using the following:

general_helper.php

Avoid relying on getenv() function and use one of the two other methods exposed by Dotenv.

Either:

// $value = getenv($key);
$value = $_SERVER[$key] ?? false;

Or:

// $value = getenv($key);
$value = $_ENV[$key] ?? false;

Env.php

Once the above change is made, you would no longer need the unsafe version of dotenv's create functions.

// $dotenv = Dotenv\Dotenv::createUnsafeImmutable(FCPATH);
$dotenv = Dotenv\Dotenv::createImmutable(FCPATH)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant