Project

General

Profile

Bug #15386

[PATCH] io.c (rb_io_check_char_readable): do not io_fflush buffered sockets

Added by normalperson (Eric Wong) 7 days ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:90339]

Description

This is a rare corner-case probably nobody cares about. (because
socket has IO#sync=false by default).  Not critical to fix
or change before 2.6 (I'm not sure if there's a compatibility risk).

Would much appreciate an extra set of eyes to review; but it can wait
for post-26.

[I tried taking a break from C and ruby-core to work on a different
 project today; but hit this bug within an hour of doing that :<]


io.c (rb_io_check_char_readable): do not io_fflush buffered sockets

I enabled userspace buffering on sockets to reduce syscall
overhead while avoiding non-portable TCP_CORK/TCP_NOPUSH.
This deadlocked for me because I was using independent threads
for reading and writing simultaneously on the same socket.

I also experimented with making io_fflush optionally
non-blocking, but that caused stream corruption with the reader
thread doing some writes.

  https://80x24.org/spew/20181206104008.29153-1-e@80x24.org/raw

Test script (hitting
  news://news.public-inbox.org/inbox.comp.version-control.git
  is fine)

require 'socket'
require 'uri'
require 'io/nonblock'
usage = "usage: #$0 news://news.public-inbox.org/inbox.comp.version-control.git"
uri = ARGV.shift or abort usage
uri = URI(uri)
uri.port ||= 119
group = uri.path.sub(%r{\A/+}, '') # String#delete_prefix requires Ruby 2.5+
s = Socket.tcp(uri.host, uri.port)
l = s.gets
l =~ /\A2\d\d / or abort "bad greeting: #{l}"
s.nonblock = true

s.puts "GROUP #{group}"
l = s.gets
code, _, min, max = l.chomp!.split.map!(&:to_i)
code == 211 or abort "bad GROUP response: #{l}"
rdr = Thread.new do
  nres = 0
  r = s.dup
  while l = r.gets
    l.start_with?('205 ') and break # cmd_quit
    l.start_with?('224 ') or abort "bad OVER response: #{l}"
    while l = r.gets
      if l == ".\r\n"
        nres += 1
        break
      end
    end
  end
  nres
end

range = min..max
s.sync = false
range.each { |i| s.puts "XOVER #{i}" }
puts "requests=#{range.size} #{Time.now}"
s.puts "QUIT"
s.flush
puts "responses=#{rdr.value} #{Time.now}"

Also available in: Atom PDF