ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: samuel@oriontransfer.net
To: ruby-core@ruby-lang.org
Subject: [ruby-core:103094] [Ruby master Bug#17527] rb_io_wait_readable/writable with scheduler don't check errno
Date: Tue, 30 Mar 2021 06:12:09 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-91158.20210330061208.48456@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-17527.20210111175935.48456@ruby-lang.org

Issue #17527 has been updated by ioquatix (Samuel Williams).


PR: https://github.com/ruby/ruby/pull/4338

----------------------------------------
Bug #17527: rb_io_wait_readable/writable with scheduler don't check errno 
https://bugs.ruby-lang.org/issues/17527#change-91158

* Author: ysbaddaden (Julien Portalier)
* Status: Open
* Priority: Normal
* Assignee: ioquatix (Samuel Williams)
* ruby -v: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]
* Backport: 2.5: DONTNEED, 2.6: DONTNEED, 2.7: DONTNEED, 3.0: UNKNOWN
----------------------------------------
## Problem

Playing with the new Fiber Scheduler, I noticed that `TCPServer#accept` would hung forever after closing the server from another Fiber. I expected it to be resumed and fail with IOError, as it happens with threads.

## Analysis

What happens is that the `accept4` call in `rsock_s_accept` fails and sets errno to `Errno::EBADF`, it then checks a few memory/limit related errnos, then calls `rb_io_wait_readable` expecting it to handle the current errno for IO errors. But when a scheduler is set, it immediately delegates to `Scheduler#io_wait` and doesn't check the current errno! In my case (nio4r), the `io_wait` hook returns a ready state, which causes `rsock_s_accept` to loop forever.

I tried to manually check in the `io_wait` hook whether the IO is closed, but the fd is never updated (AFAIK never set to -1) so `io.closed?` is always false. I'm not sure schedulers should check whether the fd is closed, thought.

## Proposed solution

A solution is to follow what happens for threads, and only check the scheduler when errno is EAGAIN or EWOULDBLOCK. I believe it's the only errors where we're expected to wait. This change also means that EINTR will be handled, too, and other errnos to raise an exception.

Instead of raising `IOError.new("closed stream")` as it happens for threads, it raises `Errno::EBADF` when a Scheduler is set. I suppose in the thread branches, it updates the IO at some point and calls `rb_io_check_closed` with the updated fd —maybe with `GetOpenFile` (`RB_IO_POINTER`) — and we ought to do the same at some point?

Another solution it to not delegate to the scheduler inside `rb_io_wait_readable` because it will eventually call `rb_wait_for_single_fd` that will check for the scheduler, but we can avoid some function calls, as well as thread-related debug information that could be confusing. It also won't raise help to raise IOError.

I'm attaching a patch that implements the first solution. It fixes both `rb_io_wait_readable` and `rb_io_wait_writable` since the latter may exhibit the same kind of issue in another scenario. This is speculative, I didn't hit one, yet.

---Files--------------------------------
rb_io_wait_methods_with_scheduler_skip_errno_checks.patch (1.52 KB)


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

  parent reply	other threads:[~2021-03-30  6:12 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-11 17:59 [ruby-core:102003] [Ruby master Bug#17527] rb_io_wait_readable/writable with scheduler don't check errno julien
2021-01-12  2:45 ` [ruby-core:102010] " nobu
2021-01-24 22:52 ` [ruby-core:102228] " samuel
2021-01-24 22:52 ` [ruby-core:102229] " samuel
2021-03-30  5:51 ` [ruby-core:103093] " samuel
2021-03-30  6:12 ` samuel [this message]
2021-03-30  7:02 ` [ruby-core:103096] " samuel
2021-03-30  7:38 ` [ruby-core:103097] " samuel
2021-03-30  8:15 ` [ruby-core:103098] " samuel
2021-04-23 23:18 ` [ruby-core:103579] " samuel
2021-04-24  4:14 ` [ruby-core:103580] " nagachika00
2021-04-24  4:17 ` [ruby-core:103581] " nagachika00
2021-04-24  5:04 ` [ruby-core:103582] " nagachika00

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-91158.20210330061208.48456@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).