-
-
Notifications
You must be signed in to change notification settings - Fork 31.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test_input_tty hangs when run multiple times in the same process on macOS 10.15 #89050
Comments
(I'm still investigating at the moment whether something changed in my environment.) Running the following right now hangs on test_input_tty for me: ./python.exe -m test test_builtin test_builtin -v This fails on all branches up to and including 3.7, so I assume this is environment-specific unless it's a regression due to a change that was backported all the way back to 3.7, which is out of the question as the last functional commit on 3.7 was back in June. Things I tried so far:
The test in question is using deadline if available and |
Hynek confirmed on Big Sur with Python 3.9.5 from asdf that test_input_tty hangs, too, if ran for the second time in the same process. Moreover, readline is not it. First of all, it's libedit on macOS: ❯ ll /usr/lib/libreadline.dylib So Python uses that by default:
>>> import readline
>>> readline._READLINE_LIBRARY_VERSION
'EditLine wrapper'
>>> readline._READLINE_RUNTIME_VERSION
1026
>>> readline._READLINE_VERSION
1026
Unless you instruct it to use readline (for example by providing "-I$(brew --prefix readline)/include" to CFLAGS and "-L$(brew --prefix readline)/lib" to LDFLAGS before running ./configure):
>>> import readline
>>> readline._READLINE_LIBRARY_VERSION
'8.1'
>>> readline._READLINE_RUNTIME_VERSION
2049
>>> readline._READLINE_VERSION
2049 The hang is the same in both cases. Next course of action, checking if it's not due to fork shenanigans in _run_child(): cpython/Lib/test/test_builtin.py Line 2001 in 1841c70
|
Parent process hangs on:
where "name" in frame #7 is the "readinto" method of <_io.FileIO name=3 mode='rb' closefd=True> and "arg" is <memory at 0x102f5d090>. Child process hangs on:
|
This might be a long-standing problem. I haven't encountered it before because I was always running -R: with -j and in this case the test is skipped: test_input_tty (test.test_builtin.PtyTests) ... skipped 'stdin and stdout must be ttys' |
Amazingly, excluding every other test function with a bunch of This is very weird. Looking further. Semi-relatedly, I found BPO-26228, could reproduce it, and finished an open PR on it. While those are separate issues, I'm hoping to solve them both. |
I found the high-level reason why test_builtin hangs: it runs doctests as well. What's the root cause? I don't know yet. But to confirm, I can also hang the tests by running: $ python3.9 -m test test_doctest test_builtin -v Now to discover what it is that doctest does... |
The doctest runner sets an output redirecting debugger, which subclasses Pdb, around actually running the doctest. This action causes the hang. New finding, we can hang the test with test_pdb too: $ python3.9 -m test test_pdb test_builtin -v |
It *is* readline-related after all O_O Commenting out this section in Pdb.__init__ makes the issue go away: Lines 234 to 239 in 64a7812
time ./python.exe -E -Wd -m test test_builtin test_builtin == Tests result: SUCCESS == All 2 tests OK. Total duration: 1.3 sec I'll be continuing on this tomorrow to find the root cause. |
Is it related to https://bugs.python.org/issue41034 ? |
I've looked into this and the hang happens on this line: cpython/Lib/test/test_builtin.py Line 2030 in de3db14
So the issue is that on the second run, there's nothing to read on that fd. I've tried using os.stat to check if there's data on the fd, but it returned 0 data in both 1st and 2nd runs. However, if a small sleep is added before running os.stat, it does return size of data on 1st run and returns 0 on 2nd run, meaning it's possible to avoid the hang and error out instead (is that an improvement?) This is on MacOS 11.4 Big Sur by the way. This is my test debug branch: https://github.com/python/cpython/compare/main...akulakov:Test-check_input_tty-FIX?expand=1 |
Still seen on Ventura 13.5.2 in Python 3.13 (i.e., the main branch). Would it be possible to get fix? |
test_builtin.test_input_tty() calls forkpty(), whereas recent macOS versions are known to have issues with fork(). forkpty() manual page:
Maybe the test should be rewritten with openpty() and login_tty(), but replace fork() with subprocess.Popen()? int
__forkpty (int *pptmx, char *name, const struct termios *termp,
const struct winsize *winp)
{
int ptmx, terminal, pid;
if (openpty (&ptmx, &terminal, name, termp, winp) == -1)
return -1;
switch (pid = __fork ())
{
case -1:
__close (ptmx);
__close (terminal);
return -1;
case 0:
/* Child. */
__close (ptmx);
if (login_tty (terminal))
_exit (1);
return 0;
default:
/* Parent. */
*pptmx = ptmx;
__close (terminal);
return pid;
}
} |
It happens for me now every time I run
Even
😢 Is it related to a new REPL? |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: