ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: shevegen@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:98061] [Ruby master Bug#16816] Prematurely terminated Enumerator should stay terminated
Date: Fri, 24 Apr 2020 22:04:13 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-85288.20200424220413.286@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16816.20200424203407.286@ruby-lang.org

Issue #16816 has been updated by shevegen (Robert A. Heiler).


( Just a brief typo-correction, I think you meant the link https://github.com/jruby/jruby/issues/6157 and not https://github.com/jruby/jruby/issue/6157 )

----------------------------------------
Bug #16816: Prematurely terminated Enumerator should stay terminated
https://bugs.ruby-lang.org/issues/16816#change-85288

* Author: headius (Charles Nutter)
* Status: Open
* Priority: Normal
* ruby -v: all
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
When iterating over an Enumerator, there are three different possible results of calling `next`:

1. The next item is returned and a cursor is advanced
2. There's no next item and the Enumerator will forever raise `StopIteration`
3. There's an error getting the next item which is raised out of `next`

This third case has some unexpected behavior that I discovered while working on https://github.com/jruby/jruby/issue/6157

It seems that when an Enumerator fails prematurely with an exception, any subsequent call to #next will cause it to restart.

This can be seen in a simple script I used to write a ruby/spec in https://github.com/jruby/jruby/pull/6190

```ruby
Enumerator.new {
  2.times {|i| raise i.to_s }
}.tap {|f|
  p 2.times.map { f.next rescue $!.message }
}
```

The output from this is `[0, 0]`. After the iteration fails, the second `next` call causes it to restart and it fails again.

Contrast this to the behavior of item 3 above; when an Enumerator finishes iterating without error, it remains "finished" forever and can't be restarted.

I believe the restarting behavior is at best undocumented behavior and at worst incorrect and unspected. Take this example:

```ruby
e = Enumerator.new { |y|
  c = new_database_cursor
  5.times { y.yield c.next_result }
}
```

If `next_result` here raises an error, a subsequent call to `next` on this enumerator will cause it to restart, re-acquire the cursor, and begin again.

As another example I ask a question: how do you indicate that an Enumerator failed due to an error, and *keep it failed* so it doesn't restart again?



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

  parent reply	other threads:[~2020-04-24 22:04 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-24 20:34 [ruby-core:98059] [Ruby master Bug#16816] Prematurely terminated Enumerator should stay terminated headius
2020-04-24 20:36 ` [ruby-core:98060] " headius
2020-04-24 22:04 ` shevegen [this message]
2020-04-25  8:05 ` [ruby-core:98063] " headius
2020-05-07  8:17 ` [ruby-core:98176] " ko1
2020-05-07  8:25 ` [ruby-core:98177] " mame
2020-05-07 20:02 ` [ruby-core:98206] " eregontp
2020-12-21 13:46 ` [ruby-core:101587] " intrip
2021-04-09 21:24 ` [ruby-core:103355] [Ruby master Feature#16816] " merch-redmine

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