merijn / posix-pty Goto Github PK
View Code? Open in Web Editor NEWThis package simplifies the creation of subprocesses that interact with their parent via a pseudo terminal (see man pty).
License: BSD 3-Clause "New" or "Revised" License
This package simplifies the creation of subprocesses that interact with their parent via a pseudo terminal (see man pty).
License: BSD 3-Clause "New" or "Revised" License
If spawnWithPty
is called at an unlucky time (i.e. when other system calls like printing to stderr and launching other processes are happening) then some kind of bad interaction with the runtime can get triggered, resulting in either full CPU livelock (stuck at 100% across all CPUs) or freezing of the Haskell process.
A nice repro (courtesy of u/Noughtmare on Reddit) is below. You can also find my original, slightly more complicated version as a full repo here.
module Main where
import Control.Concurrent
import Control.Monad
import System.Posix.Pty
import System.Process
main :: IO ()
main = do
forkIO $ replicateM_ 1000 $ putStrLn "This is a test"
-- Repeat this 20 times
replicateM_ 20 $ do
forkIO $ void $ spawnWithPty Nothing True "sleep" ["99999999"] (80, 24)
forkIO $ void $ readCreateProcess (proc "echo" ["hi"]) ""
-- All CPUs should now be stuck at 100%
threadDelay 60000000
I'm filing this issue after a Reddit discussion which determined that posix-pty
most likely needs to be more careful about how it forks. You can see the discussion here: https://www.reddit.com/r/haskell/comments/d4h7ty/why_does_this_code_livelock_all_my_cpus_at_100/
Currently only compiles on OSX, fix to at least support FreeBSD, OSX and linux.
While using pty-wrap on output of tools like nix-instantiate
, hGetSome
encounters GHC.IO buffering and tryReadPty
assumes the protocol that relies on using read()
directly.
The fix to this has been included to #5 that has been left ignored for a while.
I'm using posix-pty to write a program that is similar to the script
command, that runs bash in a pty and forwards input and output between it and the user's terminal. For some reason, control-d is not being handled in the usual way. Usually, when at the bash prompt and nothing has been typed, control-d will be caught by bash and it runs "exit". Using posix-pty, instead "^D" gets displayed, and bash does not exit.
My program was earlier not using posix-pty and was setting up the pty itself and running a program connected to it using System.Process. In that version, control-d worked as expected.
My program is reading "\EOT" from the user, and is feeding that into writePty.
I suspect that something about how posix-pty sets up the pty is causing the problem. Perhaps something in fork_exec_with_pty
?
You can find the two versions of my program here:
Right now, if you call spawnWithPty
too many times (~128), the commands fails, even if the process being created for each Pty
has terminated.
Perhaps a ptyClose
would help here, but generally some way of returning the Pty
UNIX resource.
ptyClose :: Pty -> IO ()
The package I am working on is https://github.com/andygill/plist-buddy, a wrapper around the OSX plist-buddy command, and the quickcheck tests are failing because of this.
Is there a good reason Pty
is abstract? I would like to access the underlying Fd
to, for example, run select
on it.
Hello, the function spawnWithPty allows setting the environment for the new process, but it is missing an argument to set the current working directory of the new process.
For reference, see the cwd
field of CreateProcess
Right now, the workaround for those using this library is to call setCurrentDirectory just before calling spawnWithPty
, but this is dangerous and can interfere with other threads.
Thank you
Thank you for this library!
The test suite does not pass. Is this a known issue, or is there a bug?
iris ~/tmp % cabal get posix-pty
Unpacking to posix-pty-0.2.1.1/
iris ~/tmp % cd posix-pty-0.2.1.1
iris ~/tmp/posix-pty-0.2.1.1 % cabal sandbox init
Writing a default package environment file to
/home/spwhitton/tmp/posix-pty-0.2.1.1/cabal.sandbox.config
Creating a new sandbox at /home/spwhitton/tmp/posix-pty-0.2.1.1/.cabal-sandbox
iris ~/tmp/posix-pty-0.2.1.1 % cabal test
Package has never been configured. Configuring with default flags. If this
fails, please run configure manually.
Resolving dependencies...
Configuring posix-pty-0.2.1.1...
Preprocessing library posix-pty-0.2.1.1...
[1 of 1] Compiling System.Posix.Pty ( System/Posix/Pty.hs, dist/build/System/Posix/Pty.o )
In file included from /usr/include/x86_64-linux-gnu/sys/ioctl.h:21:0: error:
0,
from cbits/fork_exec_with_pty.c:2:
/usr/include/features.h:148:3: error:
warning: #warning "_BSD_SOURCE and _SVID_SOURCE are deprecated, use _DEFAULT_SOURCE" [-Wcpp]
# warning "_BSD_SOURCE and _SVID_SOURCE are deprecated, use _DEFAULT_SOURCE"
^~~~~~~
In file included from /usr/include/x86_64-linux-gnu/sys/ioctl.h:21:0: error:
0,
from cbits/fork_exec_with_pty.c:2:
/usr/include/features.h:148:3: error:
warning: #warning "_BSD_SOURCE and _SVID_SOURCE are deprecated, use _DEFAULT_SOURCE" [-Wcpp]
# warning "_BSD_SOURCE and _SVID_SOURCE are deprecated, use _DEFAULT_SOURCE"
^~~~~~~
Preprocessing test suite 'stty' for posix-pty-0.2.1.1...
[1 of 1] Compiling Main ( tests/stty.hs, dist/build/stty/stty-tmp/Main.o )
Linking dist/build/stty/stty ...
Running 1 test suites...
Test suite stty: RUNNING...
speed 38400 baud;
rows 10; columns 10;
line = 0;
intr = ^C;
quit = ^\;
erase = ^?;
kill = ^U;
eof = ^D;
eol = <undef>;
eol2 = <undef>;
swtch = <undef>;
start = ^Q;
stop = ^S;
susp = ^Z;
rprnt = ^R;
werase = ^W;
lnext = ^V;
discard = ^O;
min = 1; time = 0;
-parenb
-parodd
-cmspar cs8
-hupcl
-cstopb
cread
-clocal
-crtscts
-ignbrk
-brkint
-ignpar
-parmrk
-inpck
-istrip
-inlcr
-igncr
icrnl ixon
-ixoff
-iuclc
-ixany
-imaxbel
-iutf8
opost
-olcuc
-ocrnl
onlcr
-onocr
-onlret
-ofill
-ofdel nl0
cr0 tab0
bs0 vt0 ff0
isig icanon
iexten echo
echoe echok
-echonl
-noflsh
-xcase
-tostop
-echoprt
echoctl
echoke
-flusho
-extproc
stty: fdReadBuf: hardware fault (Input/output error)
Test suite stty: FAIL
Test suite logged to: dist/test/posix-pty-0.2.1.1-stty.log
0 of 1 test suites (0 of 1 test cases) passed.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.