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 file not always being loaded #28571

Closed
Garanaw opened this issue May 19, 2019 · 13 comments
Closed

.env file not always being loaded #28571

Garanaw opened this issue May 19, 2019 · 13 comments

Comments

@Garanaw
Copy link

Garanaw commented May 19, 2019

  • Laravel Version: 5.8.9
  • PHP Version: 7.2.4
  • Database Driver & Version: MySQL 5.7.21

Description:

When doing several ajax requests the .env file is not always being pulled.

Steps To Reproduce:

Using Vue with Axios:

  1. Create a button to pull some result from the server
  2. When received, send a new request through Axios to get relations for the previous result

NOTE: This does NOT happen every single time, it happens randomly.

Code for Ajax Requests:

getCountries() {
    axios.get('/api/countries').then(result => {
        this.getLanguages(result.data.id);
    }).catch(error => console.log(error) );
},
getLanguages(countryId) {
    axios.get('/api/countries/'+countryId+'/languages').then(result => {
        // do whatever with it
    }).catch(error => {
        let keys = Object.keys(error);
        keys.forEach(key => console.log(error[key]) );
    });
}

Code in .env file:

# Database
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_database
DB_USERNAME=myUsername
DB_PASSWORD=hereMyPassword

Code in config/database.php

return [
    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => 'InnoDB',
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ],
]

Result from the second Ajax request (getLanguages)

"SQLSTATE[HY000] [1045] Access denied for user 'forge'@'localhost' (using password: NO) (SQL: select * from `users` where `id` = 1 and `users`.`deleted_at` is null limit 1)"

method: "get"
timeout: 0
​
readyState: 4
​
response: "{\n    \"message\": \"SQLSTATE[HY000] [1045] Access denied for user 'forge'@'localhost' (using password: NO) (SQL: select * from `users` where `id` = 1 and `users`.`deleted_at` is null limit 1)\" ... "
responseType: ""
​
responseXML: null
​
status: 500
​
statusText: "Internal Server Error"
​
timeout: 0
​
withCredentials: false
​
exception: "Illuminate/Database/QueryException"
​​
file: "/var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php"
​​
line: 664
​​
message: "SQLSTATE[HY000] [1045] Access denied for user 'forge'@'localhost' (using password: NO) (SQL: select * from `users` where `id` = 1 and `users`.`deleted_at` is null limit 1)"
​​
trace: (52) […]
​​​
0: {…}
class: "Illuminate/Database/Connection"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php"
function: "runQueryCallback"
line: 624
type: "->"
​​​
1: {…}
class: "Illuminate/Database/Connection"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php"
function: "run"
line: 333
type: "->"

