Bug #81589 [Opn->Dup]: PHP script on FPM / FastCGI may end prematurely
| From: | nikic@php.net | Date: | Thu, 04 Nov 2021 09:58:37 +0000 |
| Subject: | Bug #81589 [Opn->Dup]: PHP script on FPM / FastCGI may end prematurely | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-237541@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=81589&edit=1
ID: 81589
Updated by: nikic@php.net
Reported by: peter dot kotuliak at gmail dot com
Summary: PHP script on FPM / FastCGI may end prematurely
-Status: Open
+Status: Duplicate
Type: Bug
Package: FPM related
Operating System: Linux
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
This is the same issue as bug #72185, which also has a PR with the same fix you propose: https://github.com/php/php-src/pull/3198
Previous Comments:
------------------------------------------------------------------------
[2021-11-04 09:09:21] peter dot kotuliak at gmail dot com
Description:
------------
Running provided test script (error reporting cannot be Off) on real webserver (Apache, nginx
tested) using php-fpm, ends prematurely.
This is FPM specific bug (e.g. use of Apache handler is OK).
Script "terminates" because FPM sometimes generates empty (zero length) FCGI record /
packet. FCGI specification however states that such a record may indicate content termination.
Solution is to replace this code in fastcgi.c
} else if (len - limit < (int)(sizeof(req->out_buf) - sizeof(fcgi_header))) {
if (!req->out_hdr) {
open_packet(req, type);
}
if (limit > 0) {
memcpy(req->out_pos, str, limit);
req->out_pos += limit;
}
if (!fcgi_flush(req, 0)) {
with this
} else if (len - limit < (int)(sizeof(req->out_buf) - sizeof(fcgi_header))) {
if (limit > 0) {
if (!req->out_hdr) {
open_packet(req, type);
}
memcpy(req->out_pos, str, limit);
req->out_pos += limit;
}
if (!fcgi_flush(req, 0)) {
It solves bug, occurring when stream changes (from OUT to ERR or vice versa), but having too few
bytes left in output buffer (in this case, first it is called close_packet due to stream change and
since limit==0 additional open_packet and close_packet (inside fcgi_flush) is called with zero
content length, which webserver interprets as "request finished").
Script has to be tested on real webserver (FPM tester used in .phpt does not copy webserver behavior
that well in this case).
Every PHP version is affected.
If you have any questions, or trouble reproducing bug, please contact me.
Test script:
---------------
<?php
for($i=0;$i<167;$i++){
error_log('PHP is the best programming language');
}
echo 'end';
?>
Expected result:
----------------
string "end"
Actual result:
--------------
empty string
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=81589&edit=1