php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #15906 pcntl_signal() does not work while waiting with socket_accept()
Submitted: 2002-03-06 11:42 UTC Modified: 2002-07-01 18:05 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: christophe dot dirac at swissphone dot ch Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 4.1.1 OS: SuSE 7.0 Kernel 2.2.17
Private report: No CVE-ID: None
 [2002-03-06 11:42 UTC] christophe dot dirac at swissphone dot 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-07-01 18:05 UTC] jason@php.net
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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 20:01:32 2024 UTC