ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "jeremyevans0 (Jeremy Evans)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:109780] [Ruby master Feature#18982] Add an `exception: false` argument for Queue#push, Queue#pop, SizedQueue#push and SizedQueue#pop
Date: Tue, 30 Aug 2022 00:42:40 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-99016.20220830004239.7941@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-18982.20220829102034.7941@ruby-lang.org

Issue #18982 has been updated by jeremyevans0 (Jeremy Evans).


shyouhei (Shyouhei Urabe) wrote in #note-4:
> +1 for avoiding exceptions but `nil` can be problematic?  Because a closed queue would also return `nil` for `pop`.  You cannot distinguish if a queue is closed or would just block.

We could add another keyword argument for the exception value, and have that value returned instead of raising an exception (the keyword argument would default to `nil`).  I'm not sure if it's worth supporting that, but it is a simple approach.

----------------------------------------
Feature #18982: Add an `exception: false` argument for Queue#push, Queue#pop, SizedQueue#push and SizedQueue#pop
https://bugs.ruby-lang.org/issues/18982#change-99016

* Author: byroot (Jean Boussier)
* Status: Open
* Priority: Normal
----------------------------------------
This replaces [Feature #18965]

Currently these methods raise in three occasions:

  - `ThreadError(queue empty)` for `#pop` in `nonblock=true` mode, and the operation would block.
  - `ThreadError(queue full)` for `SizedQueue#push` in `nonblock=true` mode, and the operation would block.
  - `ClosedQueueError` if trying to `#push` in a closed queue.

I see several reasons to prefer a `nil` return value.

- Queue is often used in conjunction with threads, so you have to be very careful not to rescue an unrelated `ThreadError`.
- Queue if often used for low level code, deep in the stack, so exceptions are costly.

I propose that passing `exception: false` would cause the method to return `nil` instead of raising in the three cases listed above.

The argument `exception: true` is consistent with various other methods such as `IO#read_nonblock(exception: false)`.



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

  parent reply	other threads:[~2022-08-30  0:42 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-29 10:20 [ruby-core:109756] [Ruby master Feature#18982] Add an `exception: false` argument for Queue#push, Queue#pop, SizedQueue#push and SizedQueue#pop byroot (Jean Boussier)
2022-08-29 10:23 ` [ruby-core:109758] " byroot (Jean Boussier)
2022-08-29 10:24 ` [ruby-core:109759] " Eregon (Benoit Daloze)
2022-08-30  0:08 ` [ruby-core:109778] " shyouhei (Shyouhei Urabe)
2022-08-30  0:15 ` [ruby-core:109779] " byroot (Jean Boussier)
2022-08-30  0:42 ` jeremyevans0 (Jeremy Evans) [this message]
2022-08-30  8:36 ` [ruby-core:109786] " shyouhei (Shyouhei Urabe)
2022-09-22  4:50 ` [ruby-core:109988] " ko1 (Koichi Sasada)
2022-09-22  6:46 ` [ruby-core:109990] " byroot (Jean Boussier)
2022-10-06  6:47 ` [ruby-core:110199] " ko1 (Koichi Sasada)
2022-10-06  6:49 ` [ruby-core:110200] " byroot (Jean Boussier)

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