1# Make sure that libev child watchers, implicitly installed through the use
2# of subprocess, do not cause waitpid() to fail to poll for processes.
3# NOTE: This was only reproducible under python 2.
4from __future__ import print_function
5import gevent
6from gevent import monkey
7monkey.patch_all()
8
9import sys
10from multiprocessing import Process
11from subprocess import Popen, PIPE
12
13from gevent import testing as greentest
14
15def f(sleep_sec):
16    gevent.sleep(sleep_sec)
17
18
19
20class TestIssue600(greentest.TestCase):
21
22    __timeout__ = greentest.LARGE_TIMEOUT
23
24    @greentest.skipOnLibuvOnPyPyOnWin("hangs")
25    def test_invoke(self):
26        # Run a subprocess through Popen to make sure
27        # libev is handling SIGCHLD. This could *probably* be simplified to use
28        # just hub.loop.install_sigchld
29        # (no __enter__/__exit__ on Py2) pylint:disable=consider-using-with
30        p = Popen([sys.executable, '-V'], stdout=PIPE, stderr=PIPE)
31        gevent.sleep(0)
32        p.communicate()
33        gevent.sleep(0)
34
35    def test_process(self):
36        # Launch
37        p = Process(target=f, args=(0.5,))
38        p.start()
39
40        with gevent.Timeout(3):
41            # Poll for up to 10 seconds. If the bug exists,
42            # this will timeout because our subprocess should
43            # be long gone by now
44            p.join(10)
45
46
47if __name__ == '__main__':
48    greentest.main()
49