-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
bpo-41494: Adds window resizing support to pty.spawn [ SIGWINCH ] #21752
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
Conversation
|
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA). CLA MissingOur records indicate the following people have not signed the CLA: For legal reasons we need all the people listed to sign the CLA before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. If you have recently signed the CLA, please wait at least one business day You can check yourself to see if the CLA has been received. Thanks again for the contribution, we look forward to reviewing it! |
|
The following are two [ very old ] stackoverflow threads that are relevant. https://stackoverflow.com/questions/6418678/resize-the-terminal-with-python |
| os.dup2(slave_fd, STDERR_FILENO) | ||
| if (slave_fd > STDERR_FILENO): | ||
| os.close(slave_fd) | ||
|
|
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.
C code equivalent to "_winresz" is
void _winresz(int pty_slave) {
struct winsize w;
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &w) == 0)
ioctl(pty_slave, TIOCSWINSZ, &w);
}
Notice that the above code checks if the TIOCGWINSZ call is a success before
making the TIOCSWINSZ call. Therefore, "_winresz" must always be called
in a try block.
References:
| w = struct.pack('HHHH', 0, 0, 0, 0) | ||
| s = fcntl.ioctl(STDIN_FILENO, termios.TIOCGWINSZ, w) | ||
| fcntl.ioctl(pty_slave, termios.TIOCSWINSZ, s) | ||
|
|
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.
The SIGWINCH handler depends on the pty slave fd returned by openpty in the body of "wspawn" [ see below ]; while the handler could just have been a function nested inside wspawn, that would make wspawn less readable. That is the reason for writing "_create_hwinch", which simply takes the pty slave fd as an argument and returns the appropriate signal handler function [ for SIGWINCH ].
References:
|
|
||
| os.close(master_fd) | ||
| return os.waitpid(pid, 0)[1] | ||
|
|
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.
Adding detailed comments for the reviewers. Most manpages referenced are from Linux. However, they should be similar on the BSDs.
Bugs:
- Since signal.signal() is being called by wspawn(), we can only call wspawn from the main thread. Even if that was not the case, since wspawn is setting the SIGWINCH handler globally, we should only have one instance of wspawn() running across threads anyway.
- wspawn() currently raises an exception if the initial attempt to set window size fails / if the attempt to register the SIGWINCH handler fails. Possible workaround: catch exception and try pty.spawn() instead.
- Can calling select with very short timeout result in high CPU usage? Not tested.
| except: | ||
| pass | ||
| return _hwinch | ||
|
|
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.
C code equivalent to "_cleanup" would use close(2), tcsetattr(3), and sigaction(2). This performs cleanup such as closing files, resetting tty attributes, and resetting the SIGWINCH signal handler. It is called right before wspawn returns/raises an exception.
References:
- https://man7.org/linux/man-pages/man2/close.2.html / https://docs.python.org/3/library/os.html?highlight=os%20close#os.close
- https://man7.org/linux/man-pages/man3/termios.3.html / https://docs.python.org/3/library/termios.html
- https://man7.org/linux/man-pages/man2/sigaction.2.html / https://docs.python.org/3/library/signal.html#signal.signal
| signal.signal(SIGWINCH, old_hwinch) | ||
| except: | ||
| pass | ||
|
|
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.
This paragraph explains "_ekill". If the parent loop [ see "_wcopy" below ] terminates due to an exception, then that exception is caught by wspawn. Then, we call waitpid on spawned child process so that it does not become a zombie. However, right before calling waitpid, the child process is sent SIGTERM. A second (*) after that, the child process, if alive, receives SIGKILL to ensure that it is no longer running.
(*) Will an adjustable sleep be more useful?
References:
| os.waitpid(child_pid, 0) | ||
| except: | ||
| pass | ||
|
|
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.
This is a modified version of "_copy". The differences are:
- select is called with a finite, adjustable timeout value.
- Calls waitpid [ with WNOHANG ] on the spawned child process periodically.
- While _copy does not return anything, _wcopy cleanly exits and returns the output of os.waitpid.
References:
| else: | ||
| _writen(master_fd, data) | ||
| return ret | ||
|
|
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.
"wspawn" is spawn+the following differences.
- It sets window size at the beginning.
- It registers a SIGWINCH handler.
- It does not depend on OSError to return. It does a clean return instead [ see "_wcopy" above ].
No. 3 above is related to
|
Added detailed comments to the diff. |
|
|
||
| # Author: Soumendra Ganguly. | ||
| SIGWINCH = signal.SIGWINCH | ||
|
|
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.
This was tested using Python 3.7 after commenting out the sys.audit lines.
https://docs.python.org/3/library/pty.html presents us with an example usage of pty.spawn. This example mimics script(1). However, the script(1) from util-linux has fantastic signal handing that pty.spawn does not directly provide. In fact, Lib/pty.py says "Bugs: No signal handling. Doesn't set slave termios and window size."
xterm(1) on Debian 10 GNU/Linux was used to test the pty.spawn example mentioned above; upon resizing the xterm(1) window, the output of programs such as ls(1) became scattered and hard to visually parse.
Currently, this patch does not modify any of the functions that are already present in Lib/pty.py. Instead, it exposes a new function called "wspawn" [ pty.wspawn ]. This is like pty.spawn + the following differences.
The aforementioned pty.spawn example now works well with window resizing if pty.wspawn is used in place of pty.spawn.
Signed-off-by: Soumendra Ganguly [email protected]
https://bugs.python.org/issue41494