php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75287 Builtin webserver crash after chdir in a shutdown function
Submitted: 2017-09-29 17:20 UTC Modified: 2018-01-12 17:20 UTC
From: manuele dot menozzi at gmail dot com Assigned:
Status: Re-Opened Package: Built-in web server
PHP Version: 7.0.24 OS: macOS / Linux Debian
Private report: No CVE-ID: None
 [2017-09-29 17:20 UTC] 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.  


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-10-15 02:30 UTC] 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-10-15 02:30 UTC] laruence@php.net
-Status: Open +Status: Closed
 [2017-12-01 14:11 UTC] weirdan at gmail dot com
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
 [2018-01-12 17:19 UTC] nikic@php.net
Automatic comment on behalf of nikita.ppv@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=9e98e99a3a88f98d1377d5b2a43b7ad223d8a61f
Log: Revert &quot;Fixed bug #75287 (Builtin webserver crash after chdir in a shutdown function)&quot;
 [2018-01-12 17:20 UTC] nikic@php.net
-Status: Closed +Status: Re-Opened
 [2018-01-12 17:20 UTC] nikic@php.net
Reverted via https://github.com/php/php-src/commit/9e98e99a3a88f98d1377d5b2a43b7ad223d8a61f due to BC break in patch release, see bug #75604.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC