<html><head></head><body><p dir="ltr">Hi Maciek, </p>
<p dir="ltr">Of course it's a difference between pypy and cpython. if you close all fds, cpython's random module still works, but PyPy's doesn't. </p>
<p dir="ltr">Cheers, </p>
<br>
Carl Friedrich<br><br><div class="gmail_quote">On December 23, 2015 2:54:23 PM GMT+01:00, Maciej Fijalkowski <fijall@gmail.com> wrote:<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div dir="ltr">well, ok, but that does not sound like a pypy bug then - "close all existing fds and complain that some of them are closed" is a bit not good - maybe it's a bug in python-daemon and the PEP?</div><div class="gmail_extra"><br /><div class="gmail_quote">On Wed, Dec 23, 2015 at 3:36 PM, hubo <span dir="ltr"><<a href="mailto:hubo@jiedaibao.com" target="_blank">hubo@jiedaibao.com</a>></span> wrote:<br /><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><u></u>
<div style="BORDER-LEFT-WIDTH:0px;FONT-SIZE:10.5pt;FONT-FAMILY:微软雅黑;BORDER-RIGHT-WIDTH:0px;BORDER-BOTTOM-WIDTH:0px;COLOR:#000000;MARGIN:12px;LINE-HEIGHT:1.5;BORDER-TOP-WIDTH:0px" marginheight="0" marginwidth="0">
<div>No, the python-daemon module is critical in this problem, because it is the
python-daemon module who closed the fd to /dev/urandom. When process swith to
daemon, it forks itself, and then close all open fds (including stdin, stdout
and stderr), so it also closes the fd for /dev/urandom which is used by PyPy
library. It is the standard behavior defined by <a href="https://www.python.org/dev/peps/pep-3143/#daemoncontext-objects" target="_blank">https://www.python.org/dev/peps/pep-3143/#daemoncontext-objects</a>
and also the standard behavior for unix daemons. And unfortunately there is not
a way to prevent the fd to be closed without knowing exactly what number it is
on.</div>
<div> </div>
<div>Without python-daemon (or similar libraries), it is only possible to
reproduce the problem by closing the fd (usually 4) forcely, but it does not
make much sense.</div>
<div> </div>
<div> </div>
<div style="FONT-SIZE:10pt;FONT-FAMILY:Verdana;COLOR:#c0c0c0" align="left">2015-12-23
<hr style="BORDER-TOP:#c0c0c0 1px solid;min-height:1px;BORDER-RIGHT:0px;WIDTH:122px;BORDER-BOTTOM:0px;BORDER-LEFT:0px" align="left" />
<span>hubo</span> </div>
<hr style="BORDER-TOP:#c0c0c0 1px solid;min-height:1px;BORDER-RIGHT:0px;BORDER-BOTTOM:0px;BORDER-LEFT:0px" />
<blockquote style="FONT-SIZE:10pt;FONT-FAMILY:Verdana;PADDING-LEFT:0px;MARGIN-LEFT:0px">
<div><strong>发件人:</strong>Maciej Fijalkowski <<a href="mailto:fijall@gmail.com" target="_blank">fijall@gmail.com</a>></div>
<div><strong>发送时间:</strong>2015-12-23 21:22</div>
<div><strong>主题:</strong>Re: Re: Re: [pypy-dev] Dead loop occurs when using
python-daemon and multiprocessing together in PyPy 4.0.1</div><div><div class="h5">
<div><strong>收件人:</strong>"hubo"<<a href="mailto:hubo@jiedaibao.com" target="_blank">hubo@jiedaibao.com</a>></div>
<div><strong>抄送:</strong>"pypy-dev"<<a href="mailto:pypy-dev@python.org" target="_blank">pypy-dev@python.org</a>></div>
<div> </div>
<div>
<div dir="ltr">can you reproduce the OSError problem without having the daemon
module involved either?</div>
<div class="gmail_extra"><br />
<div class="gmail_quote">On Wed, Dec 23, 2015 at 3:14 PM, hubo <span dir="ltr"><<a href="mailto:hubo@jiedaibao.com" target="_blank">hubo@jiedaibao.com</a>></span> wrote:<br />
<blockquote class="gmail_quote" style="PADDING-LEFT:1ex;BORDER-LEFT:#ccc 1px solid;MARGIN:0px 0px 0px 0.8ex"><u></u>
<div style="BORDER-LEFT-WIDTH:0px;FONT-SIZE:10.5pt;FONT-FAMILY:微软雅黑;BORDER-RIGHT-WIDTH:0px;BORDER-BOTTOM-WIDTH:0px;COLOR:#000000;MARGIN:12px;LINE-HEIGHT:1.5;BORDER-TOP-WIDTH:0px" marginheight="0" marginwidth="0">
<div>I can only reproduce the <strong>OSError</strong> problem. Maybe the
CPU 100% is not really a dead lock, but rather some kind of automatic crash
report? Although it is quite easy to crash the program with os.urandom, it
only stops responding when the crash happens in system libraries
like multiprocessing or email.</div>
<div> </div>
<div>The posix.urandom problem is quite easy to reproduce:</div>
<div> </div>
<div>#!/usr/bin/pypy<br />import os<br />os.urandom(16)<br />def
test():<br /> print repr(os.urandom(16))<br />import
daemon<br />import sys<br />if __name__ == '__main__':<br />
with daemon.DaemonContext(initgroups=False,
stderr=sys.stderr,stdout=sys.stdout):<br />
test()</div>
<div> </div>
<div>(stderr and stdout is kept open to show console messages in the daemon.
initgroups=False is a workaround on python-daemon not working in
Python2.6)</div>
<div> </div>
<div>Or, with module random:</div>
<div> </div>
<div>#!/usr/bin/pypy<br />import random<br />def test():<br />
random.Random()<br />import daemon<br />import sys<br />if __name__ ==
'__main__':<br /> with
daemon.DaemonContext(initgroups=False,
stderr=sys.stderr,stdout=sys.stdout):<br />
test()<br /></div>
<div>And when run scripts with pypy:</div>
<div> </div>
<div>pypy test3.py</div>
<div> </div>
<div>it crashes with OSError:</div>
<div>Traceback (most recent call last):<br /> File "test2.py", line 13,
in <module><br /> test()<br /> File "test2.py",
line 6, in test<br /> random.Random()<br /> File
"/opt/pypy-4.0.1-linux_x86_64-portable/lib-python/2.7/random.py", line 95,
in __init__<br /> self.seed(x)<br /> File
"/opt/pypy-4.0.1-linux_x86_64-portable/lib-python/2.7/random.py", line 111,
in seed<br /> a = long(_hexlify(_urandom(2500)),
16)<br />OSError: [Errno 9] Bad file descriptor</div>
<div> </div>
<div>It is still not clear why it causes dead loop (or long-time no
responding) in multiprocessing (should have thrown an ImportError) and the
exact condition for the file descriptor of /dev/urandom appears (just call
os.urandom and import random does not reproduce the result), but I believe
it is definitely linked to the problem.</div>
<div> </div>
<div style="FONT-SIZE:10pt;FONT-FAMILY:Verdana;COLOR:#c0c0c0" align="left">2015-12-23
<hr style="BORDER-TOP:#c0c0c0 1px solid;BORDER-RIGHT:0px;WIDTH:122px;BORDER-BOTTOM:0px;MIN-HEIGHT:1px;BORDER-LEFT:0px" align="left" />
<span>hubo</span> </div>
<hr style="BORDER-TOP:#c0c0c0 1px solid;BORDER-RIGHT:0px;BORDER-BOTTOM:0px;MIN-HEIGHT:1px;BORDER-LEFT:0px" />
<blockquote style="FONT-SIZE:10pt;FONT-FAMILY:Verdana;PADDING-LEFT:0px;MARGIN-LEFT:0px">
<div><strong>发件人:</strong>Maciej Fijalkowski <<a href="mailto:fijall@gmail.com" target="_blank">fijall@gmail.com</a>></div>
<div><strong>发送时间:</strong>2015-12-23 20:07</div>
<div><strong>主题:</strong>Re: Re: [pypy-dev] Dead loop occurs when using
python-daemon and multiprocessing together in PyPy 4.0.1</div>
<div>
<div>
<div><strong>收件人:</strong>"hubo"<<a href="mailto:hubo@jiedaibao.com" target="_blank">hubo@jiedaibao.com</a>></div>
<div><strong>抄送:</strong>"pypy-dev"<<a href="mailto:pypy-dev@python.org" target="_blank">pypy-dev@python.org</a>></div>
<div> </div>
<div>
<div dir="ltr">That's very interesting, can you produce a standalone example
that does not use multiprocessing? That would make it much easier to fix
the bug (e.g. os.fork followed by os.urandom failing)</div>
<div class="gmail_extra"><br />
<div class="gmail_quote">On Wed, Dec 23, 2015 at 1:54 PM, hubo <span dir="ltr"><<a href="mailto:hubo@jiedaibao.com" target="_blank">hubo@jiedaibao.com</a>></span> wrote:<br />
<blockquote class="gmail_quote" style="PADDING-LEFT:1ex;BORDER-LEFT:#ccc 1px solid;MARGIN:0px 0px 0px 0.8ex"><u></u>
<div style="BORDER-LEFT-WIDTH:0px;FONT-SIZE:10.5pt;FONT-FAMILY:微软雅黑;BORDER-RIGHT-WIDTH:0px;BORDER-BOTTOM-WIDTH:0px;COLOR:#000000;MARGIN:12px;LINE-HEIGHT:1.5;BORDER-TOP-WIDTH:0px" marginheight="0" marginwidth="0">
<div>Thanks for the response. Should I put it directly in the bug
tracker?</div>
<div> </div>
<div>FYI, I've located the reason to be the incompatibility with
python-daemon (or rather the standard unix-daemon behavior) and
PyPy <strong>posix.urandom</strong> implementation. </div>
<div> </div>
<div>It seems that in PyPy 4.0.1, when module <strong>random</strong>
loaded, a file descriptor is created on /dev/urandom. I think PyPy
implementation use the shared descriptor to read from /dev/urandom.
Sadly when python-daemon fork the process and turns it into an unix
daemon, it closes all the currently open file descriptors. After that
all os.urandom calls failed with OSError. I think maybe the other
functions of Random class is also using the file descriptor in C
code and just never detects if the return value is 0, and
causes the dead loop.</div>
<div> </div>
<div>I think the problem will be solved if the
implementation re-open the handle when it is
closed somehow.</div>
<div> </div>
<div>multiprocessing is using random internally. Also there
are lots of other modules using random, like email etc. The dead
loop occurs when you use any of the libraries in a daemon.</div>
<div> </div>
<div> </div>
<div> </div>
<div style="FONT-SIZE:10pt;FONT-FAMILY:Verdana;COLOR:#c0c0c0" align="left">2015-12-23
<hr style="BORDER-TOP:#c0c0c0 1px solid;BORDER-RIGHT:0px;WIDTH:122px;BORDER-BOTTOM:0px;MIN-HEIGHT:1px;BORDER-LEFT:0px" align="left" />
<span>hubo</span> </div>
<hr style="BORDER-TOP:#c0c0c0 1px solid;BORDER-RIGHT:0px;BORDER-BOTTOM:0px;MIN-HEIGHT:1px;BORDER-LEFT:0px" />
<blockquote style="FONT-SIZE:10pt;FONT-FAMILY:Verdana;PADDING-LEFT:0px;MARGIN-LEFT:0px">
<div><strong>发件人:</strong>Maciej Fijalkowski <<a href="mailto:fijall@gmail.com" target="_blank">fijall@gmail.com</a>></div>
<div><strong>发送时间:</strong>2015-12-23 19:35</div>
<div><strong>主题:</strong>Re: [pypy-dev] Dead loop occurs when using
python-daemon and multiprocessing together in PyPy 4.0.1</div>
<div><strong>收件人:</strong>"hubo"<<a href="mailto:hubo@jiedaibao.com" target="_blank">hubo@jiedaibao.com</a>></div>
<div><strong>抄送:</strong>"pypy-dev"<<a href="mailto:pypy-dev@python.org" target="_blank">pypy-dev@python.org</a>></div>
<div>
<div>
<div> </div>
<div>
<div dir="ltr">Hi hubo
<div><br /></div>
<div>Can you put it as a bug report? Those things get easily lost on
the mailing list (and sadly I won't look at it right now,
multiprocessing scares me)</div></div>
<div class="gmail_extra"><br />
<div class="gmail_quote">On Wed, Dec 23, 2015 at 12:03 PM, hubo <span dir="ltr"><<a href="mailto:hubo@jiedaibao.com" target="_blank">hubo@jiedaibao.com</a>></span> wrote:<br />
<blockquote class="gmail_quote" style="PADDING-LEFT:1ex;BORDER-LEFT:#ccc 1px solid;MARGIN:0px 0px 0px 0.8ex"><u></u>
<div style="BORDER-LEFT-WIDTH:0px;FONT-SIZE:10.5pt;FONT-FAMILY:微软雅黑;BORDER-RIGHT-WIDTH:0px;BORDER-BOTTOM-WIDTH:0px;COLOR:#000000;MARGIN:12px;LINE-HEIGHT:1.5;BORDER-TOP-WIDTH:0px" marginheight="0" marginwidth="0"><u></u>
<div>Hello devs,</div>
<div> </div>
<div>A (possible) dead loop is found when I use python-daemon and
multiprocessing together in PyPy 4.0.1, which does not appear in
Python(2.6 or 2.7). Also it does not appear in earlier PyPy versions
(2.0.2)</div>
<div> </div>
<div><strong>Reproduce</strong>:</div>
<div> </div>
<div>First install python-daemon:</div>
<div>pypy_pip install python-daemon</div>
<div> </div>
<div>Use the following test script (also available in
attachment):</div>
<div> </div>
<div>#!/usr/bin/pypy<br />import daemon<br />import
multiprocessing<br />def test():<br /> q =
multiprocessing.Queue(64)<br />if __name__ ==
'__main__':<br /> with
daemon.DaemonContext():<br />
test()</div>
<div> </div>
<div>When executing the script with pypy:</div>
<div>pypy test.py</div>
<div> </div>
<div>The background service does not exit, and is consuming 100%
CPU:</div>
<div>ps aux | grep pypy<br />root 7769
99.1 0.5 235332 46812
? R
17:52 2:09 pypy
test.py<br />root 7775 0.0
0.0 103252 804 pts/1 S+
17:54 0:00 grep pypy</div>
<div> </div>
<div><img src="cid:flashmail$K6aXDxLY$1450877768__0@nmmp" /></div>
<div> </div>
<div> </div>
<div>Executing the script with python:</div>
<div>python2.7 test.py</div>
<div>And the background service normally exits.</div>
<div> </div>
<div><strong>Environment:</strong></div>
<div>I'm using CentOS 6.5, with portable PyPy distribution for linux
(<a href="https://bitbucket.org/squeaky/portable-pypy/downloads/pypy-4.0.1-linux_x86_64-portable.tar.bz2" target="_blank">https://bitbucket.org/squeaky/portable-pypy/downloads/pypy-4.0.1-linux_x86_64-portable.tar.bz2</a>)</div>
<div>I run the script on system built-in python (python 2.6.6), a
compiled CPython (2.7.11), and pypy from epel-release(pypy 2.0.2,
python 2.7.2), and the problem does not appear. Though the compiled
CPython is 2.7.11 and PyPy 4.0.4 is python 2.7.10, I think that
does not matter much.</div>
<div> </div>
<div>Please contact if you have any questions or ideas.</div>
<div> </div>
<div> </div>
<div style="FONT-SIZE:10pt;FONT-FAMILY:Verdana;COLOR:#c0c0c0">
<div align="left">2015-12-23</div>
<hr style="BORDER-TOP:#c0c0c0 1px solid;BORDER-RIGHT:0px;WIDTH:122px;BORDER-BOTTOM:0px;MIN-HEIGHT:1px;BORDER-LEFT:0px" align="left" />
<span><font color="#888888"><span>hubo</span>
</font></span></div><u></u></div><br />_______________________________________________<br />pypy-dev
mailing list<br /><a href="mailto:pypy-dev@python.org" target="_blank">pypy-dev@python.org</a><br /><a href="https://mail.python.org/mailman/listinfo/pypy-dev" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/pypy-dev</a><br /><br /></blockquote></div><br /></div></div></div></div></blockquote></div></blockquote></div><br /></div></div></div></div></blockquote></div></blockquote></div><br /></div></div></div></div></blockquote></div></blockquote></div><br /></div>
<p style="margin-top: 2.5em; margin-bottom: 1em; border-bottom: 1px solid #000"></p><pre class="k9mail"><hr /><br />pypy-dev mailing list<br />pypy-dev@python.org<br /><a href="https://mail.python.org/mailman/listinfo/pypy-dev">https://mail.python.org/mailman/listinfo/pypy-dev</a><br /></pre></blockquote></div></body></html>