FAQ
From: christophe.dirac@swissphone.ch
Operating system: SuSE 7.0 Kernel 2.2.17
PHP version: 4.1.1
PHP Bug Type: Unknown/Other Function
Bug description: pcntl_signal() does not work while waiting with socket_accept()

Configure Command: ./configure --with-imap --with-mysql --enable-sockets
--enable-pcntl

Example:
#! /usr/local/bin/php -q
<?php
error_reporting(E_ALL);
set_time_limit(0);

function sig_handler($signo) {
switch($signo) {
case SIGTERM:
// handle shutdown tasks
echo "Got SIGTERM! Closing sockets and exiting...\n";
@socket_close($msgsock);
@socket_close($sock);
exit;
break;
case SIGINT:
// handle shutdown tasks
echo "Got SIGINT! Closing sockets and exiting...\n";
@socket_close($msgsock);
@socket_close($sock);
exit;
break;
default:
// handle all other signals
}
}

$address = "127.0.0.1";
$port = 5550;

echo "Installing signal handler...\n";
if (pcntl_signal(SIGTERM, "sig_handler"))
echo "SIGTERM installed!\n";
if (pcntl_signal(SIGINT, "sig_handler"))
echo "SIGINT installed!\n";

if (($sock = socket_create(AF_INET, SOCK_STREAM, 0)) < 0) {
echo "socket_create() failed: reason: " . strerror($sock) . "\n";
exit;
}
if (($ret = socket_bind($sock, $address, $port)) < 0) {
echo "socket_bind() failed: reason: " . strerror($ret) . "\n";
exit;
}
if (($ret = socket_listen($sock, 5)) < 0) {
echo "socket_listen() failed: reason: " . strerror($ret) . "\n";
exit;
}

do {
echo "Listening...\n";
if (($msgsock = socket_accept($sock)) < 0) {
echo "socket_accept() failed: reason: " . strerror($msgsock) . "\n";
break;
}
else { socket_write($msgsock, "READY\n", 6); }
do {
break 1;
} while(true);
socket_close($msgsock);
} while(true);
socket_close($sock);
---END---

This peace of code is waiting for a socket connection
with socket_accept() and has installed
signal handlers for SIGINT and SIGTERM!

I'm expecting that catching signal SIGINT should
enter the function sig_handler() even if the script
is waiting for a connection at this moment.

That does not happen, any signal installed with
pcntl_signal() is ignored (of course not SIGKIL) while
waiting for connections with socket_accept().
The script does not enter the function sig_handler().

Kind regards
Chris

Search Discussions

  • Jason at Jul 1, 2002 at 10:05 pm
    ID: 15906
    Updated by: jason@php.net
    Reported By: christophe.dirac@swissphone.ch
    -Status: Open
    +Status: Bogus
    Bug Type: Unknown/Other Function
    Operating System: SuSE 7.0 Kernel 2.2.17
    PHP Version: 4.1.1
    New Comment:

    Thank you for taking the time to write to us, but this is not
    a bug. Please double-check the documentation available at
    http://www.php.net/manual/ and the instructions on how to report
    a bug at http://bugs.php.net/how-to-report.php

    This is because pcntl uses system call restarting, which means that it
    will notice the signal, store that it occured, then resume the call to
    the function after the io operation completes.

    This is usually desireable because it prevents data errors.

    I could possibly add an option to pcntl_signal that would not restart
    system calls.

    If this is desirable to you submit a feature request.

    Thanks,

    -Jason


    Previous Comments:
    ------------------------------------------------------------------------

    [2002-03-06 11:42:43] christophe.dirac@swissphone.ch

    Configure Command: ./configure --with-imap --with-mysql
    --enable-sockets --enable-pcntl

    Example:
    #! /usr/local/bin/php -q
    <?php
    error_reporting(E_ALL);
    set_time_limit(0);

    function sig_handler($signo) {
    switch($signo) {
    case SIGTERM:
    // handle shutdown tasks
    echo "Got SIGTERM! Closing sockets and exiting...\n";
    @socket_close($msgsock);
    @socket_close($sock);
    exit;
    break;
    case SIGINT:
    // handle shutdown tasks
    echo "Got SIGINT! Closing sockets and exiting...\n";
    @socket_close($msgsock);
    @socket_close($sock);
    exit;
    break;
    default:
    // handle all other signals
    }
    }

    $address = "127.0.0.1";
    $port = 5550;

    echo "Installing signal handler...\n";
    if (pcntl_signal(SIGTERM, "sig_handler"))
    echo "SIGTERM installed!\n";
    if (pcntl_signal(SIGINT, "sig_handler"))
    echo "SIGINT installed!\n";

    if (($sock = socket_create(AF_INET, SOCK_STREAM, 0)) < 0) {
    echo "socket_create() failed: reason: " . strerror($sock) . "\n";
    exit;
    }
    if (($ret = socket_bind($sock, $address, $port)) < 0) {
    echo "socket_bind() failed: reason: " . strerror($ret) . "\n";
    exit;
    }
    if (($ret = socket_listen($sock, 5)) < 0) {
    echo "socket_listen() failed: reason: " . strerror($ret) . "\n";
    exit;
    }

    do {
    echo "Listening...\n";
    if (($msgsock = socket_accept($sock)) < 0) {
    echo "socket_accept() failed: reason: " . strerror($msgsock) .
    "\n";
    break;
    }
    else { socket_write($msgsock, "READY\n", 6); }
    do {
    break 1;
    } while(true);
    socket_close($msgsock);
    } while(true);
    socket_close($sock);
    ---END---

    This peace of code is waiting for a socket connection
    with socket_accept() and has installed
    signal handlers for SIGINT and SIGTERM!

    I'm expecting that catching signal SIGINT should
    enter the function sig_handler() even if the script
    is waiting for a connection at this moment.

    That does not happen, any signal installed with
    pcntl_signal() is ignored (of course not SIGKIL) while
    waiting for connections with socket_accept().
    The script does not enter the function sig_handler().

    Kind regards
    Chris

    ------------------------------------------------------------------------


    --
    Edit this bug report at http://bugs.php.net/?id=15906&edit=1

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupphp-bugs @
categoriesphp
postedMar 6, '02 at 4:42p
activeJul 1, '02 at 10:05p
posts2
users2
websitephp.net

2 users in discussion

Jason: 1 post Christophe Dirac: 1 post

People

Translate

site design / logo © 2020 Grokbase