-
-
Notifications
You must be signed in to change notification settings - Fork 643
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 file not always loading #248
Comments
Same here, for some reason, once in a while the file is not read and I get message |
@RickKukiela What operating system are you using? What is your environment like (apache/nginx, php loaded as module/php-fpm)? |
@tomzx Windows 10, easyphp devserver (no docker / virtual machine setup), mariadb running locally, php 7.1 Its apache 2.4 / php 7.1 - I think its just installed as a regular module not php-fpm I have this same setup on multiple machines and only the old slow machine exhibits this behavior. |
On my side: Solaris, PHP 5.6 Apache 2.4 with mod_php. |
This is caused by putenv() and getenv() functions in PHP. This library uses it to define if env variable already exists and if not - do nothing. I tested it, and I realize that $_ENV variable was filled with values from .env file, but getenv() function always return null. Maybe there will be a good idea, to add some configuration, to define if the variables from the .env file should be placed only in $_ENV variable, or both in $_ENV and putenv(). And with getting values the same. This should fix the problem, and wasn't cause backwards compatibility. Sure, You can overwrite values, but sometimes we don't want to do this, and described solution can works. |
I'm experiencing the same issue when I run multiple concurrent ajax resquests. My .env file has 15 lines (only 6 of them are variables) but in some of the concurrent requests, my I'm running on Apache 2.4.29 with PHP 5.6.33 |
I create my own function env() (like PHP's built-in getenv()), and in this function I use only _$ENV global variable to get values. Now, I don't have any problems, but this thing should be fixed in the Library. |
Believe it or not, using $_ENV didn't do the trick for me. It was weird because only in one API endpoint the env file wasn't loading, and in all the other endpoints it was. Probably the error triggers when a lot of requests go to the server at the same time. I was able to solve it by recursively trying to load the env file.
|
Duplicate of #219. |
Putenv and Getenv are not thread-safe. This is probably the cause of your issue. What happens is the following: |
Yes, this library cannot be used in a multi-threaded environment. The issue is well-described in the link I posted. :) |
I think it's likely the following example will work in a multithreaded environment. This avoids using the adapter that would have called <?php
use Dotenv\Environment\Adapter\EnvConstAdapter;
use Dotenv\Environment\Adapter\ServerConstAdapter;
use Dotenv\Environment\DotenvFactory;
use Dotenv\Dotenv;
$factory = new DotenvFactory([new EnvConstAdapter(), new ServerConstAdapter()]);
Dotenv::create($path, null, $factory)->load(); |
I've tried this solution and it did solve the issue. |
There's no work around for packages that use a function that isn't threadsafe. They should instead update their code to be threadsafe. The |
I'm using craft3 beta which uses this package to load the config data.
On my main computer its a core i7 (2016 model) with 24gb RAM and a M.2 SSD etc. I do not have any problems loading .env every time.
Our other PC has a a regular SSD, Core2 Quad 2.4ghz and 8gb of RAM
On the other computer every once in a while the .env data fails to load, which causes craft3 to crash due to the database driver value being empty because .env did not load.
Simply refreshing the page causes the error to go away for a while but it always comes back.
An exact replication of that environment does not bug on my PC at all. Aside from the speed difference, the only other difference is the problem computer is running PHP 7.1.3 and I'm running PHP 7.1.0
The text was updated successfully, but these errors were encountered: