Bug #75287 [Com]: Builtin webserver crash after chdir in a shutdown function
| From: | weirdan at gmail dot com | Date: | Fri, 01 Dec 2017 14:11:22 +0000 |
| Subject: | Bug #75287 [Com]: Builtin webserver crash after chdir in a shutdown function | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-212864@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=75287&edit=1
ID: 75287
Comment by: weirdan at gmail dot com
Reported by: manuele dot menozzi at gmail dot com
Summary: Builtin webserver crash after chdir in a shutdown
function
Status: Closed
Type: Bug
Package: Built-in web server
Operating System: macOS / Linux Debian
PHP Version: 7.0.24
Block user comment: N
Private report: N
New Comment:
This not just fixed that bug, but also changed the way relative path to the router script is
treated. Previously it assumed the path to be relative to $CWD, now it's relative to document
root (set with the -t option).
It caused (easily fixed) breaks for scripts that were starting CLI server like this:
php -S 0.0.0.0:8888 -t public public/index.php.
Now they fail with:
PHP Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0
PHP Fatal error: Unknown: Failed opening required '<path
to>/public/public/index.php'
(note the duplicate 'public/public').
The fix is to start CLI server like this (router path is relative to doc root):
php -S 0.0.0.0:8888 -t public index.php
Previous Comments:
------------------------------------------------------------------------
[2017-10-15 02:30:06] laruence@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=816758eda2bcdd69ba505fb6bbb79124a7bf2254
Log: Fixed bug #75287 (Builtin webserver crash after chdir in a shutdown function)
------------------------------------------------------------------------
[2017-09-29 17:20:31] manuele dot menozzi at gmail dot com
Description:
------------
Hi,
PHP's built-in webserver crashes if it's started with a router script with relative path
and in a shutdown function there is a chdir to a different directory than one where the router
script is.
Test script:
---------------
Create the file /home/user/router.php with the following content
<?php
function shutdownWithChdir() {
chdir('/tmp');
}
register_shutdown_function('shutdownWithChdir');
echo 'Hello World';
?>
Now in your terminal:
$ cd /home/user
$ php -S localhost:3000 router.php
Now perform the first request to the webserver and see that correctly returns "Hello
World". Then perform a second request.
Expected result:
----------------
You would expect to get "Hello World" even for the second request.
Actual result:
--------------
Instead you get:
Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0
Fatal error: Unknown: Failed opening required 'router.php' (include_path='.:')
in Unknown on line 0
For every subsequent request. If you restart the webserver you'll get the same result: first
request ok, subsequent requests error.
This happens only if the chdir is done inside a "shutdown function" not if it's done
in the "normal" script execution.
I can confirm this bug on PHP 5.6, 7.0 and 7.1 (I do not remember the exact versions numbers) and I
don't think it's related to the #74496.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=75287&edit=1