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 [IPv6:2a01:4f8:1c0c:6b10::1]) (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 5AAFC1F44D for ; Wed, 17 Apr 2024 01:13:33 +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=RU39YqIs; 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=hn1goTl1; dkim-atps=neutral Received: from nue.mailmanlists.eu (localhost [127.0.0.1]) by nue.mailmanlists.eu (Postfix) with ESMTP id 1792C8430C; Wed, 17 Apr 2024 01:13:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ml.ruby-lang.org; s=mail; t=1713316402; bh=ivhCzO+03cJ3kBcUl1CErXKnV0b99XH+BaXxh+40dTk=; 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=RU39YqIsmoa86oZI/xgfQVwCUHia+OpvyfdTdCD7LN6yUeO7u/AkSlBuw3lXf3N/V /+nKg4g7ivr4Z6bZwl/pHyTy7fiTBb1M9wI8qt7rxiF6RKBSZ7qsl/2vot5tewpf3m OAcQwMnkmhMiDt/7VbvD7dpTkD48uRE0P/qhE60Q= Received: from s.wfbtzhsv.outbound-mail.sendgrid.net (s.wfbtzhsv.outbound-mail.sendgrid.net [159.183.224.104]) by nue.mailmanlists.eu (Postfix) with ESMTPS id D86DF84306 for ; Wed, 17 Apr 2024 01:13:18 +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=hn1goTl1; 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=C4660jtmphtpUSdmada3tu/ba1BCNXHV9wbPk9xiLao=; b=hn1goTl1Z1NNBk3V4rQEEqWcs6E+rqQfMUEX8THDk4aZdAEmSNO02NpFoNj+WbIIlyX9 stWUoZvoSArHmeDjzYgRNUWeKU8Zy445B2ExJ+d4tmxujQtgvoIJslAxdq9//s+wqTXG6h yVgp0Cpi2L97zJCuMzixmFN5JVFoZ63FtifLrpYvqrqbcnbQG5X/d3ZbhMT+6rBPWejvwl qYxYk2DlZAuO5nj8xMvqhT9wTphKvvEjEokCQHG0axT82MFbzs2Zv76TFEdsOot7zOQCp4 gl/AHxrGo2HHBxWPTHtuaZm9BxCet1u2NwSeQ4n3Gd5j4KbDBGvfLaUfB+guB4jA== Received: by recvd-547d99795c-6s9kc with SMTP id recvd-547d99795c-6s9kc-1-661F222D-7 2024-04-17 01:13:17.930722317 +0000 UTC m=+357159.326491630 Received: from herokuapp.com (unknown) by geopod-ismtpd-2 (SG) with ESMTP id bXJVfCp9Rla1Ig1heFA1kQ for ; Wed, 17 Apr 2024 01:13:17.915 +0000 (UTC) Date: Wed, 17 Apr 2024 01:13:17 +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: akr 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: 94137 X-SG-EID: =?us-ascii?Q?u001=2EjUYdomjR8qg=2FmRRJiw9jcD8=2F=2Fg2V+YCwM42AbEffAz+piO1ivD2ssVdIf?= =?us-ascii?Q?UsAvXwsSNeFNBeVO12+lrvx7XL=2FCyQz9srcFMO2?= =?us-ascii?Q?qGFRSIbkxmupWNuTzsOAZFXExDDJIBdO1T1eckK?= =?us-ascii?Q?Hcb7RIeeenvcyXuYJcU0H62FgdisQjR1bKezxUt?= =?us-ascii?Q?LVwaZ=2FPlcfXB03eR7OXkKys6YZStvyKqYTOoP5y?= =?us-ascii?Q?bXo1NZLezkcZDag3yXhwxMtMw07yM6BFYPEv3vF?= =?us-ascii?Q?Am6n?= To: ruby-core@ml.ruby-lang.org X-Entity-ID: u001.I8uzylDtAfgbeCOeLBYDww== Message-ID-Hash: FMFRYFCUSSJRSJGR4CBFG6LZNW5DBG5R X-Message-ID-Hash: FMFRYFCUSSJRSJGR4CBFG6LZNW5DBG5R 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:117542] [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: "akr (Akira Tanaka) via ruby-core" Cc: "akr (Akira Tanaka)" Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Issue #20215 has been updated by akr (Akira Tanaka). I couldn't understand the motivation of this issue. However, the state of the read side of unidirectional data flow (Unix pipe, half of a stream socket, etc.) can be one of the following. 1. We can read 1 or more bytes immediately. 2. We can detect EOF immediately. 3. We cannot determine data/EOF immediately. If we wait indefinitely, we can read 1 or more bytes. 4. We cannot determine data/EOF immediately. If we wait indefinitely, we can detect EOF. `io.eof?` returns true on 2 and 4, false otherwise. (So, it blocks on 3 and 4.) `io.wait_readable(0)` returns truthy on 1 and 2, falsy otherwise. They are enough to distinguish the states. If we don't want to block, it is impossible to distinguish 3 and 4, though. ---------------------------------------- Feature #20215: Introduce `IO#readable?` https://bugs.ruby-lang.org/issues/20215#change-107934 * 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/