I was able to reproduce the bug. It is also present on (at least) x86-darwin. I would seem to have something to due with the top-level exception handler, but I haven't had a chance to investigate.<br><br><div class="gmail_quote">
On Tue, Sep 14, 2010 at 5:45 AM, Nicolas Bertolotti <span dir="ltr"><<a href="mailto:Nicolas.Bertolotti@mathworks.fr">Nicolas.Bertolotti@mathworks.fr</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div link="blue" vlink="purple" lang="FR">
<div>
<p class="MsoNormal"><span lang="EN-US">Hello,</span></p>
<p class="MsoNormal"><span lang="EN-US"> </span></p>
<p class="MsoNormal"><span lang="EN-US">In some cases, the toplevel handler may
enter an infinite loop. This can be highlighted using the following sample code
:</span></p>
<p class="MsoNormal"><span lang="EN-US">structure Main =</span></p>
<p class="MsoNormal"><span lang="EN-US">struct</span></p>
<p class="MsoNormal"><span lang="EN-US"> fun main() =</span></p>
<p class="MsoNormal"><span lang="EN-US">
let</span></p>
<p class="MsoNormal"><span lang="EN-US">
val _ = MLton.Signal.setHandler(Posix.Signal.pipe,
MLton.Signal.Handler.ignore);</span></p>
<p class="MsoNormal"><span lang="EN-US">
fun loop(i) =</span></p>
<p class="MsoNormal"><span lang="EN-US">
(TextIO.output(TextIO.stdErr, (Int.toString(i)) ^ "\n");</span></p>
<p class="MsoNormal"><span lang="EN-US">
if i <= 0 then</span></p>
<p class="MsoNormal"><span lang="EN-US">
()</span></p>
<p class="MsoNormal"><span lang="EN-US">
else</span></p>
<p class="MsoNormal"><span lang="EN-US">
(Posix.Process.sleep(Time.fromSeconds(1));</span></p>
<p class="MsoNormal"><span lang="EN-US">
loop(i - 1)));</span></p>
<p class="MsoNormal"><span lang="EN-US">
in</span></p>
<p class="MsoNormal"><span lang="EN-US">
loop(10);</span></p>
<p class="MsoNormal"><span lang="EN-US">
OS.Process.success</span></p>
<p class="MsoNormal"><span lang="EN-US">
end</span></p>
<p class="MsoNormal"><span lang="EN-US">
handle _ => (TextIO.output(TextIO.stdErr, "Exception raised\n");</span></p>
<p class="MsoNormal"><span lang="EN-US">
OS.Process.failure)</span></p>
<p class="MsoNormal"><span lang="EN-US"> </span></p>
<p class="MsoNormal"><span lang="EN-US"> val _ =
OS.Process.exit(main());</span></p>
<p class="MsoNormal"><span lang="EN-US">end;</span></p>
<p class="MsoNormal"><span lang="EN-US"> </span></p>
<p class="MsoNormal"><span lang="EN-US">If you do the following (linux platform):</span></p>
<p class="MsoNormal"><span lang="EN-US">$ mlton –output test test.sml</span></p>
<p class="MsoNormal"><span lang="EN-US">$ ./test 2>&1 | tee log.txt</span></p>
<p class="MsoNormal"><span lang="EN-US">And, in another console (within 10 seconds)
:</span></p>
<p class="MsoNormal"><span lang="EN-US">$ strace -p `ps -auxwww | grep test | grep
-v grep | awk '{ print $2 }'` > toto.log 2>&1 & kill `ps -auxwww
| grep tee | grep log | awk '{ print $2 }'`</span></p>
<p class="MsoNormal"><span lang="EN-US">(in order to log a “strace” of
the process in “toto.log” and kill the “tee” process so
that the standard error of the process becomes unaccessible)</span></p>
<p class="MsoNormal"><span lang="EN-US">the process then enters an infinite loop.</span></p>
<p class="MsoNormal"><span lang="EN-US"> </span></p>
<p class="MsoNormal"><span lang="EN-US">The “strace” log shows the
following messages:</span></p>
<p class="MsoNormal"><span lang="EN-US">“</span></p>
<p class="MsoNormal"><span lang="EN-US">Process 27026 attached - interrupt to quit</span></p>
<p class="MsoNormal"><span lang="EN-US">restart_syscall(<... resuming
interrupted call ...>) = 0</span></p>
<p class="MsoNormal"><span lang="EN-US">write(2, "8\n"...,
2)
= -1 EPIPE (Broken pipe)</span></p>
<p class="MsoNormal"><span lang="EN-US">--- SIGPIPE (Broken pipe) @ 0 (0) ---</span></p>
<p class="MsoNormal"><span lang="EN-US">write(2, "Exception raised\n"...,
17) = -1 EPIPE (Broken pipe)</span></p>
<p class="MsoNormal"><span lang="EN-US">--- SIGPIPE (Broken pipe) @ 0 (0) ---</span></p>
<p class="MsoNormal"><span lang="EN-US">write(2, "unhandled exception: Io:
outp"..., 86) = -1 EPIPE (Broken pipe)</span></p>
<p class="MsoNormal"><span lang="EN-US">--- SIGPIPE (Broken pipe) @ 0 (0) ---</span></p>
<p class="MsoNormal"><span lang="EN-US">write(2, "unhandled exception: Io:
outp"..., 86) = -1 EPIPE (Broken pipe)</span></p>
<p class="MsoNormal"><span lang="EN-US">--- SIGPIPE (Broken pipe) @ 0 (0) ---</span></p>
<p class="MsoNormal"><span lang="EN-US">write(2, "unhandled exception: Io:
outp"..., 86) = -1 EPIPE (Broken pipe)</span></p>
<p class="MsoNormal"><span lang="EN-US">…</span></p>
<p class="MsoNormal"><span lang="EN-US">“</span></p>
<p class="MsoNormal"><span lang="EN-US"> </span></p>
<p class="MsoNormal"><span lang="EN-US">I have managed to fix this in my code
(simply catch the exception when writing “Exception raised” in the
“main” function’s exception handler).</span></p>
<p class="MsoNormal"><span lang="EN-US">Anyway, it is still an issue that I may
have on other binaries and some other users may also face.</span></p>
<p class="MsoNormal"><span lang="EN-US"> </span></p>
<p class="MsoNormal"><span lang="EN-US">It appears that, when stderr is not
accessible, the toplevel handler which attempts to write its own message
generates an exception that causes the infinite loop.</span></p>
<p class="MsoNormal"><span lang="EN-US"> </span></p>
<p class="MsoNormal"><span lang="EN-US">I tried to fix the issue in
basis-library/mlton/exn.sml but I could not manage to eliminate the issue:</span></p>
<p><span lang="EN-US"><span>-<span style="font: 7pt "Times New Roman";">
</span></span></span><span lang="EN-US">The exceptions that may occur
in the call to “message” seem to be properly handled</span></p>
<p><span lang="EN-US"><span>-<span style="font: 7pt "Times New Roman";">
</span></span></span><span lang="EN-US">If I add a global ref that I
set to true the first time I enter the function and that prevents it from
running again, it has no effect.</span></p>
<p class="MsoNormal"><span lang="EN-US"> </span></p>
<p class="MsoNormal"><span lang="EN-US">Any thought?</span></p>
<p class="MsoNormal"><span lang="EN-US"> </span></p>
<p class="MsoNormal"><span lang="EN-US">Thanks in advance</span></p>
<p class="MsoNormal"><span lang="EN-US"> </span></p>
<p class="MsoNormal"><span lang="EN-US">Nicolas</span></p>
<p class="MsoNormal"><span lang="EN-US"> </span></p>
<table style="border-collapse: collapse;" align="left" border="0" cellpadding="0" cellspacing="0">
<tbody><tr>
<td style="width: 171.1pt; border-width: 1pt medium medium 1pt; border-style: solid none none solid; border-color: windowtext -moz-use-text-color -moz-use-text-color windowtext; background: none repeat scroll 0% 0% rgb(10, 42, 112); padding: 0cm;" valign="top" width="228">
<table style="border-collapse: collapse;" border="0" cellpadding="0" cellspacing="0">
<tbody><tr>
<td style="width: 169.6pt; padding: 0cm 5.4pt;" valign="top" width="226">
<p class="MsoNormal"><span style="color: white;" lang="EN-US"> </span></p>
<p class="MsoNormal"><a href="http://www.mathworks.fr/" title="http://www.mathworks.fr/" target="_blank"><span style="color: white; text-decoration: none;"><img src="" alt="cid:image001.gif@01C82D2B.AFBEF470" border="0" height="52" width="176"></span></a><u><span lang="EN-US"></span></u></p>
<p class="MsoNormal"><span style="font-size: 12pt;" lang="EN-US"> </span></p>
</td>
</tr>
</tbody></table>
</td>
<td style="width: 144.25pt; border-width: 1pt 1pt medium medium; border-style: solid solid none none; border-color: windowtext windowtext -moz-use-text-color -moz-use-text-color; background: none repeat scroll 0% 0% rgb(10, 42, 112); padding: 0cm;" width="192">
<table style="border-collapse: collapse;" border="0" cellpadding="0" cellspacing="0">
<tbody><tr>
<td style="width: 142.4pt; padding: 0cm 5.4pt;" width="190">
<p class="MsoNormal" style="text-align: right;" align="right"><span style="font-size: 10pt; color: white;" lang="EN-GB"><a href="http://www.mathworks.fr/" title="http://www.mathworks.fr/" target="_blank"><span style="color: white;">Accelerating the
pace of engineering and science</span></a></span></p>
</td>
</tr>
</tbody></table>
</td>
</tr>
<tr>
<td style="width: 171.1pt; border-width: medium medium medium 1pt; border-style: none none none solid; border-color: -moz-use-text-color -moz-use-text-color -moz-use-text-color windowtext; padding: 0cm;" valign="top" width="228">
<table style="width: 170.6pt; border-collapse: collapse;" border="0" cellpadding="0" cellspacing="0" width="227">
<tbody><tr>
<td style="width: 170.6pt; padding: 1.4pt 5.4pt;" valign="top" width="227">
<p class="MsoNormal"><b><span style="font-size: 10pt; color: rgb(0, 36, 112);" lang="EN-GB">Nicolas Bertolotti</span></b><span style="font-size: 10pt; color: rgb(0, 36, 112);" lang="EN-GB"> <br>
Senior Development Engineer</span><span style="font-size: 12pt;" lang="EN-GB"></span></p>
</td>
</tr>
</tbody></table>
</td>
<td style="width: 144.25pt; border-width: medium 1pt medium medium; border-style: none solid none none; border-color: -moz-use-text-color windowtext -moz-use-text-color -moz-use-text-color; padding: 0cm;" valign="top" width="192">
<table style="width: 143.75pt; border-collapse: collapse;" border="0" cellpadding="0" cellspacing="0" width="192">
<tbody><tr>
<td style="width: 143.75pt; padding: 1.4pt 5.4pt;" valign="top" width="192">
<p class="MsoNormal" style="text-align: right;" align="right"><span style="font-size: 10pt; color: rgb(0, 36, 112);">2 Rue de Paris<br>
92196 Meudon Cedex</span></p>
<p class="MsoNormal" style="text-align: right;" align="right"><span style="font-size: 10pt; color: rgb(0, 36, 112);">France</span></p>
</td>
</tr>
</tbody></table>
</td>
</tr>
<tr>
<td style="width: 171.1pt; border-width: medium medium 1pt 1pt; border-style: none none solid solid; border-color: -moz-use-text-color -moz-use-text-color windowtext windowtext; padding: 0cm;" valign="bottom" width="228">
<table style="border-collapse: collapse;" border="0" cellpadding="0" cellspacing="0">
<tbody><tr>
<td style="width: 165.35pt; padding: 1.4pt 5.4pt;" valign="top" width="220">
<p class="MsoNormal"><span style="font-size: 10pt; color: rgb(0, 36, 112);"><a href="mailto:Nicolas.Bertolotti@mathworks.fr" target="_blank">Nicolas.Bertolotti@mathworks.fr</a></span></p>
</td>
</tr>
</tbody></table>
</td>
<td style="width: 144.25pt; border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: -moz-use-text-color windowtext windowtext -moz-use-text-color; padding: 0cm;" valign="top" width="192">
<table style="border-collapse: collapse;" border="0" cellpadding="0" cellspacing="0">
<tbody><tr>
<td style="width: 46.4pt; padding: 1.4pt 5.4pt;" valign="top" width="62">
<p class="MsoNormal" style="text-align: right;" align="right"><span style="font-size: 10pt; color: rgb(0, 36, 112);">tel:<br>
fax:</span></p>
</td>
<td style="width: 97.35pt; padding: 1.4pt 5.4pt;" valign="top" width="130">
<p class="MsoNormal" style="text-align: right;" align="right"><span style="font-size: 10pt; color: navy;">+33.1.41.14.88.55</span></p>
<p class="MsoNormal" style="text-align: right;" align="right"><span style="font-size: 10pt; color: navy;">+33.1.55.64.06.64</span><span style="font-size: 10pt; color: rgb(0, 36, 112);"></span></p>
</td>
</tr>
</tbody></table>
</td>
</tr>
</tbody></table>
<p class="MsoNormal"> <span style="font-size: 12pt; font-family: "Times New Roman","serif";"></span></p>
<p class="MsoNormal"> </p>
</div>
</div>
<br>_______________________________________________<br>
MLton mailing list<br>
<a href="mailto:MLton@mlton.org">MLton@mlton.org</a><br>
<a href="http://mlton.org/mailman/listinfo/mlton" target="_blank">http://mlton.org/mailman/listinfo/mlton</a><br></blockquote></div><br>