|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60938 xp_socket.c functions should respect pcntl signal handlers
Submitted: 2012-01-31 13:41 UTC Modified: 2021-09-05 04:22 UTC
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: nasretdinov at gmail dot com Assigned: cmb (profile)
Status: No Feedback Package: PCNTL related
PHP Version: 5.3.9 OS: *nix
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: nasretdinov at gmail dot com
New email:
PHP Version: OS:


 [2012-01-31 13:41 UTC] nasretdinov at gmail dot com
Socket functions in xp_socket.c read and write indefinitely in case they received  
signal (read/write returned EINTR). Example, from 

while(1) {
retval = php_pollfd_for(sock->socket, PHP_POLLREADABLE, ptimeout);
if (retval == 0) sock->timeout_event = 1;
if (retval >= 0) break;
if (php_socket_errno() != EINTR) break;

The problem here is that this behavior is not compatible with pcntl signal 
because pcntl signals are implemented using ticks and this code actually does not 
even if we need to call signal handler.

Suggested fix for read() is to take into account existence of PCNTL and do the 
following check:

if (php_socket_errno() != EINTR) break;
#ifdef PCNTL_G
else if(PCNTL_G(head)) break;

/* PCNTL_G(head) is pointer to a queue of signals
   that were caught and will be processed after next tick.
   This queue is empty if we do not register handlers for received signal.

This fix (and adding ZEND_EXTERN_MODULE_GLOBALS(pcntl) to pcntl.c) gives the 
expected result below

Test script:
declare(ticks = 1);
function sighandler($sig) {
echo "Caught signal $sig, exiting correctly\n";

pcntl_signal(SIGINT, 'sighandler', false);
pcntl_signal(SIGHUP, 'sighandler', false);

mysql_connect(); // assumes 'mysqlnd' as MySQL driver
mysql_query('SELECT SLEEP(100)'); // ignores SIGINT and SIGHUP for 100 sec

Expected result:
$ php test.php
Warning: mysql_query(): MySQL server has gone away in test.php on line 14

Warning: mysql_query(): Error reading result set's header in 
/Users/nasretdinov/test.php on line 14
Caught signal 2, exiting correctly

Actual result:
$ php
# ...after 100 sec...
Caught signal 2, exiting correctly


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2021-08-24 14:18 UTC]
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2021-08-24 14:18 UTC]
Isn't this obsolete with the availability of pcntl_async_signals()
as of PHP 7.1.0?
 [2021-09-05 04:22 UTC] php-bugs at lists dot php dot net
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 "Re-Opened". Thank you.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jun 17 13:01:29 2024 UTC