Ask a Question related to PHP Bugs, Design and Development.
-
antoine dot bajolet at tdf dot fr #1
#20745 [Com]: socket_accept() in blocking mode doesn't handle signal interrupts
ID: 20745
Comment by: antoine dot bajolet at tdf dot fr
Reported By: polone at townnews dot com
Status: No Feedback
Bug Type: Sockets related
Operating System: Linux
PHP Version: 4.2.3
New Comment:
Hello,
The issue still exists,
PHP Version => 4.4.6
System => Linux pentium2.antoine 2.6.20 #5 Sun Feb 18 16:28:17 CET 2007
i686
Build Date => Mar 6 2007 21:15:38
Complete code to reproduce error :
script.php
------------------------------------------------------------------
#!/usr/local/bin/php
<?php
define ( 'BIND_PORT', 3333 );
/* Signal Handler */
function signalHandler( $sig ) {
switch ( $sig ) {
case SIGTERM :
exit( 0 );
break;
case SIGCHLD :
pcntl_waitpid(-1,$status,WNOHANG);
break;
}
}
print "My PID is ".posix_getpid()."\n";
declare ( ticks = 1 );
pcntl_signal( SIGTERM, 'signalHandler' );
pcntl_signal( SIGCHLD, 'signalHandler' );
// infinite execution time
set_time_limit( 0 );
// Socket creation
$sock = socket_create ( AF_INET, SOCK_STREAM, SOL_TCP );
// Bind sur le port BIND_PORT
if ( !socket_bind( $sock, '0.0.0.0', BIND_PORT ) ) {
print "Unable to bind to port " . BIND_PORT . " !\n";
exit( 1 );
}
// Listening up to 16 buffets
socket_listen ( $sock, 16 );
// Infinite loop
while ( true ) {
// Wait for incoming connections, hangs signal handling
$subsock = socket_accept ( $sock );
// Connection received, forking sub-process
$subPid = pcntl_fork();
if ( $subPid === 0 ) {
// Get remote informations
socket_getpeername( $subsock , $remoteAddr , $remotePort );
socket_write( $subsock, 'Simple Socket Server accepting
commands from ' . $remoteAddr . "\n" );
// Whe are in interactive mode
while ( true ) {
$received = socket_read ( $subsock, 65536, PHP_NORMAL_READ
);
// Clean shutdown
if ( !$received ) {
socket_shutdown( $subsock, 2 );
socket_close( $subsock );
exit( 0 );
}
// Cleaning entry
$received = trim( $received );
if ( $received ) {
// two commands : HELLO and QUIT
switch ( $received ) {
case 'HELLO':
socket_write( $subsock, 'HELLO ' . $remoteAddr
.. "\n" );
break;
case 'QUIT':
// (shutdown)
socket_write( $subsock, 'BYE' . "\n" );
socket_shutdown( $subsock, 2 );
socket_close( $subsock );
exit( 0 );
break;
default :
socket_write( $subsock, 'Unknown command...' .
"\n" );
}
}
}
}
} // while
?>
------------------------------------------------------------------
Commands :
[Shell1]$ ./script.php
My PID is 18135
[Shell2]$ kill 18135
<Happens nothing on Shell1...>
[Shell2]$ telnet localhost 3333
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host
[Shell1] : The script exits.
=> The signal is properly intercepted but the handler is only called
when the script continues after socket_accept().
Whe are using a little more complex code when the fist process is a
child of a fork itself : At this time, the only way to end it is a not
very clean SIGKILL.
Regards,
AB
Previous Comments:
------------------------------------------------------------------------
[2006-05-25 19:31:39] lindsay at bitleap dot com
I still see this issue with php 5.1.4. I did notice that the signal
will queue up and on the next inbound connection the queued signal
executes.
------------------------------------------------------------------------
[2006-02-09 18:55:35] flachi at gmail dot com
I also have this problem with PHP 4.4.2. Is there any way to fix it ?
------------------------------------------------------------------------
[2003-01-02 02:50:09] piotr at t-p-l dot com
I'd like to revive this bug .. as of 4.3.0 this beheaviour persists ..
I even tried this define(ticks=1); approach quoted in one of the
related bugs .. it didnt help .. during a call to socket_accept() as
far as I can see (I've tested only with SIGTERM and SIGINT) .. those
handlers never get called .. with the new feature (the 3rd param on
pcntl_signal() .. set to false) .. the socket_accept() call gets
interrupted, but the handler is still not called. I would say there is
a need to resolve this .. right now there is no way to cleanly kill a
daemon process running php unless non blocking sockets are used ..
which is basicaly a waste of cpu cycles ..
Please respond as to the status of this problem asap.
------------------------------------------------------------------------
[2002-12-15 04:05:02] [email]sniper@php.net[/email]
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 "Open". Thank you.
------------------------------------------------------------------------
[2002-12-01 14:31:23] [email]sniper@php.net[/email]
Please try using this CVS snapshot:
[url]http://snaps.php.net/php4-latest.tar.gz[/url]
For Windows:
[url]http://snaps.php.net/win32/php4-win32-latest.zip[/url]
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
[url]http://bugs.php.net/20745[/url]
--
Edit this bug report at [url]http://bugs.php.net/?id=20745&edit=1[/url]
antoine dot bajolet at tdf dot fr Guest
-
#25309 [Opn->Bgs]: stream_set_blocking() doesn't set non-blocking mode
ID: 25309 Updated by: wez@php.net Reported By: flugelaar at pandora dot be -Status: Open +Status: ... -
#25309 [NEW]: stream_set_blocking() doesn't set non-blocking mode
From: flugelaar at pandora dot be Operating system: Linux Slackware 9.0 PHP version: 5CVS-2003-08-29 (dev) PHP Bug Type: ... -
signal interrupts one sleeping thread or all of them?
Suppose in a multithreading environment, If several threads are blocked, which can be interrupted by a signal. And a signal is sent to this... -
DBD::Oracle9 and non-blocking mode
I'm about ready to make available an early copy of my library, but first I have some questions regarding non-blocking mode. I've never actually... -
:Oracle9 and non-blocking mode
Hi Jim, I use it to timeout connect calls when a database is either down and/or unreachable for some reason. This is rare, but it does happen,...



Reply With Quote

