From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on starla X-Spam-Level: X-Spam-Status: No, score=-1.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 Received: from nue.mailmanlists.eu (nue.mailmanlists.eu [94.130.110.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 231421F44D for ; Mon, 22 Apr 2024 04:06:17 +0000 (UTC) Authentication-Results: dcvr.yhbt.net; dkim=pass (1024-bit key; secure) header.d=ml.ruby-lang.org header.i=@ml.ruby-lang.org header.a=rsa-sha256 header.s=mail header.b=MkUFF8fJ; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ruby-lang.org header.i=@ruby-lang.org header.a=rsa-sha256 header.s=s1 header.b=ZqDB65ks; dkim-atps=neutral Received: from nue.mailmanlists.eu (localhost [127.0.0.1]) by nue.mailmanlists.eu (Postfix) with ESMTP id 767D38432D; Mon, 22 Apr 2024 04:06:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ml.ruby-lang.org; s=mail; t=1713758767; bh=z2UtNc9YKJG/jCPwtpwOaIZNQONuhs9S5nNBCjNwUFw=; h=Date:References:To:Reply-To:Subject:List-Id:List-Archive: List-Help:List-Owner:List-Post:List-Subscribe:List-Unsubscribe: From:Cc:From; b=MkUFF8fJmjEvab29PeBVZ7zidFaTltEJBJHigclr9G0LcRLPojfhuHPHDKyPoiwyY E0cAB1hr2Xbohrc/bGHEap9MfxFM7HWI/0c97nriBU/H24YHCgblMQfSfuS1bqjUcu mSgLGY5dVhK0l9lGp3SmJDpRb6rqOGrGHjlCVceE= Received: from s.wrqvtbkv.outbound-mail.sendgrid.net (s.wrqvtbkv.outbound-mail.sendgrid.net [149.72.123.24]) by nue.mailmanlists.eu (Postfix) with ESMTPS id EF886842B9 for ; Mon, 22 Apr 2024 04:06:04 +0000 (UTC) Authentication-Results: nue.mailmanlists.eu; dkim=pass (2048-bit key; unprotected) header.d=ruby-lang.org header.i=@ruby-lang.org header.a=rsa-sha256 header.s=s1 header.b=ZqDB65ks; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ruby-lang.org; h=from:references:subject:mime-version:content-type: content-transfer-encoding:list-id:to:cc:content-type:from:subject:to; s=s1; bh=RjIEdFPh5T5Mgt4ytSAg+YeHyNXlnsXaYtDcx5Xn4T0=; b=ZqDB65ks7AiRd63o1NgoRtJb+3Txy5Md8vbbApYAI1VxfHugKMxj6HxNNwdAafdBE0i3 Vbf3SdAEUjD8k4Mg2MoCqQUGQ+yMaCtri9zoAFQXEGcoCOBZ16AIyfw1LrgLWEm+TWZUqT Z6QhY5qbdbd5UoqVCdMhZVGVrFhgdG3nRqQ2Pb/9ileyDqWKNz1diNuA1RwG5boeAqoHKK TP06Dldq9qYF8DG5We33uUKAr/ccGAhlos68MsBEKaEOqZnEeL2Xxi7V3wBGZs9Tj5agil Xr0z5ZqL0eXNoKVR4tZOJH5bzAuK6HvEbu4u4XXXP1st2K+9g+AnlBpoyLXJTJag== Received: by filterdrecv-6db7b8944d-xfq64 with SMTP id filterdrecv-6db7b8944d-xfq64-1-6625E22B-E 2024-04-22 04:06:03.507864264 +0000 UTC m=+800201.235474150 Received: from herokuapp.com (unknown) by geopod-ismtpd-3 (SG) with ESMTP id bBWnKP2BT7W65liFu6FH8A for ; Mon, 22 Apr 2024 04:06:03.464 +0000 (UTC) Date: Mon, 22 Apr 2024 04:06:03 +0000 (UTC) Message-ID: References: Mime-Version: 1.0 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 20215 X-Redmine-Issue-Author: ioquatix X-Redmine-Issue-Priority: Normal X-Redmine-Sender: ioquatix X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-Redmine-MailingListIntegration-Message-Ids: 94232 X-SG-EID: =?us-ascii?Q?u001=2E3QFJPY5gaRwyNXkncYONHM3OeFnb=2FgeOrLDgg3PYIKrq5rvk81iAipQ1q?= =?us-ascii?Q?cdmuT9o5PIWGjXtTDstodNmslT=2F6xfCuIofWR2K?= =?us-ascii?Q?w8msDgVaVLyG1kDq1Na8Wfc9djqR1lpKDredAJj?= =?us-ascii?Q?Rp8GSUGzsrfXitcVF1+73EYNuPbUmGZvDTmWElj?= =?us-ascii?Q?JdDBfbYesggcf2GsdfsrG1CCwXKq=2FGIjsyBouxj?= =?us-ascii?Q?A+IgU=2FOQ71zxNXN1v62uw2FD8+XIfopy9OPBeF3?= =?us-ascii?Q?RaEMTZkkLVT6EijlHQKZdc8j8Q=3D=3D?= To: ruby-core@ml.ruby-lang.org X-Entity-ID: u001.I8uzylDtAfgbeCOeLBYDww== Message-ID-Hash: QVU4WBQQZ2D5TEHNXOFWZVEIYVKW36H2 X-Message-ID-Hash: QVU4WBQQZ2D5TEHNXOFWZVEIYVKW36H2 X-MailFrom: bounces+313651-b711-ruby-core=ml.ruby-lang.org@em5188.ruby-lang.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.3 Precedence: list Reply-To: Ruby developers Subject: [ruby-core:117636] [Ruby master Feature#20215] Introduce `IO#readable?` List-Id: Ruby developers Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: "ioquatix (Samuel Williams) via ruby-core" Cc: "ioquatix (Samuel Williams)" Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Issue #20215 has been updated by ioquatix (Samuel Williams). Regarding naming, I had other ideas like `open?` or `connected?` but I think they have their own issues. I think my preference is `readable?` but I'd like to hear alternatives. Maybe `open?` is okay but it sounds like the opposite of `closed?` which strictly speaking it isn't. Regarding implementation, I spent several hours debugging usage of `eof?`. Because `eof?` can mutate the underlying buffer, it's not concurrency safe either. This was the source of an extremely hard to diagnose bug where blocking `read` and `eof?` occurred in different fibers.. Maybe we should create a separate issue, to make `eof?` safer (i.e. [it doesn't call `io_fillbuf` internally](https://github.com/ruby/ruby/blob/d42a8d66024f0a86c5a162eeffff1ab91ad9fa43/io.c#L2677)). ---------------------------------------- Feature #20215: Introduce `IO#readable?` https://bugs.ruby-lang.org/issues/20215#change-108044 * Author: ioquatix (Samuel Williams) * Status: Open ---------------------------------------- There are some cases where, as an optimisation, it's useful to know whether more data is potentially available. We already have `IO#eof?` but the problem with using `IO#eof?` is that it can block indefinitely for sockets. Therefore, code which uses `IO#eof?` to determine if there is potentially more data, may hang. ```ruby def make_request(path = "/") client = connect_remote_host # HTTP/1.0 request: client.write("GET #{path} HTTP/1.0\r\n\r\n") # Read response client.gets("\r\n") # => "HTTP/1.0 200 OK\r\n" # Assuming connection close, there are two things the server can do: # 1. peer.close # 2. peer.write(...); peer.close if client.eof? # <--- Can hang here! puts "Connection closed" # Avoid yielding as we know there definitely won't be any data. else puts "Connection open, data may be available..." # There might be data available, so yield. yield(client) end ensure client&.close end make_request do |client| puts client.read # <--- Prefer to wait here. end ``` The proposed `IO#readable?` is similar to `IO#eof?` but rather than blocking, would simply return false. The expectation is the user will subsequently call `read` which may then wait. The proposed implementation would look something like this: ```ruby class IO def readable? !self.closed? end end class BasicSocket # Is it likely that the socket is still connected? # May return false positive, but won't return false negative. def readable? return false unless super # If we can wait for the socket to become readable, we know that the socket may still be open. result = self.recv_nonblock(1, MSG_PEEK, exception: false) # No data was available - newer Ruby can return nil instead of empty string: return false if result.nil? # Either there was some data available, or we can wait to see if there is data avaialble. return !result.empty? || result == :wait_readable rescue Errno::ECONNRESET # This might be thrown by recv_nonblock. return false end end ``` For `IO` itself, when there is buffered data, `readable?` would also return true immediately, similar to `eof?`. This is not shown in the above implementation as I'm not sure if there is any Ruby method which exposes "there is buffered data". -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/