<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Untitled &#187; posix signals</title>
	<atom:link href="http://ammonlauritzen.com/blog/tag/posix-signals/feed/" rel="self" type="application/rss+xml" />
	<link>http://ammonlauritzen.com/blog</link>
	<description>and still for good reason.</description>
	<lastBuildDate>Wed, 05 May 2010 18:43:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>php signals while selecting</title>
		<link>http://ammonlauritzen.com/blog/2008/10/27/php-signals-while-selecting/</link>
		<comments>http://ammonlauritzen.com/blog/2008/10/27/php-signals-while-selecting/#comments</comments>
		<pubDate>Mon, 27 Oct 2008 20:26:38 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[posix signals]]></category>
		<category><![CDATA[sockets]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/?p=399</guid>
		<description><![CDATA[So a fairly longstanding gripe of mine has been that PHP fails to execute registered signal handlers when it receives a signal in the middle of a blocking select call. Today, I finally bumped into a situation where I couldn&#8217;t just change the spec to avoid the situation&#8230; and I&#8217;ve finally figured out how to [...]]]></description>
			<content:encoded><![CDATA[<p>So a fairly longstanding gripe of mine has been that PHP fails to execute registered signal handlers when it receives a signal in the middle of a blocking select call. Today, I finally bumped into a situation where I couldn&#8217;t just change the spec to avoid the situation&#8230; and I&#8217;ve finally figured out how to make it work.</p>
<p>The bug has been reported <a href='http://bugs.php.net/44614'>here</a>, where it was ignored for a few months before being shot down and ignored some more as per php dev team regulations.</p>
<p>Sample code given by the reporter of the bug is markedly similar to the situations I&#8217;ve encountered the problem:</p>
<pre class="brush: php;">
pcntl_signal(SIGINT, &quot;sig_handler&quot;);
$sock = socket_create_listen($port);
$read_socks = array($sock);
$n = NULL;
$foo = socket_select($read_socks, $n, $n, NULL);
</pre>
<p>By filling in his blanks, my first test case looks something like this:</p>
<pre class="brush: php;">
&lt;?
function sig_handler($signo) {
        echo &quot;received sig #$signo\\\n&quot;;
}
pcntl_signal( SIGINT, &quot;sig_handler&quot; );

$socket = socket_create_listen( 1234 );
$r = array( $socket );
$n = NULL;
while( true ) {
        $foo = socket_select( $r, $n, $n, NULL );
        echo &quot;select returned '$foo'\\\n&quot;;
}
?&gt;
</pre>
<p>When executing the script and pressing ^C (which sends SIGINT), the following occurs:</p>
<pre class="brush: plain;">
ammon@morbo:~$ php sigtest.php
PHP Warning:  socket_select(): unable to select [4]: Interrupted system call in /home/ammon/sigtest.php on line 13
select returned ''
</pre>
<p>Ok, so the warning is to be expected, and we can easily squelch that.</p>
<p>The real problem is that the signal handler never runs.</p>
<p>However&#8230; for the first time in my life, a response to a php bug report proves enlightening. The dev who answered this ticket provides his sample code and says he can&#8217;t duplicate the bug. Upon looking at the differences between their code, only one difference stands out:</p>
<pre class="brush: php;">
declare(ticks=1);
</pre>
<p>The <a href='http://php.net/declare'>declare(ticks)</a> directive is deprecated as of php 5.3 and will not be with us in php 6.0. Ticks are an unreliable, unpredictable, and generally bad thing in php. I&#8217;ve neither successfully used them nor seen a successful and justified use.</p>
<p>That being said&#8230; turning the tick on but not telling it to do anything appears to address the problem of discarded interrupts:</p>
<pre class="brush: php;">
&lt;?
declare(ticks=1);

function sig_handler($signo) {
        echo &quot;received sig #$signo\\\n&quot;;
}
pcntl_signal( SIGINT, &quot;sig_handler&quot; );

$socket = socket_create_listen( 1234 );
$r = array( $socket );
$n = NULL;
while( true ) {
        $foo = @socket_select( $r, $n, $n, NULL );
        echo &quot;select returned '$foo'\\\n&quot;;
}
?&gt;
</pre>
<p>And execution:</p>
<pre class="brush: plain;">
ammon@morbo:~$ php sigtest.php
received sig #2
select returned ''
</pre>
<p>Which is precisely the desired behavior.</p>
<p>I don&#8217;t know what the performance hit for turning ticks on is, I haven&#8217;t had time to research this. But I can confirm that by declaring ticks globally, it does work in an OO environment as well:</p>
<pre class="brush: php;">
&lt;?
declare(ticks=1);

class signal_tester {
    function __construct() {
        pcntl_signal( SIGINT, array(&amp;$this,&quot;sig_handler&quot;) );
        $this-&gt;start();
    }

    function sig_handler($signo) {
        echo &quot;received sig #$signo\\\n&quot;;
    }

    function start() {
        $socket = socket_create_listen( 1234 );
        $r = array( $socket );
        $n = NULL;
        while( true ) {
            $foo = @socket_select( $r, $n, $n, NULL );
            echo &quot;select returned '$foo'\\\n&quot;;
        }
    }
}

$test = new signal_tester();
?&gt;
</pre>
<p>Executing and hitting ^C:</p>
<pre class="brush: plain;">
ammon@morbo:~$ php sigtest.php
received sig #2
select returned ''
</pre>
<p>After a few minutes of largely unscientific testing, it appears that turning ticks on globally costs a whopping 4 bytes of ram and causes the script to occasionally consume more cpu than the top process I used to monitor it. So&#8230; at first glance the cost is pretty negligible and all I can say is that if you ever need to handle signals (SIGTERM, SIGHUP, etc&#8230;) from within a blocking select call in php, it looks like declare ticks is the only option for now.</p>
<p>I did the initial tests in 5.1.6, but can confirm the same behavior in 5.2.5. I don&#8217;t know how the behavior is going to be in 5.3, since I don&#8217;t run alpha releases on my servers but my gut likes to think that it will continue to work the same for now&#8230; and will hopefully not break until 6.0 (when everything else will explode for a few years). Shrug.</p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2008/10/27/php-signals-while-selecting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
