php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47893 CLI script with non-blocking STDIN terminates randomly
Submitted: 2009-04-03 23:17 UTC Modified: 2009-04-07 16:12 UTC
From: jon at thesquareplanet dot com Assigned:
Status: Closed Package: CGI/CLI related
PHP Version: 5.2CVS-2009-04-03 (snap) OS: Debian Lenny
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jon at thesquareplanet dot com
New email:
PHP Version: OS:

 

 [2009-04-03 23:17 UTC] jon at thesquareplanet dot com
Description:
------------
When setting STDIN to be non-blocking, and then running fread() on it, the cli script suddenly terminates at a seemingly random point during the reading. No error message is given.

Seemingly related bugs:
http://bugs.php.net/bug.php?id=25616 <-- This is the same bug, but has erroneously been labeled as a dupe of the one below
http://bugs.php.net/bug.php?id=25575 <-- This bug describes a somewhat similar, though not nearly identical, problem

These bugs are both set as last modified in 2003/2004, but the issue remains unfixed.

Reproduce code:
---------------
<?php
stream_set_blocking(STDIN, FALSE);
while (1) {
var_dump(fread(STDIN,1));
echo "\n";;
}
?>

Expected result:
----------------
A never-ending sequence of var_dumps of either an empty string or a typed character

Actual result:
--------------
The expected output, but only for a short while, and then the script terminates

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-04-03 23:18 UTC] jon at thesquareplanet dot com
A discussion of this bug can also be found at:
http://www.mail-archive.com/php-general@lists.php.net/msg118150.html
 [2009-04-04 15:48 UTC] lbarnaud@php.net
This is reproducible with this script too:

<?php
stream_set_blocking(STDIN, 0);
$str = str_repeat("a", 65535);
var_dump($str);
?>

This is caused by STDOUT being non-blocking if your shell passes the same descriptor for STDOUT and STDIN (the write fails with EAGAIN and PHP aborts).

Running the same script with different STDIN/STDOUT does not reproduce the problem, e.g.
php test.php >/dev/null
or
php test.php </dev/null
 [2009-04-07 16:12 UTC] lbarnaud@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jan 02 12:01:29 2025 UTC