Bug #17801
closedrepeated read from PTY blocks all Fibers
Description
Ruby version:
ruby 3.0.1p64 (2021-04-05 revision 0fb782ee38) [x86_64-linux]
used Scheduler:
https://github.com/ruby/ruby/blob/master/test/fiber/scheduler.rb
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 ioquatix (Samuel Williams) almost 3 years ago
- Description updated (diff)
- Assignee set to ioquatix (Samuel Williams)
Investigating.
Updated by pebauer (peter bauer) almost 3 years ago
expected output from script on cli:
date
Di 13 Apr 2021 21:51:20 CEST
test
date
Di 13 Apr 2021 21:51:21 CEST
test
Updated by pebauer (peter bauer) almost 3 years ago
ioquatix (Samuel Williams) wrote in #note-1:
Investigating.
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
Fiber.schedule do
loop do
puts master.gets if master.ready?
sleep 0.1
end
end
Workaround 2: # shows much better performance than Workraround 1
Fiber.schedule do # never try to read more bytes then available
loop do
puts master.read(master.nread) if master.nread > 0
sleep 0.1
end
end`
Updated by ioquatix (Samuel Williams) almost 3 years ago
This works for me if I set:
master.nonblock = true
Can you try it and report back?
We could consider making this the default.
Updated by pebauer (peter bauer) almost 3 years ago
Yes, this works. Thank you.
master.nonblock = true
Updated by hsbt (Hiroshi SHIBATA) almost 3 years ago
- Status changed from Open to Closed