Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:130639 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id 201491A00BC for ; Wed, 15 Apr 2026 14:52:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1776264766; bh=c4LNlPjBVA9OQ6fNyU7ct1TZB6w8vDLcPsSVFhKsZMo=; h=Date:From:To:In-Reply-To:References:Subject:From; b=B+LLFzPs45+MZipenCrrS8nbRksUVyf5qw50oNnt4f1xLvWk4aknM2EhMoaGgn3FB a1zgE8zAxQBw3thlfLMsl3EanZRPEI5w5HwkISo/R7ZIBG/kqnO5ZbdOsTKSDac36P 7h7Lp2HSwiO6FKn3RaZnjSfKHgKddKYqKWqVP/bz5UNvGUI32q6qGxU1EQlxHTc+RT av1CMH2z1mzpmc5r+2QR7J1AQdXeBMpdjceE9xcByeIc6Tg3GGgwMebuWmT2EdZ1En O+eoSsawa6nm6XDXyMUkucQo5y3T4KrpPLCT0+DKLP9pXVVVkW9V+jn8K9xXHuVdUR mZtYfC8YYKS7w== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9890F180088 for ; Wed, 15 Apr 2026 14:52:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from fhigh-b5-smtp.messagingengine.com (fhigh-b5-smtp.messagingengine.com [202.12.124.156]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 15 Apr 2026 14:52:45 +0000 (UTC) Received: from phl-compute-04.internal (phl-compute-04.internal [10.202.2.44]) by mailfhigh.stl.internal (Postfix) with ESMTP id E9AC07A0098 for ; Wed, 15 Apr 2026 10:52:38 -0400 (EDT) Received: from phl-imap-02 ([10.202.2.81]) by phl-compute-04.internal (MEProxy); Wed, 15 Apr 2026 10:52:38 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to; s=fm2; t=1776264758; x=1776351158; bh=2rYXKb34oln61GQlgWbnr Z/4QIWem+F7wBAoPe0ufOw=; b=drnamt7YPks/q++LELZ7B3M/lNQoM7I3FQ3Ri +swe7rH0ydjxLSanqSF/+uXjy1jasX0fWhPOqRYwGvjAySY3I93zofN10oT2Jw7x 9U+d8XWdexHi8KHn2fO65kGWmAJCoR8+ICcMlCet6GS/Fy/AikbvHLY0Sn6sF1sv XMH3ZJDrUlDxPl6YbsVohwOwgRCNSV8hmRs7xgCcaX/2gWHnJ/uuYGvA5NTOy57v kGOxa94K04xajQJXM6nCYr3VymcQMEFaMsr9Wm/T5toV8UYM6uMvXC0vvaIszbiO aCPxX/Bu0lTdevcBI/iXzMwlyQt4h4YerGk6wvJpjufAjpcfw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; t=1776264758; x=1776351158; bh=2 rYXKb34oln61GQlgWbnrZ/4QIWem+F7wBAoPe0ufOw=; b=OLxvNSicD2+WEF2RR kovaVLXQzeHF6QcsTVlQUVY9CDB5ua/77IvR38EhJ11CjrN4Atj0Vr6gDuC1p5GV 5vlGilTY8GPv7uLZdsNeot8SImeIkKsCc8ogvMi2mI4l5337Yt7Eekvrlpsfs6GS jRwUMQyng1kR7cc8+fjSo+uRWg1OszFR/bH9oQS88jqQmTI/lRvWvUYe20FgFFaI ch2IE/EKRb791uLR7zqrAC7xVYMdtay/txa+qaFy58BvRAU0nSLZ3UDnNbJEa/yI +zUEov3W38f+HP9cLo3nRcZOUrqWPR1aksUGGezj5RokmVnxg8S6DHy8mTRWmAV0 02E2A== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefhedrtddtgdeggeefkecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpefoggffhffvkfgjfhfutgfgsehtqhertdertdejnecuhfhrohhmpedfnfgrrhhrhicu ifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomheqne cuggftrfgrthhtvghrnhepuefgteeijeeuveffudelhffhtefhkeevtdeuvefgffdvfeei vdetgfehveetleffnecuffhomhgrihhnpehphhhprdhnvghtnecuvehluhhsthgvrhfuih iivgeptdenucfrrghrrghmpehmrghilhhfrhhomheplhgrrhhrhiesghgrrhhfihgvlhgu thgvtghhrdgtohhmpdhnsggprhgtphhtthhopedupdhmohguvgepshhmthhpohhuthdprh gtphhtthhopehinhhtvghrnhgrlhhssehlihhsthhsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 3A44D700065; Wed, 15 Apr 2026 10:52:38 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 X-ThreadId: Atmh9k-BTBuN Date: Wed, 15 Apr 2026 09:52:17 -0500 To: "php internals" Message-ID: In-Reply-To: References: <4985896b-c80a-4302-912e-9f572a260fb5@app.fastmail.com> Subject: Re: [PHP-DEV] [RFC] Context Managers Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: larry@garfieldtech.com ("Larry Garfield") On Tue, Apr 14, 2026, at 9:27 AM, Rob Landers wrote: > On Tue, Apr 14, 2026, at 16:18, Rob Landers wrote: >>=20 >>=20 >> On Tue, Nov 4, 2025, at 21:13, Larry Garfield wrote: >>> Arnaud and I would like to present another RFC for consideration: Co= ntext Managers. >>>=20 >>> https://wiki.php.net/rfc/context-managers >>>=20 >>> You'll probably note that is very similar to the recent proposal fro= m Tim and Seifeddine. Both proposals grew out of casual discussion seve= ral months ago; I don't believe either team was aware that the other was= also actively working on such a proposal, so we now have two. C'est la= vie. :-) >>>=20 >>> Naturally, Arnaud and I feel that our approach is the better one. I= n particular, as Arnaud noted in an earlier reply, __destruct() is unrel= iable if timing matters. It also does not allow differentiating between= a success or failure exit condition, which for many use cases is absolu= tely mandatory (as shown in the examples in the context manager RFC). >>>=20 >>> The Context Manager proposal is a near direct port of Python's appro= ach, which is generally very well thought-out. However, there are a few= open questions as listed in the RFC that we are seeking feedback on. >>>=20 >>> Discuss. :-) >>>=20 >>> --=20 >>> Larry Garfield >>> larry@garfieldtech.com >>>=20 >>=20 >> Hi Larry/Arnaud, >>=20 >> This is a pretty exciting thread and fascinating proposal. That being= said, I have a couple of subtle questions that don't seem to be answere= d in the (very long) thread or the RFC itself -- If I missed it, please = let me know: >> 1. What happens if a Fiber is suspended in the using block and neve= r resumed? When is the using block released to clean up the context? Since it decomposes to a try-catch-finally, it will exit whenever the fi= nally block would have run if you'd just typed out try-catch-finally you= rself. Arnaud checked, and confirmed that when the fiber is destroyed t= he using block will exit in a success case (ie, exitContext(null)). >> 2. There's still no mention of how this should affect debugging, wil= l we see the "desugared" or "sugared" version? Is that even a concern fo= r the RFC? Error messages would see the original code, so "error on line X" would b= e based on the original `using` block. That's the same as any other des= ugaring we already do. (PIpes, PFA, constructor promotion, etc.) Debug= gers will see the materialized opcodes, again, the same other desugaring= cases. >> 3. I will say it is weird to have exitContext return an exception; b= ut what happens if an exception is thrown during exitContext? Why not ju= st have it return void and throw if you need to throw instead of having = two paths to the same thing? There's a subtle but important difference here: An exception passed thro= ugh exitContext() is the original exception from lower in the call stack= , and its backtrace will be the original location of the error. An exce= ption thrown from within exitContext() itself indicates a failure that t= he Context Manager is responsible for, usually an error in the exitConte= xt() logic itself. Technically a Context Manager can wrap-and-rethrow the exception if it w= ants, but then it is "claiming ownership" over it, just like in any othe= r case of wrap-and-rethrow. Our expectation is that 90% of the time, "let the exception propagate up= unimpeded" is the desired behavior. This approach makes "return $e" th= e right thing to do almost-always, which is nice and simple to remember. See the "return values and exception handling" section for a discussion = of this in more detail. As I said in a previous reply, our constraints = are different than Python's so we end up with a different solution. If = you have a suggestion for an alternate approach to the problem, we're ha= ppy to listen. >> 4. Looking at the desugared form ... I'm a bit confused: if exitCont= ext is called during the finally path and returns an exception, it is ju= st swallowed? But if it is thrown, it won't be? The finally path is only reached in case of a successful exit. Therefor= e there is no exception to pass in, and thus returning an exception is m= eaningless. If exitContext() throws a new exception of its own (which w= ould indicate an error in its own logic), that will just bubble up past= the `using` block entirely, which is what we want. >> 5. That being said, I don't think the RFC shares with us when we sho= uld return an exception vs. throw an exception. See the "return values and exception handling" section. If something th= ere isn't clear, let me know and I will try to clarify further. >> =E2=80=94 Rob > > Maybe the desugared version should look more like this? > > } catch (\Throwable $e) { > try { > $__mgr->exitContext($e); > } catch (\Throwable $cleanupException) { > throw new ContextManagerException( > $cleanupException->getMessage(), > previous: $e > ); > } > throw $e; > } > I'm not sure I see a reason to force any new exceptions to be only of th= e ContextManagerException type. If there's a TypeError inside exitConte= xt() or something, I'd expect that to be propagated as a TypeError. --Larry Garfield