﻿<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://purl.org/atom/ns#">
	<link xmlns="http://purl.org/atom/ns#" type="text/html" rel="alternate" href="http://sial.org/blog/" title="Jeremy Mates’s Blog"/>
	<title xmlns="http://purl.org/atom/ns#">Jeremy Mates’s Blog</title>
	<entry xmlns="http://purl.org/atom/ns#" xmlns:default="http://www.w3.org/1999/xhtml">
		<title xmlns="http://purl.org/atom/ns#">Heuristic for "hung" Perl Code</title>
		<dc:subject>Coding</dc:subject>
		<dc:subject>Perl</dc:subject>
		<summary xmlns="http://purl.org/atom/ns#">summary</summary>
		<content xmlns="http://purl.org/atom/ns#" mode="escaped">&lt;p&gt;Software “hangs,” where the software appears wedged or otherwise unresponsive, are remarkably easy to produce, though the hang may stem from any number of different problems.&lt;/p&gt;
&lt;p style="font-family: &quot;Courier&quot;, &quot;Bitstream Vera Sans Mono&quot;, monospace; background-color: #efefef; padding: 0.5em; white-space: pre; border-color: #999; border-width: 1px; border-style: solid; clear: both; max-width: 50em;"&gt;$ &lt;kbd style="font-weight: bold;"&gt;perl -e &#39;$i=&lt;STDIN&gt;&#39;&lt;/kbd&gt;&lt;/p&gt;
&lt;p&gt;Describing the problem as “my code hangs” or the equally bad “it doesn’t work” only leads to new questions (hung like laundry?) and various areas of additional research:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Add &lt;tt&gt;warn&lt;/tt&gt; statements (or use the &lt;a href="http://search.cpan.org/perldoc/perldebug"&gt;debugger&lt;/a&gt;, or &lt;a href="http://search.cpan.org/perldoc/Devel::ptkdb"&gt;&lt;tt&gt;Devel::ptkdb&lt;/tt&gt;&lt;/a&gt;, or perhaps &lt;a href="http://www.gnu.org/software/ddd/"&gt;DDD&lt;/a&gt;; I simply use &lt;tt&gt;warn&lt;/tt&gt; statements) if the cause of the hang is unknown. Narrow down the problem as much as possible. Once isolated, try to reproduce the problem in a minimal test script.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The hang &lt;a href="http://perl.plover.com/FAQs/Buffering.html"&gt;might be a buffering problem&lt;/a&gt;. Peruse the “Suffering from Buffering” article to understand how, if at all, the program buffers output. For standard output, try setting &lt;a href="http://search.cpan.org/perldoc/perlvar
"&gt;&lt;tt&gt;$|=1&lt;/tt&gt;&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Check—carefully, if on an unknown system—for &lt;acronym title="Not a File System"&gt;NFS&lt;/acronym&gt; or other remote filesystems, as these may wedge a process in the kernel. The degree of care required depends on the ease with which a new shell can be opened, or a hung &lt;a href="http://www.freebsd.org/cgi/man.cgi?query=df"&gt;&lt;tt&gt;df(1)&lt;/tt&gt;&lt;/a&gt; command worked around. If possible, work under &lt;a href="http://www.gnu.org/software/screen/"&gt;&lt;tt&gt;screen&lt;/tt&gt;&lt;/a&gt; or &lt;a href="http://dtach.sourceforge.net/"&gt;&lt;tt&gt;dtach&lt;/tt&gt;&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Otherwise, is the hang due to the process waiting for terminal input? Or is it buffering for output? Waiting for input from a local filesystem or remote network resource? Waiting on a child process? These questions can be answered with &lt;tt&gt;lsof&lt;/tt&gt;, or a &lt;a href="http://bhami.com/rosetta.html"&gt;system tracing utility&lt;/a&gt; such as &lt;a href="http://www.freebsd.org/cgi/man.cgi?query=ktrace"&gt;&lt;tt&gt;ktrace(1)&lt;/tt&gt;&lt;/a&gt; or &lt;tt&gt;strace&lt;/tt&gt;. These tools, while complex, are &lt;a href="http://sial.org/howto/debug/unix/#s7"&gt;fairly easy to learn&lt;/a&gt;. Does the process hang after a specific system call, such as &lt;tt&gt;read(2)&lt;/tt&gt;? Or does it loop on a particular system call or sequence of system calls? Read the &lt;tt&gt;man&lt;/tt&gt; page if unfamiliar with the call.&lt;/p&gt;&lt;p&gt;If possible, trace &lt;tt&gt;perl -e '$i=&lt;STDIN&gt;'&lt;/tt&gt; or &lt;tt&gt;grep something&lt;/tt&gt; to see how a process hangs on input. Also find and trace a process that hangs on output, or one that is wedged in a loop on a particular system call, so that identifying hung processes becomes easier in “the wild” of production.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If these steps do not resolve the problem, consult &lt;a href="http://www.mikeash.com/getting_answers.html"&gt;Getting Answers&lt;/a&gt; prior to seeking help from others.&lt;/p&gt;




&lt;!-- technorati tags start --&gt;&lt;p style="text-align:right;font-size:10px;"&gt;Technorati Tags: &lt;a href="http://www.technorati.com/tag/coding" rel="tag"&gt;coding&lt;/a&gt;, &lt;a href="http://www.technorati.com/tag/Perl" rel="tag"&gt;Perl&lt;/a&gt;, &lt;a href="http://www.technorati.com/tag/Unix" rel="tag"&gt;Unix&lt;/a&gt;&lt;/p&gt;&lt;!-- technorati tags end --&gt;
&lt;br /&gt;

		</content>
		<issued xmlns="http://purl.org/atom/ns#">2009-05-20T22:12:20-0700</issued>
		<link xmlns="http://purl.org/atom/ns#" type="text/html" rel="alternate" href="http://sial.org/blog/2009/05/heuristic_for_hung_code_in_per.html" title=""/>
		<id xmlns="http://purl.org/atom/ns#">358</id>
	</entry>
</feed>
