pexpect/ptyprocess

Race condition in tests?

Closed this issue · 4 comments

There seems to be some kind of race condition in the testsuite on both python-3.4 and python-2.7 here on Fedora 21 (it seems to occour more often in python-3.4 though).

This is the log of the tests:
self = <tests.test_spawn.PtyTestCase testMethod=test_spawn_sh>

    def test_spawn_sh(self):
        env = os.environ.copy()
        env['FOO'] = 'rebar'
        p = PtyProcess.spawn(['sh'], env=env)
        p.read()
        p.write(b'echo $FOO\n')
        time.sleep(0.1)
        response = p.read()
        assert b'rebar' in response

        p.sendeof()
        p.read()

        with self.assertRaises(EOFError):
>           p.read()
E           AssertionError: EOFError not raised

tests/test_spawn.py:21: AssertionError

What could cause these random tests? How can I help debugging it?

It seems to happen in about half of the time in test_spawn_sh and so far once in test_spawn_unicode_sh.

I would imagine that the race is between the subprocess responding to the EOF and the parent process calling read(). If the first read doesn't get everything that the subprocess wrote, then the second read will succeed. Does putting a tiny sleep after p.sendeof() improve matters? I'd like to find a better solution than adding sleeps, but it should check if my guess about the problem is correct.

Yes, that's indeed the case. The first read sometimes just gets exit and not the \r\n bit thereafter.

What about calling p.readline() there instead of p.read()?