repeated read from PTY blocks all Fibers
ruby 3.0.1p64 (2021-04-05 revision 0fb782ee38) [x86_64-linux]
Example Code which should work:
require 'pty' require "./scheduler.rb" line_event = TracePoint.new(:line) do |t| p [t.lineno, t.method_id, t.path] end Fiber.set_scheduler(Scheduler.new) master, slave = PTY.open read, write = IO.pipe pid = spawn("bash", :in=>read, :out=>slave) read.close # we dont need the read slave.close # or the slave line_event.enable do Fiber.schedule do loop do puts "date" write.puts "date" sleep 1 end end Fiber.schedule do loop do puts master.gets.chomp puts master.gets.chomp # this line blocks all fibers, why ? puts "test" sleep 1 end end #write.close end
Updated by pebauer (peter bauer) 4 months ago
ioquatix (Samuel Williams) wrote in #note-1:
Hello Samuel, if found the following:
A read on a PTY master which is not ready seems to block all fibers.
I think not ready - means 0 Bytes to read, which can be checked via IO.nread
`Workaround 1: # read line by line
puts master.gets if master.ready?
Workaround 2: # shows much better performance than Workraround 1
Fiber.schedule do # never try to read more bytes then available
puts master.read(master.nread) if master.nread > 0