Skip to content

[Help] $_ENV gets overridden #1061

@nickdnk

Description

@nickdnk

What happened?

Hello

I'm not sure if this is a bug or just me being stupid, but I can't for the life of me figure out how to populate my $_ENV array inside the worker handler.

Code looks like this. I'm using the Slim framework.

<?php

use Slim\App;

ignore_user_abort(true);

// This loads dotenv, defines $app and populates my $_ENV
require 'app/bootstrap.php';

/** @var App $app */
// $_ENV contains all my expected variables at this point if I dump them, so I copy them into another array:
$env = array_merge($_ENV);

// And use that array in the handler:
$handler = static function () use ($app, $env) {

    $_ENV = array_merge($_ENV, $env);
    // At this point, $_ENV still contains what I expect it to if I error_log(json_encode($_ENV))

    // But inside my $app, all the $_ENV variables I can normally access no longer contain my values;
    // they get overridden with what looks like caddy environment variables, even though the $_ENV array 
    // is correct just above this call?
    // In my $app, I simply use the array like $myValue = $_ENV['MY_VAR_KEY'], so no funny-business.
     $app->run();

};

while (true) {
    $keepRunning = \frankenphp_handle_request($handler);

    gc_collect_cycles();

    if (!$keepRunning) {
        break;
    }
}

I cannot understand how this would happen, and after a few requests, I get blank 200 OK but no error and no logs. I suspect this is a different issue, so I'm trying to figure out the env thing first. Unrelated, fixed.

Build Type

Docker (Debian Bookworm)

Worker Mode

Yes

Operating System

macOS

CPU Architecture

Apple Silicon

PHP configuration

It's just the default frankenphp docker image (:latest as of this issue) with the following php.ini loaded:

session.use_strict_mode = 1
session.use_cookies = 1
session.cookie_httponly = 1
session.sid_length = 54
session.sid_bits_per_character = 5
session.gc_probability = 0
zend_extension = opcache.so
opcache.enable = 1
opcache.jit = disable
opcache.jit_buffer_size = 128M
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.enable_cli=1
upload_max_filesize = 10M
post_max_size = 10M

Relevant log output

I just get: Undefined array key "MY_VAR_KEY" from a class inside $app.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions