Skip to content

Stream Socket Timeout #11175

Closed
ThePHPF/thephp.foundation
#90

Description

@mohamedallou

Description

The following code:

<?php
$socket = stream_socket_server("tcp://0.0.0.0:8080", $errno, $errstr);
if (!$socket) {
    echo "$errstr ($errno)<br />\n";
} else {
    while ($conn = stream_socket_accept($socket, -1)) {
        fwrite($conn, 'The local time is ' . date('n/j/Y g:i a') . "\n");
        fclose($conn);
    }
    fclose($socket);
}

Resulted in this output:

Warning: stream_socket_accept(): Accept failed: Operation timed out in ....server.php

But I expected this output instead:

The expected behaviour until php ** 8.1** is to make this function wait indefinitely to incoming connections, just as in socket_accept, by setting the timeout to -1. But it times out immediately

The setting of default_socket_timeout to a negative value via init_set does not fix the problem as well.

PHP Version

PHP 8.2.5

Operating System

No response

Activity

  1. KapitanOczywisty commented on May 2, 2023

    @KapitanOczywisty

    I cannot reproduce that on linux nor windows, can you provide a bit more information about operating system and if this script is executed in console or from http (fpm, apache)?
    Does error occur immediately when script is executed or when trying to connect?
    Does timeout still happen when you use positive integer e.g. 3600?

  2. mohamedallou commented on May 2, 2023

    @mohamedallou
    Author

    This was tested on MacOs X php 8.2.5 in a console script. The timeout does not happen if we use positive numbers, .i.e if we set 5 seconds it will time out after 5 second if there are no request. This works correctly. The only problem was with negative value -1 which times out immediately.

  3. mohamedallou commented on May 2, 2023

    @mohamedallou
    Author

    On the other side, if we replicate this logic using the socket api: socket_create. socket_listen... Everything works fine.

  4. KapitanOczywisty commented on May 2, 2023

    @KapitanOczywisty

    This was tested on MacOs X php 8.2.5 in a console script.

    M1 or Intel? I cannot test that further unfortunately.

  5. mohamedallou commented on May 2, 2023

    @mohamedallou
    Author

    I used M1, Mac OS 13.0 and php 8.2.5

  6. ndossche commented on May 2, 2023

    @ndossche
    Member

    I have a strong feeling this is due to the undefined behaviour issue in #11177. Because when UB happens the compiler is allowed to generate any code it, i.e. the -1 situation "does not happen" according to the compiler.
    An extreme example is this: https://godbolt.org/z/Ehb6qY68G where the return value is not even emitted under ARM while it is emitted under x86-64. This could explain why you see it in M1 but we can't on x86.

  7. added a commit that references this issue on May 2, 2023
    26605f4
  8. added 2 commits that reference this issue on May 3, 2023
    ab3f584
    001e278
  9. added a commit that references this issue on Feb 3, 2024
    d75c1d0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions