ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: melentievm@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:90255] [Ruby trunk Bug#15367] IO.select is not resumed when io-object gets closed
Date: Mon, 03 Dec 2018 09:42:03 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-75372.20181203094202.1dad4380ac0bc110@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15367.20181202164852@ruby-lang.org

Issue #15367 has been updated by printercu (Max Melentiev).


I'm using `IO.select` with ssl socket as it's suggested in docs https://ruby-doc.org/core-2.5.3/IO.html#method-c-select :

~~~ruby
    def read_from_socket
      socket.read_nonblock(read_buffer_size)
    rescue IO::WaitReadable
      IO.select([socket.to_io])
      retry
    rescue IO::WaitWritable
      IO.select(nil, [socket.to_io])
      retry
    end
~~~

Other code is like this (simplified):

~~~ruby

def run
  loop do
    data = read_from_socket
    process(data)
  end
rescue Errno::EBADF, IOError
  raise unless @stopped
end

def stop
  @stopped = true
  socket.close
end

Signal.trap('TERM') { Thread.new { stop } } # thread is just workaround for trap contex
run
# exit
~~~

I can't use just `Thread#kill` to not stop thread while it runs `#process`.
It works fine on macOS because `socket.close` makes read_from_socket fail with Errno::EBADF which is rescued later.
However this does not work on linux and `#run` hangs forever.

Is there a right way for this task to not face multi-thread limitations of `IO.select`?

For now I use workaround:

~~~ruby
    SELECT_TIMEOUT = 10

    def read_from_socket
      socket.read_nonblock(read_buffer_size)
    rescue IO::WaitReadable
      nil until IO.select([socket.to_io], nil, nil, SELECT_TIMEOUT)
      retry
    rescue IO::WaitWritable
      nil until IO.select(nil, [socket.to_io], nil, SELECT_TIMEOUT)
      retry
    end
~~~

----------------------------------------
Bug #15367: IO.select is not resumed when io-object gets closed
https://bugs.ruby-lang.org/issues/15367#change-75372

* Author: printercu (Max Melentiev)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN
----------------------------------------
Here is sample code:

~~~ ruby
rp, wp = IO.pipe
t2 = Thread.new { IO.select([rp]) }
# This also does not work:
# t2 = Thread.new { IO.select([rp], nil, [rp]) }
sleep 0.01
rp.close
t2
# => #<Thread:0x00000000089b6ce0@(pry):51 sleep>
~~~

It happens only on linux, tested with 2.5.1, 2.6.0-preview2. On macOS it gives error, as expected:
~~~
#<Thread:0x00007fab3aebce58@(pry):5 run> terminated with exception (report_on_exception is true):
Traceback (most recent call last):
	1: from (pry):5:in `block in <main>'
(pry):5:in `select': Bad file descriptor (Errno::EBADF)
> t2
=> #<Thread:0x00007fab3aebce58@(pry):5 dead>
~~~



-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2018-12-03  9:42 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-15367.20181202164852@ruby-lang.org>
2018-12-02 16:48 ` [ruby-core:90226] [Ruby trunk Bug#15367] IO.select is not resumed when io-object gets closed melentievm
2018-12-02 17:09   ` [ruby-core:90228] " Eric Wong
2018-12-02 18:35 ` [ruby-core:90229] " shevegen
2018-12-02 20:08   ` [ruby-core:90231] " Eric Wong
2018-12-02 18:59 ` [ruby-core:90230] " Greg.mpls
2018-12-03  9:42 ` melentievm [this message]
2018-12-03 10:18   ` [ruby-core:90257] " Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-75372.20181203094202.1dad4380ac0bc110@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).