You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<?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.
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?
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.
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.
Activity
KapitanOczywisty commentedon May 2, 2023
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?
mohamedallou commentedon May 2, 2023
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.
mohamedallou commentedon May 2, 2023
On the other side, if we replicate this logic using the socket api: socket_create. socket_listen... Everything works fine.
KapitanOczywisty commentedon May 2, 2023
M1 or Intel? I cannot test that further unfortunately.
mohamedallou commentedon May 2, 2023
I used M1, Mac OS 13.0 and php 8.2.5
ndossche commentedon May 2, 2023
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.
Fix phpGH-11175 and phpGH-11177: Stream socket timeout undefined beha…
Merge branch 'PHP-8.1' into PHP-8.2
Merge branch 'PHP-8.2'
Fix phpGH-11175 and phpGH-11177: Stream socket timeout undefined beha…