2: {…}
class: "Illuminate/Database/Connection"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php"
function: "select"
line: 2120
type: "->"
​​​
3: {…}
class: "Illuminate/Database/Query/Builder"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php"
function: "runSelect"
line: 2108
type: "->"
​​​
4: {…}
class: "Illuminate/Database/Query/Builder"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php"
function: "Illuminate/Database/Query/{closure}"
line: 2594
type: "->"
​​​
5: {…}
class: "Illuminate/Database/Query/Builder"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php"
function: "onceWithColumns"
line: 2109
type: "->"
​​​
6: {…}
class: "Illuminate/Database/Query/Builder"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php"
function: "get"
line: 519
type: "->"
​​​
7: {…}
class: "Illuminate/Database/Eloquent/Builder"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php"
function: "getModels"
line: 503
type: "->"
​​​
8: {…}
class: "Illuminate/Database/Eloquent/Builder"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Database/Concerns/BuildsQueries.php"
function: "get"
line: 77
type: "->"
​​​
9: {…}
class: "Illuminate/Database/Eloquent/Builder"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php"
function: "first"
line: 52
type: "->"
​​​
10: {…}
class: "Illuminate/Auth/EloquentUserProvider"
file: "/var/www/vendor/laravel/passport/src/Guards/TokenGuard.php"
function: "retrieveById"
line: 199
type: "->"
​​​
11: {…}
class: "Laravel/Passport/Guards/TokenGuard"
file: "/var/www/vendor/laravel/passport/src/Guards/TokenGuard.php"
function: "authenticateViaCookie"
line: 91
type: "->"
​​​
12: {…}
class: "Laravel/Passport/Guards/TokenGuard"
file: "/var/www/vendor/laravel/passport/src/PassportServiceProvider.php"
function: "user"
line: 274
type: "->"
​​​
13: {…}
class: "Laravel/Passport/PassportServiceProvider"
function: "Laravel/Passport/{closure}"
type: "->"
​​​
14: {…}
file: "/var/www/vendor/laravel/framework/src/Illuminate/Auth/RequestGuard.php"
function: "call_user_func"
line: 58
​​​
15: {…}
class: "Illuminate/Auth/RequestGuard"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Auth/GuardHelpers.php"
function: "user"
line: 60
type: "->"
​​​
16: {…}
class: "Illuminate/Auth/RequestGuard"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php"
function: "check"
line: 62
type: "->"
​​​
17: {…}
class: "Illuminate/Auth/Middleware/Authenticate"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php"
function: "authenticate"
line: 41
type: "->"
​​​
18: {…}
class: "Illuminate/Auth/Middleware/Authenticate"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
function: "handle"
line: 163
type: "->"
​​​
19: {…}
class: "Illuminate/Pipeline/Pipeline"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php"
function: "Illuminate/Pipeline/{closure}"
line: 53
type: "->"
​​​
20: {…}
class: "Illuminate/Routing/Pipeline"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php"
function: "Illuminate/Routing/{closure}"
line: 58
type: "->"
​​​
21: {…}
class: "Illuminate/Routing/Middleware/ThrottleRequests"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
function: "handle"
line: 163
type: "->"
​​​
22: {…}
class: "Illuminate/Pipeline/Pipeline"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php"
function: "Illuminate/Pipeline/{closure}"
line: 53
type: "->"
​​​
23: {…}
class: "Illuminate/Routing/Pipeline"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
function: "Illuminate/Routing/{closure}"
line: 104
type: "->"
​​​
24: {…}
class: "Illuminate/Pipeline/Pipeline"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php"
function: "then"
line: 682
type: "->"
​​​
25: {…}
class: "Illuminate/Routing/Router"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php"
function: "runRouteWithinStack"
line: 657
type: "->"
​​​
26: {…}
class: "Illuminate/Routing/Router"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php"
function: "runRoute"
line: 623
type: "->"
​​​
27: {…}
class: "Illuminate/Routing/Router"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php"
function: "dispatchToRoute"
line: 612
type: "->"
​​​
28: {…}
class: "Illuminate/Routing/Router"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php"
function: "dispatch"
line: 176
type: "->"
​​​
29: {…}
class: "Illuminate/Foundation/Http/Kernel"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php"
function: "Illuminate/Foundation/Http/{closure}"
line: 30
type: "->"
​​​
30: {…}
class: "Illuminate/Routing/Pipeline"
file: "/var/www/vendor/barryvdh/laravel-debugbar/src/Middleware/InjectDebugbar.php"
function: "Illuminate/Routing/{closure}"
line: 65
type: "->"
​​​
31: {…}
class: "Barryvdh/Debugbar/Middleware/InjectDebugbar"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
function: "handle"
line: 163
type: "->"
​​​
32: {…}
class: "Illuminate/Pipeline/Pipeline"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php"
function: "Illuminate/Pipeline/{closure}"
line: 53
type: "->"
​​​
33: {…}
class: "Illuminate/Routing/Pipeline"
file: "/var/www/vendor/fideloper/proxy/src/TrustProxies.php"
function: "Illuminate/Routing/{closure}"
line: 57
type: "->"
​​​
34: {…}
class: "Fideloper/Proxy/TrustProxies"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
function: "handle"
line: 163
type: "->"
​​​
35: {…}
class: "Illuminate/Pipeline/Pipeline"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php"
function: "Illuminate/Pipeline/{closure}"
line: 53
type: "->"
​​​
36: {…}
class: "Illuminate/Routing/Pipeline"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php"
function: "Illuminate/Routing/{closure}"
line: 21
type: "->"
​​​
37: {…}
class: "Illuminate/Foundation/Http/Middleware/TransformsRequest"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
function: "handle"
line: 163
type: "->"
​​​
38: {…}
class: "Illuminate/Pipeline/Pipeline"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php"
function: "Illuminate/Pipeline/{closure}"
line: 53
type: "->"
​​​
39: {…}
class: "Illuminate/Routing/Pipeline"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php"
function: "Illuminate/Routing/{closure}"
line: 21
type: "->"
​​​
40: {…}
class: "Illuminate/Foundation/Http/Middleware/TransformsRequest"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
function: "handle"
line: 163
type: "->"
​​​
41: {…}
class: "Illuminate/Pipeline/Pipeline"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php"
function: "Illuminate/Pipeline/{closure}"
line: 53
type: "->"
​​​
42: {…}
class: "Illuminate/Routing/Pipeline"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php"
function: "Illuminate/Routing/{closure}"
line: 27
type: "->"
​​​
43: {…}
class: "Illuminate/Foundation/Http/Middleware/ValidatePostSize"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
function: "handle"
line: 163
type: "->"
​​​
44: {…}
class: "Illuminate/Pipeline/Pipeline"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php"
function: "Illuminate/Pipeline/{closure}"
line: 53
type: "->"
​​​
45: {…}
class: "Illuminate/Routing/Pipeline"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php"
function: "Illuminate/Routing/{closure}"
line: 62
type: "->"
​​​
46: {…}
class: "Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
function: "handle"
line: 163
type: "->"
​​​
47: {…}
class: "Illuminate/Pipeline/Pipeline"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php"
function: "Illuminate/Pipeline/{closure}"
line: 53
type: "->"
​​​
48: {…}
class: "Illuminate/Routing/Pipeline"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
function: "Illuminate/Routing/{closure}"
line: 104
type: "->"
​​​
49: {…}
class: "Illuminate/Pipeline/Pipeline"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php"
function: "then"
line: 151
type: "->"
​​​
50: {…}
class: "Illuminate/Foundation/Http/Kernel"
file: "/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php"
function: "sendRequestThroughRouter"
line: 116
type: "->"
​​​
51: {…}
class: "Illuminate/Foundation/Http/Kernel"
file: "/var/www/public/index.php"
function: "handle"
line: 55
type: "->"
​​​
status: 500
​
statusText: "Internal Server Error"

