Hello,
I want to use the PTY library to communicate with another local program.
I am running the example code and it seems to be deadlocking.
The example code calls this out as possible, and states:
"we will change the buffering type in the factor command, assuming that
factor uses stdio for stdout buffering.
If IO.pipe is used instead of ::open, this code deadlocks because
factor's stdout is fully buffered."
I take the above to mean that the example code has been written such that a
deadlock _won't_ occur, but I appear to be wrong. When I run the code it just
hangs.
l4@ubuntu:~$ ./pty_test.rb
If I tweak the example code to send invalid input to factor then I get a
response on stderr immediately:
l4@ubuntu:~$ ./pty_test.rb
factor: ‘42\\n’ is not a valid positive integer
... at which point it continues to spin.
Are the docs wrong? Am I doing something wrong? What's happening here?
Thanks,
Jefferson
Here's a link to the example code:
https://ruby-doc.org/stdlib-3.0.0/libdoc/pty/rdoc/PTY.html#method-c-spawn
And a more succint reproduction of the same problem (with the invalid input
described above):
#!/usr/bin/env ruby
require 'pty'
IO.pipe do |r,w|
PTY.open do |m,s|
pid = spawn("factor", in: r, out: s)
r.close
s.close
w.puts('42\n') # intentionally invalid to demonstrate stderr
puts(m.gets)
end
end