Repository navigation
test: fix pty test hangs on aix #28600
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
test: fix pty test hangs on aix
Some pty tests persistently hung on the AIX CI buildbots. Fix that by adding a helper script that properly sets up the pty before spawning the script under test. On investigation I discovered that the test runner hung when it tried to close the slave pty's file descriptor, probably due to a bug in AIX's pty implementation. I could reproduce it with a short C program. The test runner also leaked file descriptors to the child process. I couldn't convince python's `subprocess.Popen()` to do what I wanted it to do so I opted to move the logic to a helper script that can do fork/setsid/etc. without having to worry about stomping on state in tools/test.py. In the process I also uncovered some bugs in the pty module of the python distro that ships with macOS 10.14, leading me to reimplement a sizable chunk of the functionality of that module. And last but not least, of course there are differences between ptys on different platforms and the helper script has to paper over that. Of course. Really, this commit took me longer to put together than I care to admit. Caveat emptor: this commit takes the hacky ^D feeding to the slave out of tools/test.py and puts it in the *.in input files. You can also feed other control characters to tests, like ^C or ^Z, simply by inserting them into the corresponding input file. I think that's nice. Fixes: nodejs/build#1820 Fixes: #28489
- Loading branch information
commit 686285ab0069f6c0d19f968f1788e7403199cce2
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import errno | ||
| import os | ||
| import pty | ||
| import select | ||
| import signal | ||
| import sys | ||
| import termios | ||
|
|
||
| STDIN = 0 | ||
| STDOUT = 1 | ||
| STDERR = 2 | ||
|
|
||
|
|
||
| def pipe(sfd, dfd): | ||
| try: | ||
| data = os.read(sfd, 256) | ||
| except OSError as e: | ||
| if e.errno != errno.EIO: | ||
| raise | ||
| return True # EOF | ||
|
|
||
| if not data: | ||
| return True # EOF | ||
|
|
||
| if dfd == STDOUT: | ||
| # Work around platform quirks. Some platforms echo ^D as \x04 | ||
| # (AIX, BSDs) and some don't (Linux). | ||
| filt = lambda c: ord(c) > 31 or c in '\t\n\r\f' | ||
| data = filter(filt, data) | ||
|
|
||
| while data: | ||
| try: | ||
| n = os.write(dfd, data) | ||
| except OSError as e: | ||
| if e.errno != errno.EIO: | ||
| raise | ||
| return True # EOF | ||
| data = data[n:] | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| argv = sys.argv[1:] | ||
|
|
||
| # Make select() interruptable by SIGCHLD. | ||
| signal.signal(signal.SIGCHLD, lambda nr, _: None) | ||
|
|
||
| master_fd, slave_fd = pty.openpty() | ||
| assert master_fd > STDIN | ||
|
|
||
| mode = termios.tcgetattr(slave_fd) | ||
| # Don't translate \n to \r\n. | ||
| mode[1] = mode[1] & ~termios.ONLCR # oflag | ||
| # Disable ECHOCTL. It's a BSD-ism that echoes e.g. \x04 as ^D but it | ||
| # doesn't work on platforms like AIX and Linux. I checked Linux's tty | ||
| # driver and it's a no-op, the driver is just oblivious to the flag. | ||
| mode[3] = mode[3] & ~termios.ECHOCTL # lflag | ||
| termios.tcsetattr(slave_fd, termios.TCSANOW, mode) | ||
|
|
||
| pid = os.fork() | ||
| if not pid: | ||
| os.setsid() | ||
| os.close(master_fd) | ||
|
|
||
| # Ensure the pty is a controlling tty. | ||
| name = os.ttyname(slave_fd) | ||
| fd = os.open(name, os.O_RDWR) | ||
| os.dup2(fd, slave_fd) | ||
| os.close(fd) | ||
|
|
||
| os.dup2(slave_fd, STDIN) | ||
| os.dup2(slave_fd, STDOUT) | ||
| os.dup2(slave_fd, STDERR) | ||
|
|
||
| if slave_fd > STDERR: | ||
| os.close(slave_fd) | ||
|
|
||
| os.execve(argv[0], argv, os.environ) | ||
| raise Exception('unreachable') | ||
|
|
||
| os.close(slave_fd) | ||
|
|
||
| fds = [STDIN, master_fd] | ||
| while fds: | ||
| try: | ||
| rfds, _, _ = select.select(fds, [], []) | ||
| except select.error as e: | ||
| if e[0] != errno.EINTR: | ||
| raise | ||
| if pid == os.waitpid(pid, os.WNOHANG)[0]: | ||
| break | ||
|
|
||
| if STDIN in rfds: | ||
| if pipe(STDIN, master_fd): | ||
| fds.remove(STDIN) | ||
|
|
||
| if master_fd in rfds: | ||
| if pipe(master_fd, STDOUT): | ||
| break | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| Hello! | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| Hello! | ||
| <Buffer 48 65 6c 6c 6f 21 0a> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is standard terminology, but I would really prefer a different term here, e.g.
master_fd(orparent_fd) andchild_fd? That’s imo both more accurate and doesn’t carry unnecessary negative connotations.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Going to go ahead and land this + open a fixup PR myself)