From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-2.6 required=3.0 tests=AWL,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY shortcircuit=no autolearn=no autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 8B5F01F66F for ; Sun, 15 Nov 2020 20:48:44 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 03A5F120999; Mon, 16 Nov 2020 05:47:59 +0900 (JST) Received: from xtrwkhkc.outbound-mail.sendgrid.net (xtrwkhkc.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 98D10120993 for ; Mon, 16 Nov 2020 05:47:57 +0900 (JST) Received: by filterdrecv-p3mdw1-859677f5fd-r8k6r with SMTP id filterdrecv-p3mdw1-859677f5fd-r8k6r-19-5FB19421-C 2020-11-15 20:48:33.238584507 +0000 UTC m=+406221.149484171 Received: from herokuapp.com (unknown) by ismtpd0011p1iad2.sendgrid.net (SG) with ESMTP id nU-eUutJRGKkmT_HKYD5cA for ; Sun, 15 Nov 2020 20:48:33.134 +0000 (UTC) Date: Sun, 15 Nov 2020 20:48:33 +0000 (UTC) From: nicholas.evans@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 76730 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 17325 X-Redmine-Issue-Author: nevans X-Redmine-Sender: nevans 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-SG-EID: =?us-ascii?Q?8M9XtQFPepB0Vyl+xsp5GpqSbmdIIu5RpRwAjm7cAEXIZzgampiQmYn=2F+X6xjU?= =?us-ascii?Q?p9JqlcIvk2CwAiacalIDzgLYoHKbZRgga1TDPbV?= =?us-ascii?Q?75E0tN3D8QQAAz0FPAdyl186F5=2FgakYP4qP9hn8?= =?us-ascii?Q?OxTEedQlCBTK1tjSzOaatPAiPAk=2Fo1MjtsFqf5O?= =?us-ascii?Q?L83uvr4cvv+D3pw9nyXyvJa=2FMaG5XgNEmlrLPk5?= =?us-ascii?Q?k0JhL38u=2FDKO4+4lk=3D?= To: ruby-core@ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== X-ML-Name: ruby-core X-Mail-Count: 100857 Subject: [ruby-core:100857] [Ruby master Feature#17325] Adds Fiber#cancel, which forces a Fiber to break/return X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #17325 has been updated by nevans (Nicholas Evans). Description updated I've updated the description to 1) better describe the motivation and 2) match the PR's current behavior when a Fiber is canceled multiple times. ---------------------------------------- Feature #17325: Adds Fiber#cancel, which forces a Fiber to break/return https://bugs.ruby-lang.org/issues/17325#change-88502 * Author: nevans (Nicholas Evans) * Status: Open * Priority: Normal ---------------------------------------- Calling `Fiber#cancel` will force a fiber to return, skipping rescue and catch blocks but running all ensure blocks. It behaves as if a `break` or `return` were used to jump from the last suspension point to the top frame of the fiber. Control will be transferred to the canceled fiber so it can run its ensure blocks. ## Propagation from resuming to resumed fibers Any non-root living fiber can be canceled and cancellation will propagate to child (resumed) fibers. In this way, a suspended task can be canceled even if it is e.g. resuming into an enumerator, and the enumerator will be canceled as well. Transfer of control should match #17221's *(much improved)* transfer/resume semantics. After the cancellation propagates all the way to the bottom of the fiber resume stack, the last fiber in the chain will then be resumed. Resuming fibers will not run until they are yielded back into. ## Suspension of canceled fibers Canceled fibers can still transfer control with `resume`, `yield`, and `transfer`, which may be necessary in order to release resources from `ensure` blocks. For simplicity, subsequent cancels will behave similarly to calling `break` or `return` inside an `ensure` block, and the last cancellation reason will overwrite earlier reasons. ## Alternatives `Fiber#raise` could be used, but: * Exceptions are bigger and slower than `break`. * `#raise` can't (and shouldn't) be sent to resuming fibers. (It can't propagate.) * Exceptions can be caught. This might be desirable, but that should be at the discretion of the calling fiber. Catch/Throw could be used (with an anonymous `Object.new`), but: * `catch` adds an extra stack frame. * It would need to add `Fiber#throw` (or wrap/intercept `Fiber.yield`). * A hypothetical `Fiber#throw` should probably only be allowed on yielding fibers (like `Fiber#resume`). (It wouldn't propagate.) Implementation: https://github.com/ruby/ruby/pull/3766 -- https://bugs.ruby-lang.org/