System's temp path differs from the fact, whether it's called in a queue or not

29 views Asked by At

I got a really strange problem:

I got a service class, which is called within a queued job. This queued job is being chained and dispatched from an action class.

Inside this action class, sys_get_temp_dir() is returning /var/tmp.

On the queued job however, the path returned by sys_get_temp_dir() is /var/folders/7s/jq24sk81665c1j4y3v9l3vth0000gn/T

public function doSomething(array $command)
{
    ray(sys_get_temp_dir())->label('methodCall');
}

returns /var/folders/7s/jq24sk81665c1j4y3v9l3vth0000gn/T.

And to get things even weirder: This is only true if I use the queue (in my case laravel/horizon). PestPHP-tests or manual tests using Tinker(well) do not reproduce the error.

Attempts to solve the problem

The first thing that comes to mind is that there are different php configurations in place. So I checked the horizon's long-living PHP processes:

ahoi             56472   0.0  0.2 410189312  81696 s002  S+    9:20AM   0:00.71 /opt/homebrew/Cellar/php/8.3.0/bin/php artisan horizon:work redis --name=default --supervisor=macbook-pro-IFR4:supervisor-1 --backoff=0 --max-time=0 --max-jobs=0 --memory=128 --queue=provisioning --sleep=3 --timeout=60 --tries=1 --rest=0
ahoi             56468   0.0  0.1 409899840  54432 s002  S+    9:20AM   0:00.27 /opt/homebrew/Cellar/php/8.3.0/bin/php artisan horizon:work redis --name=default --supervisor=macbook-pro-IFR4:supervisor-1 --backoff=0 --max-time=0 --max-jobs=0 --memory=128 --queue=deploying --sleep=3 --timeout=60 --tries=1 --rest=0
ahoi             56464   0.0  0.1 409645888  54064 s002  S+    9:20AM   0:00.26 /opt/homebrew/Cellar/php/8.3.0/bin/php artisan horizon:work redis --name=default --supervisor=macbook-pro-IFR4:supervisor-1 --backoff=0 --max-time=0 --max-jobs=0 --memory=128 --queue=building --sleep=3 --timeout=60 --tries=1 --rest=0
ahoi             56460   0.0  0.1 409629504  53872 s002  S+    9:20AM   0:00.26 /opt/homebrew/Cellar/php/8.3.0/bin/php artisan horizon:work redis --name=default --supervisor=macbook-pro-IFR4:supervisor-1 --backoff=0 --max-time=0 --max-jobs=0 --memory=128 --queue=default --sleep=3 --timeout=60 --tries=1 --rest=0
ahoi             56433   0.0  0.1 408944448  52160 s002  S+    9:20AM   0:00.47 /opt/homebrew/Cellar/php/8.3.0/bin/php artisan horizon:supervisor macbook-pro-IFR4:supervisor-1 redis --workers-name=default --balance=auto --max-processes=20 --min-processes=1 --nice=0 --balance-cooldown=3 --balance-max-shift=1 --parent-id=56417 --auto-scaling-strategy=time --backoff=0 --max-time=0 --max-jobs=0 --memory=128 --queue=default,building,deploying,provisioning --sleep=3 --timeout=60 --tries=1 --rest=0

This is not the same as I am using in CLI mode:

which php
/opt/homebrew/bin/php

ls -lah /opt/homebrew/bin/php
lrwxr-xr-x  1 ahoi  admin    27B Dec 22 12:51 /opt/homebrew/bin/php -> ../Cellar/php/8.3.1/bin/php

But:

/opt/homebrew/Cellar/php/8.3.0/bin/php -r 'echo sys_get_temp_dir();'
/var/folders/7s/jq24sk81665c1j4y3v9l3vth0000gn/T%     

php -r 'echo sys_get_temp_dir();'
/var/folders/7s/jq24sk81665c1j4y3v9l3vth0000gn/T%                                                                                                                                                                                                                                                                                                                             

Both versions are giving the correct path.

Next step is to determine the presence of the TMPDIR environment variable:

Inside the action class, which returns /var/tmp:

ray(sys_get_temp_dir(), getenv('TMPDIR'))->label('run');
ray()->phpinfo();

Here, getenv('TMPDIR') returns false.

But inside the job processed by horizon

ray(getenv('TMPDIR'))->label('run');

returns the correct path.

Digging deeper: The problem only appears, if I call the function using an HTTP request. And there, only if the HTTP request is processed by laravel valet.

Using the built-in laravel webserver, sys_get_temp_dir() does return the correct path.

0

There are 0 answers