-
Notifications
You must be signed in to change notification settings - Fork 71
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
PtyProcess.spawn (and thus pexpect) is not safe for use in multithreaded applications #43
Comments
as the maintainer of Python's subprocess module, if there are API enhancements you want from subprocess to be able to make popen_spawn better and be your default implementation instead of ptyprocess.pty_spawn, please let me know what they are. |
Another issue is, ptyprocess is using its own implementation of closing fds before doing The solution would be to use subprocess (or subprocess32 for Python 2) -- it looks at |
@gpshead belatedly, thanks for the offer. I'm not familiar enough with these kinds of restrictions to write all the pty setup code in low-level C. And even if I could, I'd be wary of such a big rewrite of old code that's in widespread use which I don't know about. I've been bitten before by trying to refactor pexpect and unwittingly breaking people's use cases. Maybe one option would be for |
(I realise that, since Ptyprocess is essentially meant to be a mirror of Popen for working with subprocesses in a pty, I basically just suggested you take over maintenance of it and make it part of the stdlib. This isn't really a reasonable request, but unfortunately I don't see any small piece of functionality you could provide to enable ptyprocess to live on top. Although maybe it's less effort than I imagine given all the code already in subprocess. In any case, it's not a demand :-) |
We found that the I'm wondering if there's a path forward here. Would you be willing to accept a PR that changes this package to use a C extension to implement |
I would've guessed that jupyter would use subprocess to implement But from this pexpect/ptyprocess projects perspective and from users perspective, waiting on CPython to have a feature would be a long wait. Your own extension could make sense. |
Just the same as the old pure Python
subprocess
module from Python 2, thePtyProcess.spawn
method is not async signal safe. The entirefork()
toexec
code path must be written in async signal safe C code. Bothpty.fork
and your ownPtyProcess.spawn
method violate this constraint.See the Python 3
subprocess
module's_posixsubprocess
extension module (and the https://pypi.python.org/pypi/subprocess32/ backport for Python 2 users).Likely symptoms when this bites you: random seeming deadlocks and hangs.
Workaround for users today: Do not use
pexpect.spawn
orpexpect.pty_spawn
in a multithreaded application.A
pexpect.popen_spawn
module appears to exist and will use thesubprocess
module. If you are on 2.7 make sure you've replaced sys.modules['subprocess'] with a reference to thesubprocess32
module first (if you haven't already done so within your Python 2.7 interpreter's stdlib)The text was updated successfully, but these errors were encountered: