Skip to content

.env issue with PHP thread-safe #7354

Closed

Description

@bioteck

I noticed a problem with functions putenv() and getenv() when PHP is thread-safe: if a URL is called before another is completed, its environment variables will be reset when the first script is finished. It's problematic with ajax requests, or simply if a user opens several links too quickly in new tabs (for example).

You can reproduce this bug by using WampServer, or any other server with PHP thread-safe, and creating two routes in "app/Http/routes.php":

Route::get('test1', function()
{
    sleep(5);

    return 'APP_ENV: '.env('APP_ENV');
});

Route::get('test2', function()
{
    sleep(5);

    return 'APP_ENV: '.env('APP_ENV');
});

Then call "test1", and while it's sleeping, call "test2".
"test1" will display "APP_ENV: local", but "test2" will display "APP_ENV: "

env_var_issue

This problem occurred with getenv(), but $_ENV and $_SERVER are not affected. So it can be resolved by using Dotenv::findEnvironmentVariable(), which uses $_ENV in priority, instead of getenv() in helper env():

$value = Dotenv::findEnvironmentVariable($key);

if ($value === null) return value($default);

The call of Dotenv::makeMutable() is also needed in bootstrapper "DetectEnvironment.php":

try
{
    Dotenv::makeMutable();
    Dotenv::load($app['path.base'], $app->environmentFile());
}
catch (InvalidArgumentException $e)
{
    //
}

Activity

  1. GrahamCampbell commented on Feb 9, 2015

    @GrahamCampbell
    Collaborator

    Please open an issue on the phpdotenv repo.

  2. bioteck commented on Feb 9, 2015

    @bioteck
    ContributorAuthor

    But it's not a phpdotenv issue! It's a PHP issue. However, I open an issue on Laravel because it can be solved in the way I explained. Why ignore my issue while I suggest a solution?

  3. GrahamCampbell commented on Feb 9, 2015

    @GrahamCampbell
    Collaborator

    There's nothing we can realistically do about it.

  4. GrahamCampbell commented on Feb 9, 2015

    @GrahamCampbell
    Collaborator

    Wait, what? If you've got a solution, then feel free to send a pull. That would be awesome! :)

    Issues aren't very helpful to us.

  5. neomerx commented on Feb 10, 2015

    @neomerx

    @bioteck Have you noticed? You should choose where to post issues depending on whether you have solution. 👍

  6. crynobone commented on Feb 10, 2015

    @crynobone
    Member

    There's nothing we can realistically do about it.

    Change env() to use Dotenv::findEnvironmentVariable($key) instead of getenv($key)?

  7. GrahamCampbell commented on Feb 10, 2015

    @GrahamCampbell
    Collaborator

    @crynobone I actually already privately discussed this via email, and it's clear that I was too hastily to dismiss this, but we're very much happy to receive pull requests to address this :)

  8. devcircus commented on Apr 28, 2018

    @devcircus
    Contributor

    @fkomaralp Laravel automatically calls the load method for you when the framework boots.

  9. added a commit that references this issue on Sep 30, 2021
  10. JustinShift commented on Jun 20, 2022

    @JustinShift

    If anyone is interested this issues only started happening to us when we set clear_env = yes in the PHP-FPM pool config. We still testing to confirm but thought I would post it here in case it helps somebody else.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions