php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70813 connection_status(), connection_aborted() not working
Submitted: 2015-10-29 14:59 UTC Modified: 2021-10-31 04:22 UTC
Votes:4
Avg. Score:4.5 ± 0.9
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:1 (33.3%)
From: sivann at gmail dot com Assigned:
Status: No Feedback Package: Network related
PHP Version: 7.4.24 OS: CentOS7
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: sivann at gmail dot com
New email:
PHP Version: OS:

 

 [2015-10-29 14:59 UTC] sivann at gmail dot com
Description:
------------
connection_status(), connection_aborted () never detect closed remote connection.

After closing connection initiated from client (e.g. wget) connection state becomes CLOSE_WAIT from ESTABLISHED, but those two functions called in a loop never detect that, even when outputing data.

Current solution is to popen netstat, or parse /proc/net/tcp, /proc/net/tcp6 to find if connection is established.

connection_status/connection_aborted should detect the connection loss even without writing anything. Currently those functions do nothing.

Test script:
---------------
<?php
ignore_user_abort(false);
function fecho($str) {
    file_put_contents("conlog.txt",$str."",FILE_APPEND);
}
while (1) {
    sleep (1);
    $d=date(DATE_RFC2822);
    $remport=$_SERVER['REMOTE_PORT'];
    $remip = $_SERVER['REMOTE_ADDR']?:($_SERVER['HTTP_X_FORWARDED_FOR']?:$_SERVER['HTTP_CLIENT_IP']);
    file_put_contents("conlog.txt","date:$d\tip:$remip\tport:$remport\tstatus:$constatus\n",FILE_APPEND);
    echo "...\n"; 
    flush();
    if (connection_status()!= CONNECTION_NORMAL){ //never works
        file_put_contents("conlog.txt","connection closed\n",FILE_APPEND);
        break;
    }
    file_put_contents("conlog.txt","connection_status=".connection_status().
        " connection_aborted():".connection_aborted()."\n",FILE_APPEND);
}
?>


Expected result:
----------------
The above writes on a file named conlog.txt. Call the above script with wget. The script should start writing data to conlog.txt at the server. Then press Ctrl+C to stop wget. The script should end. 



Actual result:
--------------
The script never stops running.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-10-29 15:11 UTC] sivann at gmail dot com
httpd-2.4.16 (apache)
 [2021-10-07 12:31 UTC] cmb@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2021-10-07 12:31 UTC] cmb@php.net
I cannot reproduce that with PHP-7.4 on Windows, neither with
httpd nor with the built-in server.  Is this still an issue for
you with any of the actively supported PHP versions[1]?

> connection_status(), connection_aborted () never detect closed
> remote connection.

You must ignore user aborts for these functions to work as
expected[2].

[1] <https://www.php.net/supported-versions.php>
[2] <https://www.php.net/manual/en/features.connection-handling.php>
 [2021-10-07 12:32 UTC] cmb@php.net
> The script never stops running.

This is what I cannot reproduce.
 [2021-10-17 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 [2021-10-17 07:55 UTC] sivann at gmail dot com
-Status: No Feedback +Status: Closed
 [2021-10-17 07:55 UTC] sivann at gmail dot com
The bug still exists. Just tested with:
* 7.4.24
* httpd-2.4.6-97
on CentOS7
 [2021-10-17 07:56 UTC] sivann at gmail dot com
I could not update the status to re-opened and it had only the closed option, Please re-open the bug, I could only post comments if I changed status to closed.
 [2021-10-17 07:57 UTC] sivann at gmail dot com
-Status: Closed +Status: Assigned
 [2021-10-17 07:57 UTC] sivann at gmail dot com
trying to reopen
 [2021-10-17 07:58 UTC] sivann at gmail dot com
-Status: Assigned +Status: Open -Operating System: Fedora 22 +Operating System: CentOS7 -PHP Version: 5.6.14 +PHP Version: 7.4.24
 [2021-10-17 07:58 UTC] sivann at gmail dot com
updated php version
 [2021-10-18 10:26 UTC] cmb@php.net
-Assigned To: cmb +Assigned To:
 [2021-10-18 10:26 UTC] cmb@php.net
Okay, maybe someone else is able to reproduce.
 [2021-10-18 13:02 UTC] sivann at gmail dot com
checked again with
* mod_php 
* php-fpm
* built-int web server

same thing, those 2 functions always return 0.
 [2021-10-18 13:26 UTC] sivann at gmail dot com
It seems adding an ob_flush() after flush() fixes it:
This works:
<?php
ignore_user_abort(true);


$remport=$_SERVER['REMOTE_PORT'];
$remip = $_SERVER['REMOTE_ADDR']?:($_SERVER['HTTP_X_FORWARDED_FOR']?:$_SERVER['HTTP_CLIENT_IP']);
file_put_contents("conlog.txt","date:$d\tip:$remip\tport:$remport\n",FILE_APPEND);

while (1) {
    sleep (1);
    $d=date(DATE_RFC2822);
    echo "...\n";
    ob_flush();
    flush();
    file_put_contents("conlog.txt","connection_status=".connection_status().  " connection_aborted():".connection_aborted()."\n",FILE_APPEND);
    if (connection_status()!= CONNECTION_NORMAL){ //never works
        file_put_contents("conlog.txt","connection closed\n",FILE_APPEND);
        ob_flush();
        flush();
        break;
    }
}

Maybe a doc edit would be needed to clarify this.

Moreover it would be easy for connection_status() and connection_aborted() to return connection status without outputting data to the client, by examining /proc/net/tcp (in linux) as we already have remote IP and port. Very useful to know when to abort long-poll requests. 
But this would be an another improvement ticket I presume.
 [2021-10-18 17:22 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2021-10-18 17:22 UTC] requinix@php.net
Do you have any output buffering enabled? As the docs do already say, PHP detects state when it tries to output, so if there is buffering then the functions won't be accurate until the buffer is flushed.
 [2021-10-31 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 19:01:28 2024 UTC