NOTE 2: The paths have been modified in this issue for the sake of security

@matt-allan
Copy link
Contributor

What web server are you using? If Apache please specify which MPM you are using (worker, prefork etc).

Does the issue persist if you run php artisan config:cache?

@laurencei
Copy link
Contributor

laurencei commented May 20, 2019

I'm fairly sure this relates to this: #24510 (comment)

@driesvints - either this issue, or the other one, should probably be left open, since it's clearly an ongoing issue? Leave it with you to decide what to do given it's not likely to be solved. Perhaps some documentation around it?

@driesvints
Copy link
Member

@Garanaw is your issue solved when running php artisan config:cache like @matt-allan suggested?

@seriquynh
Copy link
Contributor

You should never use "config:cache" on development environment.

@driesvints
Copy link
Member

@XuanQuynh he noted from the server so I suspect that this is on production?

@matt-allan
Copy link
Contributor

@XuanQuynh yes but you shouldn't be using getenv and putenv from a thread either.

I'm suggesting that they try config:cache because the error is most likely caused by getenv and putenv being called from a thread. If the problem goes away after running config:cache we have narrowed down the problem. If it doesn't go away we know it's a different issue.

I don't agree that you should never run config:cache in development. It's a bit annoying but it doesn't do any harm and in the OPs situation it's better than the alternative.

@Garanaw
Copy link
Author

Garanaw commented May 20, 2019

Apologies for the late response, I've been at work.

@driesvints when I mentioned the server I meant the backend part, but it is local.

@matt-allan It runs in local, I didn't deploy it to production yet. The environment is Wamp64 running on Windows 7, if it gives more info. As for the config:cache I didn't try it yet. Not sure if I should run that command in local for the what the previous comments say?

@matt-allan
Copy link
Contributor

Not sure if I should for the previous comments?

You shouldn't normally run config:cache in development but in this case I am trying to help debug your issue. If you don't run it I can't help you. You don't normally run config:cache in development because you will need to run config:cache again if you change something:

The command should not be run during local development as configuration options will frequently need to be changed during the course of your application's development.

https://laravel.com/docs/5.8/configuration#configuration-caching

I just need you to run config:cache to test if your issue persists. Afterwards you can just run config:clear to undo it.

The reason I am asking you to run config:cache in this case is because that avoids the putenv function call which is likely causing your problem.

The environment is Wamp64 running on Windows 7, if it gives more info

Their docs don't say what MPM it's using. Can you run phpinfo from a route, then copy the output under Configuration > apache2handler > Loaded Modules into this issue? It should look something like core mod_so http_core worker....

It would also be helpful to know what the value of Thread Safety is, it's at the top of the phpinfo output.

@Garanaw
Copy link
Author

Garanaw commented May 20, 2019

Ok, so, here are the loaded modules:

core mod_win32 mpm_winnt http_core mod_so mod_access_compat mod_actions mod_alias mod_allowmethods mod_asis mod_auth_basic mod_auth_digest mod_authn_core mod_authn_file mod_authz_core mod_authz_groupfile mod_authz_host mod_authz_user mod_autoindex mod_cache mod_cache_disk mod_dir mod_env mod_file_cache mod_include mod_isapi mod_log_config mod_mime mod_negotiation mod_rewrite mod_setenvif mod_userdir mod_vhost_alias mod_php7

And my Thread Safety is enabled.

I just ran the config:cache command, and I'll keep developing "as normal". I'll be updating the issue with the results, whether if the issue persists or if it's gone.

@matt-allan
Copy link
Contributor

Thanks for the output, that is really helpful.

The winnt MPM uses threads. PHP isn't really thread safe, even with thread safety enabled, and the putenv function is definitely not thread safe.

It's likely you are experiencing the same issue as #24510. There are some workarounds detailed in that issue.

I'm not sure if Laravel can really do anything about this other than updating the docs to mention this issue.

PHP doesn't recommend using a threaded webserver because of issues like this. The standard practice is to only call putenv in the parent thread but you can't do that if you aren't creating the threads yourself.

Laravel tried removing the putenv calls but that broke a lot of things and the change was reverted. There is a fix in PHP 7.4 that will help a little bit but you still might have issues.

@Garanaw
Copy link
Author

Garanaw commented May 20, 2019

@matt-allan thanks for the response. After a quick read to that issue, and knowing that it's a PHP issue, I guess I can live with it in development, I'll keep an eye on production to disable multi threads though. Thank you

@seriquynh
Copy link
Contributor

seriquynh commented May 21, 2019

You shouldn't normally run config:cache in development

shouldn't ... is more suitable than should never. I experience that nearly all issues relevant to environment and configuration often cause by cache. I need really care about these things again.

@matt-allan Thank you for your clear explanation!

@driesvints
Copy link
Member

Closing this as it's been determined that it's a php issue, not a Laravel issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants