Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107505 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 99503 invoked from network); 11 Oct 2019 16:04:09 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 11 Oct 2019 16:04:09 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id B653A2D2030 for ; Fri, 11 Oct 2019 06:47:18 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp3.php.net X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: X-Spam-Virus: No Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp3.php.net (Postfix) with ESMTPS for ; Fri, 11 Oct 2019 06:47:15 -0700 (PDT) Received: by mail-ed1-x52e.google.com with SMTP id c4so8759766edl.0 for ; Fri, 11 Oct 2019 06:47:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=HCHvpSx/459lZimnEQeZ6/Kx/gV8603qhbUKPW6Iguo=; b=UCzrsWzmKc69Zea0WAaIFuzIGGh8HRcLV0OLgXnbr18YYFRqyHv12C8v+YOP/Ar6yz 49bmFL8NVtY5wa06iG1c2aB/mw31Q1BK96pLabpeuQUFTpyxaxn28vZT1J3JAvibxdqA x3zzHLfa55OQZRbFma5kTXYDyhz41BTmf9RJWVAekAZCL+x3Ddtr59ZpsBadoCZR++Eq SYgmib1HSou5Upw0FDf6CUANHxtoH+OACnqd2QcRoBoEK5SmfVS7ppYDUpfImemX2pWJ 2SNWIA+dD6zDHXc7kKwk6DiDK2ifHC5hJOIedeiYPsAatCOIr5v24ZsiQRSqcZd/x9hD 5Yhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=HCHvpSx/459lZimnEQeZ6/Kx/gV8603qhbUKPW6Iguo=; b=Jzv/BG139p9i2nIWKC9YXwE3dHMY2vj6uRbYJQrDRNHsEq3ka6E3QHbWKNyGyHFuwv zzGrr2ykrm9w1+Uz8+UNvsyds3jr0GafnMVb4rKs4IpA6/Muw8u62eNs6VBi8+VxdSuD YA4j17gJPlpWGlAhCbd5aWQv66cdDRXaQHptbhiH96GWLxBpdtsio3StTlq8sktcD0y9 hgFW/lX6uogX2hWNTIiR6Esd18K6pKOKvmDcZo8g3LlmN7A0+ik/TVNudBJvwfmARDXq GN2bpOFyVFSBR6Gw5mOdTBkQO/mIRmo63f7qvzytaBC9+5Awu6AuvXi+2ZffkXb0VxjS bWQw== X-Gm-Message-State: APjAAAXwW8QewmeVn+Opk8aCgTG9pNEEEu71Qe+ll2+rL4Ri+LA1G7Pu 5XiTuuaWLaHEetNGB4LNaQKB7xMj7JUZr/8yDVc= X-Google-Smtp-Source: APXvYqw1PyJt51UprGMh3SDbBY2546ovxygauGmsvigVuMQTmBfq0SxdvU5J3gS3S4WB37u8xZ9f2hpK6OuvBTRKSBQ= X-Received: by 2002:a17:906:6bcd:: with SMTP id t13mr13816735ejs.133.1570801634151; Fri, 11 Oct 2019 06:47:14 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 11 Oct 2019 10:47:03 -0300 Message-ID: To: Nikita Popov Cc: PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Envelope-From: Subject: Re: [PHP-DEV] exit() via exception From: marcio.web2@gmail.com (Marcio Almada) Em sex, 11 de out de 2019 =C3=A0s 08:05, Nikita Popov escreveu: > > Hi, > Hello :) > Currently exit() is implemented using bailout and unclean shutdown, which > means that we're going to perform a longjmp back to the top-level scope a= nd > let the memory manager clean up all the memory it knows about. Anything n= ot > allocated using ZMM is going to leak persistently. > > For me, one of the most annoying things about this is that we can't perfo= rm > proper leak checks on code using PhpUnit, because it will always exit() a= t > the end, which will result in "expected" memory leaks. > > I think it would be good to switch exit() to work by throwing a magic > exception, similar to what Python does. This would allow us to properly > unwind the stack, executing finally blocks (which are currently skipped) > and perform a clean engine shutdown. > > Depending on the implementation, we could also allow code to actually cat= ch > this exception, which may be useful for testing scenarios, as well as > long-running daemons. > > I'm mainly wondering how exactly we'd go about integrating this in the > existing exception hierarchy. > Assuming that it is desirable to allow people > to actually catch this exception > my first thought would be along these > lines: > > Throwable (convert to abstract class) > \-> Exception > \-> Error > \-> ExitThrowable > > This does mean though that existing code using catch(Throwable) is going = to > catch exit()s as well. This can be avoided by introducing *yet another* > super-class/interface above Throwable, which is something I'd rather avoi= d. > Since you brought python as inspiration, I believe the hierarchy goes like this on their land: BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit +-- Exception +-- [kitchen sink] Being `BaseException` the base class for all built-in exceptions. It is not meant to be directly inherited by user-defined classes. It 's the equivalent to our `Throwable` situation. In this context `ExitThrowable -> Throwable ` appears legit. > > Anyone have thoughts on this matter? > Yes. There is an obvious can of worms if I've got this right: `exit()` and `die()` would no longer guarantee a program to actually terminate in case catching `ExitThrowable` is allowed. Python solves this by actually having two patterns: 1. `quit()`, `exit()`, `sys.exit()` are the equivalent to `raise SystemExit`, can be caught / interrupted 2. `os._exit()`, can't be caught but has a callback mechanism like our `register_shutdown_function`, see https://docs.python.org/3/library/atexit.html If we bind `exit()` and `die()` to a catchable exception how would we still have the scenario 2 available on PHP land without a BCB? :) I have one simple suggestion: Introduce `EngineShutdown -> Throwable`, bind `exit|die` to it but disallow `catch(\EngineShutdown $e)` at compile time. This would allow keeping backwards compatibility to scenario 2 without messing with our current exception hierarchy. > Nikita Thanks, M=C3=A1rcio