<?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>Tue, 13 Dec 2011 21:29:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<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't just change the spec to avoid the situation... and I'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't just change the spec to avoid the situation... and I'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've encountered the problem:</p>
<div class="syntax_hilite">
<div id="php-9">
<div class="php">pcntl_signal<span style="color:#006600; font-weight:bold;">&#40;</span>SIGINT, <span style="color:#FF0000;">"sig_handler"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$sock</span> = <a href="http://www.php.net/socket_create_listen"><span style="color:#000066;">socket_create_listen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$port</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$read_socks</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$sock</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$n</span> = <span style="color:#000000; font-weight:bold;">NULL</span>;<br />
<span style="color:#0000FF;">$foo</span> = <a href="http://www.php.net/socket_select"><span style="color:#000066;">socket_select</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$read_socks</span>, <span style="color:#0000FF;">$n</span>, <span style="color:#0000FF;">$n</span>, <span style="color:#000000; font-weight:bold;">NULL</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</div>
</div>
<p>
By filling in his blanks, my first test case looks something like this:</p>
<div class="syntax_hilite">
<div id="php-10">
<div class="php"><span style="color:#000000; font-weight:bold;">&lt;?</span><br />
<span style="color:#000000; font-weight:bold;">function</span> sig_handler<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$signo</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"received sig #$signo<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
pcntl_signal<span style="color:#006600; font-weight:bold;">&#40;</span> SIGINT, <span style="color:#FF0000;">"sig_handler"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p><span style="color:#0000FF;">$socket</span> = <a href="http://www.php.net/socket_create_listen"><span style="color:#000066;">socket_create_listen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC66CC;">1234</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$r</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$socket</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$n</span> = <span style="color:#000000; font-weight:bold;">NULL</span>;<br />
<span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$foo</span> = <a href="http://www.php.net/socket_select"><span style="color:#000066;">socket_select</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$r</span>, <span style="color:#0000FF;">$n</span>, <span style="color:#0000FF;">$n</span>, <span style="color:#000000; font-weight:bold;">NULL</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"select returned '$foo'<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#000000; font-weight:bold;">?&gt;</span></div>
</div>
</div>
<p>
When executing the script and pressing ^C (which sends SIGINT), the following occurs:</p>
<div class="syntax_hilite">
<div id="code-11">
<div class="code">ammon@morbo:~$ php sigtest.<span style="">php</span> <br />
PHP Warning:&nbsp; socket_select<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>: unable to select <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>: Interrupted system call in /home/ammon/sigtest.<span style="">php</span> on line <span style="color:#800000;">13</span><br />
select returned <span style="color:#CC0000;">''</span></div>
</div>
</div>
<p></p>
<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... 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't duplicate the bug. Upon looking at the differences between their code, only one difference stands out:</p>
<div class="syntax_hilite">
<div id="php-12">
<div class="php">declare<span style="color:#006600; font-weight:bold;">&#40;</span>ticks=<span style="color:#CC66CC;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</div>
</div>
<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've neither successfully used them nor seen a successful and justified use.</p>
<p>That being said... turning the tick on but not telling it to do anything appears to address the problem of discarded interrupts:</p>
<div class="syntax_hilite">
<div id="php-13">
<div class="php"><span style="color:#000000; font-weight:bold;">&lt;?</span><br />
declare<span style="color:#006600; font-weight:bold;">&#40;</span>ticks=<span style="color:#CC66CC;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p><span style="color:#000000; font-weight:bold;">function</span> sig_handler<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$signo</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"received sig #$signo<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
pcntl_signal<span style="color:#006600; font-weight:bold;">&#40;</span> SIGINT, <span style="color:#FF0000;">"sig_handler"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p><span style="color:#0000FF;">$socket</span> = <a href="http://www.php.net/socket_create_listen"><span style="color:#000066;">socket_create_listen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC66CC;">1234</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$r</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$socket</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$n</span> = <span style="color:#000000; font-weight:bold;">NULL</span>;<br />
<span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$foo</span> = @<a href="http://www.php.net/socket_select"><span style="color:#000066;">socket_select</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$r</span>, <span style="color:#0000FF;">$n</span>, <span style="color:#0000FF;">$n</span>, <span style="color:#000000; font-weight:bold;">NULL</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"select returned '$foo'<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#000000; font-weight:bold;">?&gt;</span></div>
</div>
</div>
<p>
And execution:</p>
<div class="syntax_hilite">
<div id="code-14">
<div class="code">ammon@morbo:~$ php sigtest.<span style="">php</span> <br />
received sig #2<br />
select returned <span style="color:#CC0000;">''</span></div>
</div>
</div>
<p>
Which is precisely the desired behavior.</p>
<p>I don't know what the performance hit for turning ticks on is, I haven'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>
<div class="syntax_hilite">
<div id="php-15">
<div class="php"><span style="color:#000000; font-weight:bold;">&lt;?</span><br />
declare<span style="color:#006600; font-weight:bold;">&#40;</span>ticks=<span style="color:#CC66CC;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p><span style="color:#000000; font-weight:bold;">class</span> signal_tester <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">function</span> __construct<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; pcntl_signal<span style="color:#006600; font-weight:bold;">&#40;</span> SIGINT, <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span>&amp;<span style="color:#0000FF;">$this</span>,<span style="color:#FF0000;">"sig_handler"</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">start</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">function</span> sig_handler<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$signo</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"received sig #$signo<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">function</span> start<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$socket</span> = <a href="http://www.php.net/socket_create_listen"><span style="color:#000066;">socket_create_listen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC66CC;">1234</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$r</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$socket</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$n</span> = <span style="color:#000000; font-weight:bold;">NULL</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$foo</span> = @<a href="http://www.php.net/socket_select"><span style="color:#000066;">socket_select</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$r</span>, <span style="color:#0000FF;">$n</span>, <span style="color:#0000FF;">$n</span>, <span style="color:#000000; font-weight:bold;">NULL</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"select returned '$foo'<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p><span style="color:#0000FF;">$test</span> = <span style="color:#000000; font-weight:bold;">new</span> signal_tester<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#000000; font-weight:bold;">?&gt;</span></div>
</div>
</div>
<p>
Executing and hitting ^C:</p>
<div class="syntax_hilite">
<div id="code-16">
<div class="code">ammon@morbo:~$ php sigtest.<span style="">php</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
received sig #2<br />
select returned <span style="color:#CC0000;">''</span></div>
</div>
</div>
<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... 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...) 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't know how the behavior is going to be in 5.3, since I don't run alpha releases on my servers but my gut likes to think that it will continue to work the same for now... 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>2</slash:comments>
		</item>
	</channel>
</rss>

